Control Structures

if and else

If all you could do was assign values to things and evaluate expressions, programming wouldn't be very powerful. Much of the power of progamming comes from the ability to have the program make decisions based on the values of variables. The if keyword allows you to execute a statement only if some expression (the condition) evaluates to true. The condition must be an expression that evaluates to a boolean value. The syntax is as follows:

if ( condition ) statement;

For example:

if ( boolean1 && boolean2 ) i = 5;

Anywhere a statement can go, a group of statements within curly braces can be substituted. This is called a block. So, we can write:

if ( someBoolean )
  i = 5;
  j = 6 + k;
  a = true;

If we want to do something else when the condition is not true, we can use the else keyword as follows:

if ( condition ) statement1;
             else statement2;

The same rules for replacing statements with blocks apply here as everywhere. So we can write:

if ( someBoolean ) 
  i = 5;
  j = 6 + k;
  a = true;
  i = 7;
  j = 0;
  a = false;

Since anything is valid in the else block, you can test for more than two different possibilties by putting more ifs in the else block like so:

if ( a ) { ... } 
else if ( b ) { ... }
else if ( c ) { ... }
else { ... }


The while keyword allows you to create loops - blocks of code that are repeated while some condition is still true. The syntax looks like the following:

while (expression) statement;

When this is first executed the expression is evaluated. It must evaluate to a boolean value. If it evaluates to true, the statement is executed. After the statement completes, the expression is evaluated again and the statement is executed again if the expression is still true, and the expression is evaluated again, etc. For instance, to print the numbers from 1 to 10:

int i = 1;
while (i <= 10)

There is a variant of the while loop, called a do/while loop, which executes the block once before testing the conditional expression. The syntax:

do { ... } while ( expression );


The other loop construct is the for loop. The for loop is like a while loop with a special initialization and increment elements added. The syntax is like this:

for ( initialization; condition; increment ) statement;

The initialization, condition and increment are full statements. Initialization is done once before the loop is ever run. The condition is an expression that is evaluated before each execution of the statement (the loop), the statement being executed only if the condition is true. The increment is a statement that is executed after the statement. The order is thus initialization, condition evaluation, statement execution, increment execution, condition evaluation, statement execution, increment execution, etc. until the condition evaluates to false. To count to ten using a for loop (the more natural way to do it):

int i;
for ( i = 1; i<=10; i++ ) System.out.println(i);

Or, even shorter:

for (int i = 1; i<=10; i++ ) System.out.println(i);

The only difference is that the second way, the variable i only exists for the duration of the for loop. We say that it has the scope of the for loop. More on scope later.

break and continue

There are two keywords that allow you to leave the block of a loop without completing it. The break statement will exit the loop entirely, while the continue statement will restart the loop, executing the increment if in a for loop and re-evaluating the condition in both for and while loops. The following code prints all numbers between 1 and 100 that are not multiples of 3 or 4 (in a contrived way to demonstrate both keywords):

int i = 0;
while (true)
  if ( i > 100 ) break; 
  if ( i%3 == 0 || i%4 == 0 ) continue;


The switch construct exists to execute one of various alternates depending on the value of a single integer. The syntax is as follows:

switch ( expression )
  case integer1: statements...;  break;
  case integer2: statements...;  break;
  default: statements...;  break;

The statements can be any number of statements, all ending with semicolons. If you leave out the break, the statements of the next cases will be automatically executed until a break is reached. If the evaluted expression is equal to none of the cases, the default statements are executed.

scope and blocks

A block is a group of statements inside of curly braces, like the loop of a while or for loop. Any variables declared within this block only exist until the end of the block. They are said to have the scope of the block. Blocks that are inside of larger blocks can see the variables declared in the larger blocks, but variables declared in the inside blocks do not persist into the outside blocks. Consider the following:

int i = 5;
double pi = 3.14159;
for (int j = 0; j < 10; j++)
  // here i,d,and j are defined as we enter the loop
  int k = 546;  // will only exist for the life of the for loop
  i = k;
// now only i and pi exist.  i keeps the value it was set to, so
System.out.println(i);  // prints 546

Had we tried to use k outside the loop, we would have gotten an error when we tried to compile the code.