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


The flow of control

If you look at any list of instructions, i.e. any program in the widest possible sense, then there are only a few very basic ways that the instructions can be organised.

There are only three basic variations on what you can do with a list of instructions even in a natural language.

  • You can write a one-after-the-other list.

  • You can write a conditional list for example,
    "if it is raining then pick up the umbrella"

  • You can specify a repetition, for example,
    keep stirring you coffee until the sugar dissolves.

These three are the only three possible forms that a list of instructions can take.

It can be proved that if you have these three forms then you can write any program that can exist. 

A language that has these three things is said to be Turing complete - after the mathematican and early computer pioneer Alan Turing. You might be able to guess that Java is indeed Turing complete and has all three forms.

This observation was and is the basis of the structured programming philosophy that aims to restrict programming and programmers to just these three fundamental forms clearly expressed. While you can take other more complicated instructions these are the "programming atoms" from which all other programs can be constructed.

If you have a list of instructions in front of you then you can follow the order of execution of the instructions or the "flow of control" simply by tracing your finger along the instructions in the order that they are carried out. The resulting shape is called the flow of control graph and it is fundamental to programming.

If you understand how to build a flow of control graph that does a particular job then you are a programmer and if you don't you aren't.

The three fundamental flow of control forms correspond to a straight line segment following the one after the other order of instructions, a branching structure that divides between alternative lists of instructions and a circular looping shape that indicates the repetition of instructions.




This may all seem very trivial but back in the early days of programming this wasn't known and it was difficult to work out what sorts of statements a computer language needed to make it work.

The most primitive languages use a command something like Goto x  where x is an instruction in the list. The Goto is versatile in that you can use it to create each of the three standard flow of control graphs.

Java doesn't have a Goto or anything like it and neither  does any other modern language. The reason is that Goto is simply too much freedom and in this case too much freedom leads to a mess.

Put simply Goto can be misused and it usually is.

In place of the Goto modern language offer a number of constructs for building flow of control graphs - let's look at what Java provides.

Advanced note

Many programmers believe that there is one other primitive flow of control ... recursion. 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. 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:


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:


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;}

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.

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.





Last Updated ( Monday, 17 October 2016 )

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