The if Statement

In this lesson, we’ll explore a statement that alters the sequential execution of the statements in a Java program.

Flow of control

The first Java statement that executes in an application program is the first one in the method main. Subsequent statements execute sequentially unless a statement alters this order. The order in which statements in a Java program execute is called the flow of control. If a statement contains a call to another method, control is given to that method, as shown in the figure given below. Eventually, that method returns control to the method that called it. Execution ends when and if the closing brace in main is reached. (Realize that other ways of ending program execution are also possible.)

In all of the examples we have seen so far, the statements in a program execute in the same order each time the program is run. We can alter this order during the execution of the program, however, by using one of several kinds of decision statements. The if statement and the if-else statement offer two courses of action according to the value of a certain kind of expression. We’ll discuss the if statement in this lesson, and cover the if-else statement in another lesson in this ...