While loops and for loops: Correct Solution


Part 1

Translate the following loop into a for-each loop:

Glenve[] cous;
...
for (int i = 0; i < cous.length; i++) {
    toid();
    vess(enha, cous[i]);
    ournin(-2, cous[i]);
}

Solution

for (Glenve cou : cous) {
    ournin(-2, cou.get(i));
    vess(enha, cou.get(i));
    toid();
}

It is OK if you gave the variable for the individual collection element (cou) a different name, such as elem. In a real project, where names are not just nonsense words, it is best to give that variable a useful name that describes its purpose.


Related puzzles: