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

Advanced note - Recursion

Many programmers believe that there is one other primitive flow of control ... recursion - and hence we have left one possible form out. This is also the reason that the topic is often introduced far to early in a programmer's education. Recursion isn't needed and beginners are better off leaving it until later. 

The flow of  control graph corresponding to recursion is a nested set of loops of arbitrary depth. Such a control structure can be built using the basic loops and conditionals and hence recursion can be eliminated. In this sense it certainly isn't an essential fourth control form - but it is incredibly useful and powerful but this is not the place to talk about recursion.

Compound statements

Java like many modern languages uses the concept of a compound statement to simplify the way that instructions can be put together.

The most basic unit of a Java program is a single statement and every Java statement ends with a semicolon. So for example:

 a=1+2;
 b=3*4;
 c=5-4;

and so on..

What exactly the instructions do doesn't really matter - we have three Java statements which will be obeyed one after the other. This is the default flow of control and it is what you expect when you simply write a list of statements.

It corresponds to the first of the flow of control graphs - i.e. the straight line.

You don't have to write each statement on a separate line. That is

a=1+2; b=3*4; c=5-4;

is exactly equivalent to the first list. It may be equivalent but most Java programmers would consider it bad style - one instruction per line is the norm.

This said anything that makes your program easier to understand is good and there are times when grouping instructions on a single line improves the clarity of the program.

For example, if x and y are co-ordinates then it is sometime better to set both x and y in a single line instruction:

x=0;y=0;

but even in this case you will encounter arguments that one per line or alternative forms of expression are better.

Now we come to an idea that is incorporated into most modern programming languages to a greater or lesser extent. The idea of a compound instruction. Any list of instructions enclosed in curly brackets is treated as a single instruction. For example:

{
 a=1+2;
 b=3*4;
 c=5-4;
}

or

{ a=1+2; b=3*4; c=5-4;}

are exactly the same as the previous examples in that there are three instruction which are carried out one after another but as far as Java is concerned they are a single statement.

As far as Java style is concerned the first form is preferred.

The power of a compound statement is that you can use one anywhere you can use a single instruction.

There is more to the compound statement than just grouping instructions but more of this later. 

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.

Java supports the following comparison operators, most of which are obvious:

  • Less than: a < b

  • Less than or equal to: a <= b

  • Greater than: a > b

  • Greater than or equal to: a >= b

  • Equal to a == b

  • Not Equal to: a != b

Notice the use of ! to mean "not" in not equal. This is fairly standard. The important thing about the comparison operators is that they compare two values and evaluate to either true or false.

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:

if(condition){
 list of statements
}

For example:

if(count==0){
 a=1+2;
 b=3*4;
 c=5-4;
}

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){
 total=0;
}

and not

if (count==0) total=0;

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

 

<ASIN:0072263148>

<ASIN:0132354764>

<ASIN:1849511764>