ABSTRACT

The marvellous ‘if ’ statement Because it is almost impossible to write any program without making a decision, you have already seen the ‘if ’ statement in action. The introductory chapter used the ‘if ’ statement and so too did the last chapter. You probably had a good idea what was going on, but you were not properly introduced. The marvellous ‘if ’ statement can be used in one of three different ways. The simplest way is like this:

if (condition){ //Do something if condition is true }

The line starts using ‘if ’, and then in brackets we put a condition. This can be anything that evaluates to true or false. You can even put the keyword true in there and then the ‘if ’ statement always evaluates to true, so the code in the curly brackets is always executed. Later in this chapter we will look at the different conditions you can use.