Modern Java - Writing Code
Written by Mike James   
Article Index
Modern Java - Writing Code
If Else

Arithmetic Expressions

The second idea is slightly more complicated. You can use a variable on both sides of an assignment. For example,


this retrieves the value stored in b and stores it in a. After this instruction a and b store the same value. (Again it is important not to read this as "a equals b" but "store the contents of b in a" - its a command not a question.)

You can also write things like:


and the right hand side, an arithmetic expression, will be worked out and stored in a. Notice you can use variables on the right hand side and their values will be retrieved and used in the calculation. 

You can generally write arithmetic expressions as you would normally but notice that the multiplication sign is "*" an asterisk and the division sign is /. Also feel free to use brackets to make sure things are worked out in the correct order. More on arithmetic and other types of expression later.

An expression can be thought of as a mini-program. It is a recipe for how to combine values stored in variables and constants to produce a new value. 

An arithmetic expression that causes a lot of problems when you first see it is:


Which if you read it as "a equals a plus 1" sounds like nonsense. What is does is to increase the value stored in a by one - i.e. its increments the value in a. If you read it as "retrieve the value in a, add one to it and store the result back in a" - then all should be clear.

Adding one to a variable is so common that Java has a special command for it:


is the same as a=a+1;

There are lots of different types of data that can be stored in a variable but for the moment integer variables will serve well as an example.

Advanced Variables

Variables are a little more complicated than this first introduction might suggest. When you first meet the idea a variable is often likened to a box with a label on it. The box can be used to store a value and the label is the variable's name. For example:

int a=1;

brings to mind a box labeled a that you store the value 1 in. This is a very reasonable first approximation to what a variable looks like and as long as you restrict your attention to simple values like numbers or characters there is no problem. However Java and most modern languages are object oriented and this means that things that you can store "in" variables include objects. You can still think of storing an object in a variable as if it was a box with a label but it quickly becomes increasingly difficult to make this model fit with reality.

A more accurate idea of a variable is that it is a reference to an object. That is the important part of a variable is its name and not the storing of things. We really don't care where the value 1 is stored as long as when you use the name "a" the value is retrieved. It is better to think of a as "pointing at" or referencing the location where the value 1 is stored. At first this makes no real difference to your understanding but later it does. For example when you start to work with objects it is much more accurate to think that a is a reference to an object than the object is stored in a - as it usually isn't. The advantage of this is that it makes it natural to think of two variables, a and b say, referencing the same object whereas it is much harder to think of two variables containing or storing the same object.

A variable isn't a container it is a reference to where something is stored.

The flow of control

Now we have something to write instructions about lets see what sort of things we can do.

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

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