Return to index

 

Flow Control

 

Programs are much more than the simple sequential execution of statements.  The defining and calling of methods is one way that we can avoid programs that are just one very long list of statements (the separation of code and data into separate, reusable classes is another). After a method call, execution continues at the point in the calling code just after the call was made.

However, at the lower level, there are two flow control techniques that a programming language should have.

1. We often need to excute a section of code more than once and a way to do this easily is a great benefit. This can be referred to as 'looping'.

2. We also commonly need to execute different sections of code depending upon whether some condition is true or false. This is an essential feature and can be referred to as 'branching'.

 

Looping

The most basic and flexible loop in Java is defined with the keyword 'while' and takes this form:

while (condition) {

...

}

The condition is an expression that evaluates to either true or false (a 'boolean' value). Any statements between the curly parentheses will be executed repeatedly while the condition evaluates to true.

There is an alternative 'do-while' form:

do {

...

} while (condition);

Using this form of while loop means that the statements inside the loop will definitely be performed at least once because the condition is not checked until after each loop. In most cases, the first form is preferable.

Consider the following code snippet:

turtle.gotoXY(100+25, 100);

drawSquare(20);

turtle.gotoXY(100+50, 100);

drawSquare(20);

turtle.gotoXY(100+75, 100);

drawSquare(20);

This draws 3 squares in a horizontal row with a space of 5 pixels between the squares. It works fine but is clearly a bit repetitive. What if we required to draw 100 squares? There is an obvious need to employ a loop here.

Here is the same code this time written using a while loop:

int loop=1;

while (loop<=3) {

turtle.gotoXY(100+loop*25, 100);

drawSquare(20);

loop=loop+1;

}

A more specialised type of loop, commonly used, is the 'for' loop. This takes the form:

for (initialisation; condition; increment/decrement) {

...

}

An example will make its use clearer. Let's do the same task as we did with a while loop, but this time using a for loop.

for (int loop=1; loop<=3; loop=loop+1) {

turtle.gotoXY(100+loop*25, 100);

drawSquare(20);

}

The condition (loop<=3) is checked before each loop. If the condion is true, the statements inside the loop are executed. After each loop, the increment/decrement is performed and the condition checked again. Any loop can be written as a while loop but when a variable is used as a counter, as in our example, a for loop is a little more compact.

In either case, it is now very easy to change how many squares are drawn.

NOTE: An important point to remember when writing a loop in your code is - make sure that the loop will definitely end and not repeat forever! Often when a program seems to 'freeze' or 'crash' it is because it gets stuck in a never-ending loop.

 

Branching

Often you want to execute different code depending on some condition within the program.  Java has several constructs for doing this.  We will look at two most commonly used : the if statement and the if / else statement.

The basic form of the if statement is:

if (condition) {

...

}

 

The basic form of the if-else statement is:

if (condition) {

...

} else {

...

}

 

Other forms of conditional branching, such as the 'case' statement, is covered later in TJI's Java Guide, when we return to the subject of flow control in more detail.

 

Return to index