Modern Java - Writing Code
Written by Mike James   
Article Index
Modern Java - Writing Code
Expressions
Conditionals
If Else
While
Project

Do While

There is a variant on the while loop that is worth knowing about - the do-while loop.

To see why a variant is needed consider what the minimum number of times a while loop can be carried out.

For example:

a=10
while(a<10){
 instruction;
}

In this case because a isn't less than 10 when the loop starts the body of the loop isn't carried out at all i.e. it is skipped.

Compare this to the

a=10;
do{
 instruction;
} while(a<10);

In this case the test that ends the loop comes at the end of the loop. What this means is that the instructions in the loop will be obeyed at least once.

This is the only difference between the while and the do while loops - the test is either at the start or the end of the loop respectively.

This in turn means that the while loop can skip over the loop and so the minimum number of repeats is zero but the do while loop has to execute at least one repeat.

You will quickly learn which form of the loop you need even if it seems a bit confusing at first. If you need to do something once before the condition can be tested than you need a do while otherwise you probably need a while. 

Some Examples

Flow of control is the difficult part of learning to program. You now should understand the ideas that are behind it but you might be unsure how to use them. The only cure for this is to practice. There is nothing more to know but you need to put theory into practice. It is a bit like just learning where the notes are on a piano - you next need to practice some scales before moving onto a sonata. 

So we need some examples. 

At this stage it isn't easy to invent realistic examples of how to use control statements without introducing a lot more Java.

However to close without seeing some real code in action isn't satisfying. So lets write a simple program that demonstrates the if, if else, for, while and do while loop.

These aren't realistic or convincing but they are simple.

Enter the code or download the program from the CodeBin and try it out. Modify it and see if you are happy with the result. If you are a complete beginner to programming absorbing the way that control statements work is hard but once you have done it learning the rest of programming is easy. 

largecover

The Demo project

Start a new Java project and add a JFrame as described in Chapter 1. If you don't know how to do this go back and look at the example program in Chapter 1.

Now switch to the Designer and place a button on the form. Double click the button an you will be transferred to the Code editor ready to type instructions into the button's click event handler. What we are going to do is enter a single example of each control structure into the button's event handler so that the commands are executed when you click the button.

All of the code listed below is to be entered into the button's click event handler.

If

First we look at a simple if statement:

int total=100;
if(total==100){
 System.out.println(total);
}

The command println stands for "print line" and it will print the value of the integer variable total in the Output window which you can see in NetBeans' default layout just below the source listing.

If you type in and run the above what do you expect to see?

Answer: you should see 100 printed in the Output window. Now change total to be 99. Now you should see nothing at all printed.

If Else

An if then else is fairly easy:

if(total==100){
 System.out.println("value is 100");
}else{
 System.out.println("value is not 100");
}

When you run this you will see "value is 100" printed.

What do you see if you change the value of total to 99?

You will see just the message "value is not 100".

Notice you only ever see one of the two messages.

For

In the case of the for loop:

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

what do you expect to see printed?

Be exact.

The answer is 1 to 10 because each time thought the loop i increases by 1 until it reaches 11 when the loop ends without executing the body of the loop again i.e. with the value of i set to 11.

As an aside there is a long history of using i,j and k as counters in loops - the reason is that i is the first letter of integer and a counter or index in a for loop is always an integer. It was a convention started with the language Fortran. Today programmers still tend to use i,j and k as loop counters but it would be better to use a meaningful name.