vendredi 30 janvier 2015

Passing down symbol from a for loop to an if statement

It seems that a symbol in a "for loop" is local to the loop itself. Is there any way to pass the information about what the symbol is currently representing down to, for example, an if statement?


For example, this generates an error:



int[] myAry = {1, 2, 3};
for (int i : myAry)
if (i == 2);
System.out.println(i);


While this doesn't:



int[] myAry = {1, 2, 3};
for (int i : myAry)
if (i == 2);
System.out.println("Match found!");

Aucun commentaire:

Enregistrer un commentaire