97.3k views
2 votes
Given an array of Strings called names, write a while loop, for loop and for each loop to print

each element of names on its own line. You may assume that each element of names
contains a valid String.



Write a while loop
Write a for loop here
Write afor each loop here.

User Meshtron
by
6.0k points

2 Answers

4 votes

Here is an example of how to print each element of an array of Strings using a while loop, a for loop, and a for-each loop in Java:

While loop:

String[] names = {"John", "Jane", "Jim", "Joan"};

int i = 0;

while (i < names.length) {

System.out.println(names[i]);

i++;

}

For loop:

String[] names = {"John", "Jane", "Jim", "Joan"};

for (int i = 0; i < names.length; i++) {

System.out.println(names[i]);

}

For-each loop:

String[] names = {"John", "Jane", "Jim", "Joan"};

for (String name : names) {

System.out.println(name);

}

In each of these examples, the array names contains four Strings, and each loop iterates through the array and prints each element on its own line.

User Banky
by
7.2k points
0 votes

Answer:

five

Step-by-step explanation:

User Naytzyrhc
by
6.7k points