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 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 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.

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 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.

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.




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.