

variable = expression ; 
Example:
x = 4; // Store the value 4 in variable x x = x + 1; // Read the value in variable x, add 1 to it, // then store result in variable x 
Note:


if ( condition ) onestatement 
Example:
// Code to compute the absolute value of x if ( x < 0 ) x = x; // Negate x if x is negative 
if ( condition ) onestatement1 else onestatement2 
Example:
// Code to compute the max of x and y if ( x > y ) max = x; // x is the maximum else max = y; // y is the maximun 
Switch ( integerexpression ) { case intVal1: statement11 statement12 ... break; case intVal2: statement21 statement22 ... break; ... default: statementD1 // Optional clause !!! statementD2 ... break; } 

while ( condition ) onestatement 
Example: find the first pair of numbers (i, i+1) such that i^{2} + (i+1)^{2} > 100
i = 0; while ( i*i + (i+1)*(i+1) <= 100 ) i++; 
for ( initialization ; terminationcond ; increment ) onestatement 
Example: compute 1^{2} + 2^{2} + ... n^{2}
sum = 0; for ( i = 0; i <= n; i++ ) sum += i*i; 
do onestatement while ( condition ) 
Comment:



int sum = 1; for ( int i = 0; i <= 5; sum = sum + i++ ); System.out.print(sum); 
Solution: (use variables !!!)
initialization: sum = 1 for statement: iteration 1: i = 0 (0 <= 5) sum = 1 + 0 = 1 iteration 2: i = 1 (1 <= 5) sum = 1 + 1 = 2 iteration 3: i = 2 (2 <= 5) sum = 2 + 2 = 4 iteration 4: i = 3 (3 <= 5) sum = 4 + 3 = 7 iteration 5: i = 4 (4 <= 5) sum = 7 + 4 = 11 iteration 6: i = 5 (5 <= 5) sum = 11 + 5 = 16 iteration 6: i = 6 (6 > 5) Stop: sum = 16 System.out.print(sum) will print: 16 