Conditional statements and loops
Introduction
Every procedural programming language has conditional statements (if-statements) and iterative statements (loops). You should be familiar with the concepts —unless your only previous programming language was a purely functional language. Therefore, our purpose here is to summarize the Java versions of these statements and point you to the places in the text and the CD ProgramLive for longer discussions of these statements.
The block (Gries/Gries, p. 69. ProgramLive, p. 1-4)
The block has the form
{ <sequence of statements and local-variable declarations> }
Its purpose is to aggregate (collect together) the sequence of statements and declarations into a single statement. It used below in writing conditional and iterative statements.
if-statement (Gries/Gries, p. 69. ProgramLive, p. 1-4)
The if-statement has the form
if ( <condition> ) <statement>
The <condition> is a boolean expression. We generally make the <statement> be a block.
The following example shows the conventions used in this course for indenting and the placement of the opening brace of the <statement>.
if (x < y) {
// Swap x and y
int temp= x;
x= y;
y= temp;
}
To execute the if-statement, evaluation the <condition> and, if it is true, execute the <statement>.
if-else statement (Gries/Gries, p. 69. ProgramLive, p. 1-4)
The if-else statement has the form
if ( <condition> ) <statement1> else <statement2>
The <condition> is a boolean expression. We generally make <statement1> and <statement2> be blocks.
The following example shows the conventions used in this course for indenting and the placement of the opening braces of the statements.
// Put the smaller of x and y in z
if (x <= y) {
z= x;
} else {
z= y;
}
or
// Put the smaller of x and y in z
if (x <= y) {
z= x;
}
else {
z= y;
}
To execute the if-else statement, evaluation the <condition>; if it is true, execute <statement1>; if it is false, execute <statement2>.
while-loop (Gries/Gries, p. 233. ProgramLive, p. 7-1)
The while-loop has the form
while ( <condition> ) <repetend>
where the <condition> is a boolean expression and the <repetend> is any statement. (Repetend means the thing to be repeated.)
We generally make the <repetend> be a block.
The following example shows the conventions used in this course for indenting and the placement of the opening brace of the <repetend>.
// Precondition: n >= 1
// Set x to the largest power of 2 that is at most n
x= 1;
// invariant: x is a power of 2 and x <= n
while (x <= n) {
x= 2 * x;
}
To execute the while-loop, do the following:
- Evaluate the <condition>; if it is false, terminate execution of the loop.
- Execute the <repetend>; then, repeat from step 1.
The first execution of the <repetend> is called iteration 0; the second, iteration 1; and so on.
for-loop (Gries/Gries, p.78 (a quick introduction) and p. 253. ProgramLive, p. 7-4)
We don't describe everything about the for-loop but just its use with a single "control variable" that is initialized at the beginning and incremented every iteration of the for-loop. (An iteration is one execution of the repetend —see below.) The for-loop has the form
for ( <initialization> ; <condition> ; <increment> ) <repetend>
where the <initialization> is an assignment or an initializing declaration, the <condition> is a boolean expression, the <increment> is an assignment, and the <repetend> is any statement. (Repetend means the thing to be repeated.)
We generally make the <repetend> be a block.
The following example shows the conventions used in this course for indenting and the placement of the opening brace of the <repetend>. Note that a range m..m has one value, m. But the range m..m-1 has no values and thus has the sum 0. Finally, if m= n+1 in this example, then 0 iterations (executions of the repetend) of the loop are performed.
// Precondition: m <= n+1
// Set x to the sum of the value in the range m..n
x= 0;
// invariant: x is the sum of the values in the range m..k-1
for (int k= m; k <= n; k= k+1) {
x= x+k;
}
Execution of the for-loop is equivalent to execution of the following translation into a while-loop, with one exception. If the initialization is an initializing declaration, the scope of the control variable is the for-loop itself, and the control variable cannot be referenced in statements after the loop. But in the while-loop version, the scope includes the statements following the loop.
<initialization> ;
while ( <condition> ) {
<repetend>
<increment>
}