M/F 2:30-3:20   
in G01 Gates Hall

CS 1130: Transition to OO Programming

Spring 2016

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.

  1. The block.
  2. if-statement.
  3. if-else statement.
  4. while-loop.
  5. for-loop.

The Block

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.

For more information, see Gries/Gries, p. 69, and ProgramLive, p. 1-4


if-statement

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>.

For more information, see Gries/Gries, p. 69, and ProgramLive, p. 1-4.


if-else statement

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>.

For more information, see Gries/Gries, p. 69 and ProgramLive, p. 1-4


while-loop

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:

  1. Evaluate the <condition>; if it is false, terminate execution of the loop.
  2. 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 more information, see Gries/Gries, p. 233, and ProgramLive, p. 7-1.


for-loop

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>
}

For more information, see Gries/Gries, p.78 (a quick introduction) and p. 253, as well as ProgramLive, p. 7-4.