next up previous
Next: equals() Method Up: Conditionals Previous: The Type boolean

if Statements

The full power of boolean expressions hasn't yet been harnessed. So what if we can create expressions that when evaluated give us values of type boolean? Well, the power comes when we combine such expressions with statements like the if statement. Up until now we've largely been interested in allocating memory, reading memory, writing to memory, and a little bit of reading from input devices and writing to output devices. Now we go down a whole different branch of the 'statement tree' -- we look at statements that control the flow of the program.

What does it mean to control the flow? Well, remember what we said a computer is: it's a machine that executes intstructions sequentially, and can read and write from/to memory. How can we tell it which instructions it should execute? Up until now, we've just written a long list of instructions, and depended on the compiler's habit of translating our Java statements into machine code in the same sequence that we gave the instructions. When the program runs, these instructions are just executed sequentially, one after the other. Even when we call a method, what happens is that the computer starts executing the instructions given by the method, and when it finishes it jumps back to executing the statement immediately after the method call.

Wait a minute. Didn't we say that the computer executes sequentially? What's this 'jumping around' business? Well, the computer is still executing instructions sequentially, but the question is, in what order should the computer execute them? Why can't we execute instructions 1 and 2, then execute instructions 8, 9, and 10, go back to execute instructions 5 and 6, and then end, never executing instructions 3,4, and 7? We can, and specifying the order of execution of statements is what we mean by controlling the flow of the program.

So what we need is some Java statements to tell the computer which instruction it should execute next, and the if statement is exactly one of those flow control statements.

The if statement allows us to test a boolean expression, and depending on the value, we will execute one chunk of code and or a different chunk of code.

See Nino and Hosch, Ch. 6 for a discussion of the syntax, but here is a simple example:


if (s1.age >= 40) {
        System.out.println(``You're an old student!'');
}else{
        System.out.println(``You're not so old.  You can be taught new tricks.'');
}

(Of course, newer research shows that age and learning aren't necessarily inversely correlated.....)

In this example, the boolean expression s1.age >= 40 is evaluated. If it is true, then the statement System.out.println(``You're an old student!''); is executed, but not the statement System.out.println(``You're not so old. You can be taught new tricks.'');. If it is false, the opposite happens -- first statement is skipped, and the second statement is executed.

A pictorial version looks like this:

Figure 19: Program flow around/through if statement
\begin{figure}
\begin{center}
\epsfxsize =5in {\epsfbox {ifstatement.ps}} \\
\end{center}\end{figure}

Here the lines on the left indicate the flow of the program, or the sequence of execution of statements. The large semicircles indicate that the corresponding statements of the program are skipped or not executed. Thus an if statement alters the flow of control of the program.


next up previous
Next: equals() Method Up: Conditionals Previous: The Type boolean
Chris Trendall
Copyright ©Chris Trendall, 2001. All rights reserved.

2001-12-09