Writing Java Code - Methods
Writing Java Code - Methods
Written by Ian Elliot   
Thursday, 20 January 2011
Article Index
Writing Java Code - Methods
The flow of control
Conditionals - if
Try it out


The If statement

Now we come to the second form of flow of control - the conditional.

In this case we only want to carry out some instructions if a condition is true. Of course this is jumping the gun a little because we haven't looked at what a condition is and how it is worked out but we have to start somewhere.

When you write an if statement it looks something like:

if(condition) instruction;

and the single instruction is only obeyed if the condition is true.

For example:

if (count==0) total=0;

which is read "if count equals zero then store zero in total".

Notice that to distinguish between the two meaning of "equals" we write "==" i.e. a double equals sign, to mean a test of equality and a single equals to mean assignment, that is assign zero to total. However you don't need to follow the details to understand that the instruction total=0 will only be carried out if the condition count==0 is true.

Having introduced the most basic form of the if statement you should now know that good Java style dictates that we never use it!

The best form of an if statement always uses a compound statement and formatted as below:

 list of statements

For example:


This says "if count equals zero then carry out the compound statement".

This is fine but good style dictates that you should use a compound statement even if it contains only one instruction. That is you should write:

if (count==0){

and not

if (count==0) total=0;

Both are legal and reasonable even but the first is less likely to cause an error.

if else

The if statement is the basic conditional and in theory we don't need any other sort of conditional - but in practice it is easier to have some flexibility in the language.

The if statement determines if a block of instructions will be carried out or not. The if..else statement selects one of two blocks of instructions.

For example:

if (condition) {
 instructions 1
} else {
 instructions 2

and if the condition is true then instructions 1 are carried out and if it is false instructions 2 are carried out.

Notice that in any situation only one of the blocks is carried out  - the condition has to be either true or false and hence either the first or the second block of instructions is carried out.  Beginners sometimes think that because the second set of instructions are below the first they are carried out by default - they aren't.

In most cases the if else which selects between two alternatives is enough but there are other Java statements that will select in fairly complicated ways between multiple blocks of instructions.

If you want to know more about these look up "else if" and the "switch" statement.  We will return to these more sophisticated control statements later.

The for loop

If you want to do an instruction three times you could achieve that simply by writing it three times:


However this isn't a clever way to work and it quickly becomes increasingly difficult as the number of repeats goes up.

To allow you to repeat an action a set number of times most modern languages provide a for loop - the reason for the name will become apparent very soon.

Most language also use a particular form of the for loop that was introduced by an earlier language C. The only problem is that the C style for loop is very flexible but it can seem complex to the beginner.

It is easier to look at an example before seeing the general form:

for(int counter=1; counter<11;counter++){
instructions to repeat;

When the for loop is obeyed the counter variable is first set to 1 as specified by the first part of the loop:

for(int counter=1; counter<11;counter++)

Then the test specified:

for(int counter=1; counter<11;counter++)

is evaluated. If it is true the "body" of the loop is obeyed. If it is false then the loop comes to an end and if this is the first time the for loop has been encountered the body of the loop is skipped.

If the condition is true then then the instructions in the loop are carried out. When these are completed the counter has one added two it as specified by the final part of the for:

for(int counter=1; counter<11;counter++)

Recall that the ++ operator is the increment operator and simply adds one to the value stored in counter. Then the condition counter<11 is evaluated again and if it is true the loop repeats i.e. the instructions in the body are carried out all over again but this time with counter storing the value 11. If it is false then the loop is complete.

If you think about this mechanism you should be able to see that this will cause the instructions to be repeated for values of "counter" starting at 1 and ending at 10.

Why is 10 the last value?

Simply because when you add one to ten you get 11 and 11 is not smaller than 11 and so counter<11 is false and the loop ends.




Last Updated ( Saturday, 21 May 2016 )

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