Modern Java - Writing Code
Modern Java - Writing Code
Written by Ian Elliot   
Thursday, 09 June 2016
Article Index
Modern Java - Writing Code
The flow of control
Conditionals - if

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:


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

} 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 programing is easy. 

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.


First we look at a simple if statement:

int total=100;

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:

 System.out.println("value is 100");
 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.


In the case of the for loop:

for(int i=1;i<=10;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. 


A good simple example of a while loop is perhaps the most difficult to find. One example is to just write a loop that counts just like a for loop.

For example, the following is the exact equivalent of the for loop example i.e. it prints 1 to 10:

int j=1;

This is the reason the for loop isn't really needed. Anything you can do with a for loop you can do with a while loop - but the for loop is usually easier.

Consider now a real situation where only the while loop will do - even if it is contrived.

How many times can a number be divided by two before it is less than or equal to one?

We can simulate this by writing a while loop that divides the given number and tests for it being greater than one:

int number=10;

This is should be easy enough for you to follow though but notice that the statement number=number/2 can only be understood if you don't read the equals sign as a statement that the two sides are equal.

The statement says take the contents of number, divide it by 2 and store the result back in the variable number. If you run the program you will see it print 5 and 2 as after the 2 is printed the value is divided by 2 again with the result 1 and the loop ends.

You can do the same job with a do-while loop:


The difference is that now the test is done at the end of the loop.

It is arguable that in this case the test being at the end makes it easier to see why and when the loop comes to an end. However the real difference between the two is that is you set number to one and run each loop the while loop prints nothing at all but the do loop prints one - its all a matter of where the test to end the loop is performed.

Also notice the way indents are used to show where blocks of code start and end. The content of an if, else, for, while and do while should be indented.

If you use NetBeans then you will find a handy auto format command. Simply right click anywhere in the code and select Format from the menu that appears. 






Last Updated ( Monday, 17 October 2016 )

RSS feed of all content
I Programmer - full contents
Copyright © 2018 All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.