for Statement Subtleties
Better to declare loop index in for to limit it’s scope. Prefer:
for (int i = 0; i < 10; ++i)
to:
int i; // Bad. Looop index variable visible outside loop.
for (i = 0; i < 10; ++i)
You can have multiple loop indexes separated by commas:
String mystery = "mnerigpaba", solved = ""; int len = mystery.length();
for (int i = 0, j = len - 1; i < len/2; ++i, --j) {
solved = solved + mystery.charAt(i) + mystery.charAt(j);
}
Note that the loop above is one loop, not nested loops.
Beware of common “extra semicolon” syntax error:
for (int i = 0; i < 10; ++i); // oops! semicolon ends the statement
print(meow); // this will only execute once, not 10 times
Chris Simpkins (Georgia Tech) CS 1331 Loops 8 / 12