Control Flow Statements
The If-Then Statement
The if-then
statement is the most basic of all the control flow statements. It tells your program to execute a certain section of code only if a particular test evaluates to true
. For example, the Bicycle
class could allow the brakes to decrease the bicycle's speed only if the bicycle is already in motion. One possible implementation of the applyBrakes()
method could be as follows:
If this test evaluates to false
(meaning that the bicycle is not in motion), control jumps to the end of the if-then
statement.
In addition, the opening and closing braces are optional, provided that the "then" clause contains only one statement:
Deciding when to omit the braces is a matter of personal taste. Omitting them can make the code more brittle. If a second statement is later added to the "then" clause, a common mistake would be forgetting to add the newly required braces. The compiler cannot catch this sort of error; you will just get the wrong results.
The If-Then-Else Statement
The if-then-else
statement provides a secondary path of execution when an "if" clause evaluates to false
. You could use an if-then-else
statement in the applyBrakes()
method to take some action if the brakes are applied when the bicycle is not in motion. In this case, the action is to simply print an error message stating that the bicycle has already stopped.
The following program, IfElseDemo
, assigns a grade based on the value of a test score: an A for a score of 90% or above, a B for a score of 80% or above, and so on.
The output from the program is:
You may have noticed that the value of testscore
can satisfy more than one expression in the compound statement: 76 >= 70
and 76 >= 60
. However, once a condition is satisfied, the appropriate statements are executed (grade = 'C';
) and the remaining conditions are not evaluated.
The While and Do-while Statements
The while
statement continually executes a block of statements while a particular condition is true
. Its syntax can be expressed as:
The while
statement evaluates expression, which must return a boolean
value. If the expression evaluates to true
, the while
statement executes the statement(s)
in the while block. The while
statement continues testing the expression and executing its block until the expression evaluates to false
. Using the while
statement to print the values from 1 through 10 can be accomplished as in the following WhileDemo
program:
You can implement an infinite loop using the while
statement as follows:
The Java programming language also provides a do-while
statement, which can be expressed as follows:
The difference between do-while
and while
is that do-while
evaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within the do
block are always executed at least once, as shown in the following DoWhileDemo
program:
The For Statement
The for
statement provides a compact way to iterate over a range of values. Programmers often refer to it as the "for loop" because of the way in which it repeatedly loops until a particular condition is satisfied. The general form of the for
statement can be expressed as follows:
When using this version of the for statement, keep in mind that:
- The initialization expression initializes the loop; it is executed once, as the loop begins.
- When the termination expression evaluates to
false
, the loop terminates. - The increment expression is invoked after each iteration through the loop; it is perfectly acceptable for this expression to increment or decrement a value.
The following program, ForDemo
, uses the general form of the for
statement to print the numbers 1 through 10 to standard output:
The output of this program is:
Notice how the code declares a variable within the initialization expression. The scope of this variable extends from its declaration to the end of the block governed by the for
statement, so it can be used in the termination and increment expressions as well. If the variable that controls a for
statement is not needed outside of the loop, it is best to declare the variable in the initialization expression. The names i
, j
, and k
are often used to control for
loops; declaring them within the initialization expression limits their life span and reduces errors.
The three expressions of the for
loop are optional; an infinite loop can be created as follows:
The for
statement also has another form designed for iteration through Collections and arrays. This form is sometimes referred to as the enhanced for statement, and can be used to make your loops more compact and easy to read. To demonstrate, consider the following array, which holds the numbers 1 through 10:
The following program, EnhancedForDemo
, uses the enhanced for to loop through the array:
In this example, the variable item
holds the current value from the numbers array. The output from this program is the same as before:
We recommend using this form of the for
statement instead of the general form whenever possible.
The Break Statement
The break
statement has two forms: labeled and unlabeled. You saw the unlabeled form in the previous discussion of the switch
statement. You can also use an unlabeled break
to terminate a for
, while
, or do-while
loop, as shown in the following BreakDemo
program:
This program searches for the number 12 in an array. The break
statement, terminates the for
loop when that value is found. Control flow then transfers to the statement after the for
loop. This program's output is:
An unlabeled break
statement terminates the innermost switch
, for
, while
, or do-while
statement, but a labeled break
terminates an outer statement. The following program, BreakWithLabelDemo
, is similar to the previous program, but uses nested for
loops to search for a value in a two-dimensional array. When the value is found, a labeled break
terminates the outer for
loop (labeled "search"):
This is the output of the program.
The break
statement terminates the labeled statement; it does not transfer the flow of control to the label. Control flow is transferred to the statement immediately following the labeled (terminated) statement.
The Continue Statement
The continue
statement skips the current iteration of a for
, while
, or do-while
loop. The unlabeled form skips to the end of the innermost loop's body and evaluates the boolean expression that controls the loop. The following program, ContinueDemo
, steps through a String
, counting the occurrences of the letter p
. If the current character is not a p
, the continue
statement skips the rest of the loop and proceeds to the next character. If it is a p
, the program increments the letter count.
Here is the output of this program:
To see this effect more clearly, try removing the continue
statement and recompiling. When you run the program again, the count will be wrong, saying that it found 35 p
's instead of 9.
A labeled continue
statement skips the current iteration of an outer loop marked with the given label. The following example program, ContinueWithLabelDemo
, uses nested loops to search for a substring within another string. Two nested loops are required: one to iterate over the substring and one to iterate over the string being searched. The following program, ContinueWithLabelDemo
, uses the labeled test
of continue
to skip an iteration in the outer loop.
Here is the output from this program.
The Return Statement
The next branching statements is the return
statement. The return
statement exits from the current method, and control flow returns to where the method was invoked. The return
statement has two forms: one that returns a value, and one that does not. To return a value, simply put the value (or an expression that calculates the value) after the return
keyword.
The data type of the returned value must match the type of the method's declared return
value. When a method is declared void
, use the form of return
that doesn't return a value.
The Classes and Objects section will cover everything you need to know about writing methods.
The Yield Statement
The last branching statement is the yield
statement. The yield
statement exits from the current switch
expression it is in. A yield
statement is always followed by an expression that must produce a value. This expression must not be void
. The value of this expression is the value produced by the enclosing switch
expression.
Here is an example of a yield
statement.
Last update: September 22, 2021