Unit 1 Lesson 7 - Loops

Lesson summary and assignments

A loop is a control structure which executes a statement or a code block repeatedly until its controlling boolean expression evaluates to true. The statement or code block a loop must modify the conditions of the controlling boolean expression in such a way that the controlling boolean expression will at some point be false, otherwise the loop, once entered, will be an infinite loop.

A code block is a section of code enclosed within an open and a closing curly brace.

There are four types of loops in Java:

while Loop

The contolling boolean expression of a while loop is located at the beginning of the loop. Thus, if the controlling expression is false on first entering the while loop, then the statement or code block of the while loop is never executed.

        while (expression) statement;
                            

or

        while (expression) 
        {
            statement(s)
        }
                            

Examples:

        int x=0;
        while (x < 10)  System.out.println(x++);
                            

The above while loop with a single statement body prints the numbers 0 through 9. The following is an equavalent while loop using a code block.

        int x = 0;
        while(x < 10)
        {
            System.out.println(x);
            x += 1;
        }
                            
do-while loop

The contolling boolean expression of a do-while loop is located at the beginning of the loop after the loop's statement or code block. Thus, even if the controlling expression is false on first entering the while loop, the statement or code block of the while loop will be executed one time prior to arriving to controlling expression.

        do statement; while (expression);
                            

or

        do  
        {
            statement(s)
        } while (expression);
                            

Examples:

        int x = 0;
        do System.out.println(x++); while(x < 10);
                            

The above do-while loop with a single statement body prints the numbers 0 through 9. The following is an equavalent do-while loop using a code block.

            int x = 0;
            do
            {
                System.out.println(x);
                x += 1;
            } while(x < 10);
                            

Note that with these two do-while loops, even if x had been initialized with a number > 10 such as 50, the loop would have executed the code one time, with the the value 50 being printed.

for-loop

Note that in the while loop example above, a variable is initialized. That variable is in the controlling boolean expression. Then right at the end of the loop, that variable is updated just before evaluating the controlling boolean expression again. This pattern is so common that java has a short-hand version of this pattern: the for-loop. The syntax is as follows:

            for(initialization-code; boolean expression; code-executed-at-end-of-each-loop)
            statement; or code-block
                        

For example

            for(int x = 0; x < 10; x=x+1) System.out.println(x);
                        

... or using a code-block

            for(int x = 0; x < 10; x=x+1)
            {
                System.out.println(x);
            }
                            

Both of these for-loops are equivalent to the following while loop.

            {
                int x = 0;
                while(x < 10)
                {
                    System.out.println(x);
                    x = x+1;
                }
            }
                            

Note that the entire equavalent while-loop code is contained within a pair of curly braces which limits the scope of the variable x to the loop. The variables declared within the for-loop are only visible (in-scope) within the for loop and cannot be used by code after the for loop.

Also note that the first part of the for loop (before the fist semicolon) is for code to initialize the loop. This section of the for-loop generally declares and initializes the loop-control-variable (see code.org unit 3 lesson 4). The last part of the loop (after the second semicolon) is for code to be executed at the end of each loop. What do you think the following atypical for-loop will print?

            for(int x=0, y=10; x < 10; x++, y--)
            {
                System.out.println(x + "  " + y);
            }
                            
Enhanced for-loop (for-each loop)
The enhanced for-loop, known is some languages as the for-each loop, will be described later in Unit 3 Lesson 7.

Vocabulary

algorithm
a finite set of instructions that accomplish a task
condition
determines whether or not to execute a block of code
iteration statement
a control structure that repeatedly executes a block of code