java basics @ mlab, 15-19 Nov 1999, juhuu@katastro.fi


if if (condition) { do something; } example: if (a > 100) { a = 0; } different conditions: == equal != inequal > greater than >= equal or greater than < less than <= equal or less than to combine these you can use && AND || OR examples: if ( a == b && b == c) { // if a is equal to b AND b is equal to c } if ( a == b || b == c) { // if a is equal to b OR b is equal to c }
if - else if (condition) { do something; } else { do something else; } or if (condition) { do something; } else if (condition2) { do something else; } else { do something else; }
while while (condition) { do something; }
for for is short for: initialize something; while (condition) { do something; iterate; } syntax: for ( initialization ; condition ; iteration) { do something; } example: for (int i = 1 ; i <= 100 ; i++) { System.out.println("" + i); } this routine prints out numbers from 1 to 100. i++ is the same thing as i = i + 1.
you can break out from a while/for loop with break command example: while (condition) { do something; if (condition2) { break; } }