Chapter 3. Program Control Statements
Overview
The program control statements in Java are as follows:
Selection Statements
if
switch
Iteration Statements
for
while
do-while
Jump Statements
break
continue
return
Selection Statements
The selection statements are also known as decision making statements or branching statements
The if Statement
The conditional expression controlling the if must produce a boolean result.
Nested ifs
Very common usage of using an if statement within an if/else statement block. Note, the an else statement always refers to the nearest if statement that is within the same block. Example:
if-else-if ladder
A common construct in programming is the if-else-if ladder. The conditional expressions are evaluated from the top down. As soon as a true condition is met, the statement block executes and the rest of the ladder is bypassed.
If the ladder has a final else statement, if no true condition is met, this final else block will execute.
The Switch Statement
The second type of selection statement is a switch statement. Similar to creating an if-else-ladder, the switch provides multiway branching allowing a program to select among several alternatives. The switch is typically a more efficient approach than writing ladders.
The value of an expression is tested against a list of constants and executed when a match is found.
Each of the case constants must be unique. Duplicates are not allowed. The default case is optional. The break statement causes the program flow to exit. If it is not specified, all statements at and following the match case will continue to be executed until a break statement is hit.
Nested switch statements are also allowed.
Iteration Statements
Iteration statements create loops in the program that repeat until the condition for termination is met.
The for Loop
The general form of a for loop is:
The initialization sets the initial value of the loop, which acts as the counter. The condition us a Boolean expression that determines if the loop will repeat and the iteration defines the amount by which the loop control variable will change each time the loop is repeated. The for loop will continue to execute while the condition test is true.
A loop iteration control variable can either increment or decrement.
You can also use multiple loop control variables if need be.
The while Loop
In a while loop, the loop repeats while the condition is true.
The general form is:
Note that the while condition is checked at the top of the loop, meaning that the loop code will not execute if the start of the loop is false.
The do-while Loop
Unlike the for and while loop, the do-while loop checks its condition at the bottom of the loop which means that a do-while loop will always perform at least one iteration before checking its condition.
The general form is:
Jump Statements
Jump statements are used to unconditionally transfer program control from one point to elsewhere in the program.
break
To force an immediate exit from a loop, use the break statement. When used, the loop is terminated and the program continues at the next code statement following the loop.
Examples usage:
You can also use a label with break statements, to break from a particular part of a loop into another part of the loop. Due to differing opinions on usage (similar but better than goto), and it's lack of usage in many programs, I won't make further notes on it.
continue
The continue statement can be used to force an early iteration of a loop. This will skip any code between itself and the conditional expression. Where break is used to break out of a loop, continue can be used to skip code block from executing under a certain condition, but not stop the loop entirely.
Example:
return
The return statement is used to break from a loop or method, and sometimes return a value. It is discussed further in the next chapter. Reminder to link to it.
Last updated
Was this helpful?