124k views
0 votes
In reference to for-each loops, which of the following statements is true? f

A) For-each loops can be rewritten as for loops
B) an index variable is NOT explicitly coded
C) an ArrayIndexOutOfBoundsException will always be thrown

User Elexhobby
by
6.6k points

1 Answer

2 votes

Answer:

Option B: an index variable is NOT explicitly coded

Step-by-step explanation:

For-each loops appear in some programming languages such as Java and JavaScript. It is sometimes considered as an enhanced version of for loop to traverse elements in an array.

To traverse the elements of an array using the For-Each loop, index variable is not required. The syntax of For-Each loop in Java is as follows:

for(type variableName : arrayName)

{

// some codes

}

One sample code to traverse array using For-each loop is as follows:

  1. int myArray[] = {3, 5, 7, 9, 11};
  2. for(int num: myArray)
  3. {
  4. System.out.println(num);
  5. }

The Java codes above will output 3 5 7 9 11. In every round of the iteration of the For-each loop, the loop variable num will just take one value of element in the array and print it out one after another.

User Djphinesse
by
6.5k points