1. For Loops

1.1. Objectives

  • Identify the three parts of a for loop
  • Initialize a for loop
  • Set conditions in a for loop
  • Create statements for changing the state of the for loop

1.2. Time Goal

  • 20 minutes on this section

1.3. Key Terms

1.4. Exercises

Note

A for loop is usually used when you know how many times you want the loop to execute. A for loop has 3 parts: initialization, condition, and update. The parts are separated by semicolons (;).

Each of the three parts of a for loop declaration is optional (initialization, condition, and update), but the semicolons are not optional.

for (initialization; condition; update)

Note

One of the strange things about a for loop is that the code doesn’t actually execute where you see it in the declaration. The code in the initialization area is executed only one time before the loop begins, the condition is checked each time through the loop and the loop continues as long as the condition is true, at the end of each execution of the body of the loop the updates are done. When the loop condition is false execution will continue at the next statement after the body of the loop.

You can compare a while loop to a for loop to understand that a for loop actually executes like a while loop does if you use the while loop to repeat the body of the loop a specific number of times.

labs/repetition/Figures/compareForAndWhile.png

Figure 1: Showing how a for loop maps to a while loop

Q-235: Q-234: What do you think will happen when you run the code in the next section below? How would it change if you modified line 11 to initialize the loop with i = 3?

Note

The method printPopSong prints the words to a song. It initializes the value of the variable i equal to 5 and then checks if i is greater than 0. Since 5 is greater than 0, the body of the loop executes. Before the condition is checked again, i is decreased by 1. When the value in i is equal to 0 the loop stops executing.

The number of times a loop executes can be calculated by (largestValue - smallestValue + 1). By the largest value I mean the largest value that allows the loop to execute and by the smallest value I mean the smallest value that allows the loop to execute. So in the code above the largest value is 5 and the smallest value that allows the loop to execute is 1 so this loop executes (5 - 1 + 1 = 5 times).

Check your understanding

    Q-236: What does the following code print?

    for (int i = 3; i < 8; i++)
    {
       System.out.print(i + " ");
    }
    
  • 3 4 5 6 7 8
  • This loop starts with i equal to 3 but ends when i is equal to 8.
  • 0 1 2 3 4 5 6 7 8
  • What is i set to in the initialization area?
  • 8 8 8 8 8
  • This would be true if the for loop was missing the update part (int i = 3; i < 8; ) but it does increment i in the update part (int i = 3; i < 8; i++).
  • 3 4 5 6 7
  • The value of i is set to 3 before the loop executes and the loop stops when i is equal to 8. So the last time through the loop i is equal to 7.

    Q-237: What does the following code print?

    for (int i = 1; i <= 10; i++)
    {
       System.out.print(i + " ");
    }
    
  • 3 4 5 6 7 8
  • What is i set to in the initialization area?
  • 0 1 2 3 4 5 6 7 8 9
  • What is i set to in the initialization area?
  • 1 2 3 4 5 6 7 8 9 10
  • The value of i starts at 1 and this loop will execute until i equals 11. The last time through the loop the value of i is 10.
  • 1 3 5 7 9
  • This loop increments i by 1 each time in the update area.

    Q-238: How many times does the following method print a *?

    for (int i = 3; i <= 9; i++)
    {
       System.out.print("*");
    }
    
  • 10
  • This would be true if i started at 0 and ended at 9. Does it?
  • 6
  • Since i starts at 3 and the last time through the loop it is 9 the loop executes 7 times (9 - 3 + 1 = 7)
  • 7
  • How many numbers are between 3 and 9 (including 3 and 9)?
  • 9
  • This would be true if i started at 0 and the value of i the last time through the loop it was 8.

Mixed up programs

    Q-241: What does the following code print?

    for (int i = 3; i <= 12; i++)
    {
       System.out.print(i + " ");
    }
    
  • 5 6 7 8 9
  • What is i set to in the initialization area?
  • 4 5 6 7 8 9 10 11 12
  • What is i set to in the initialization area?
  • 3 5 7 9 11
  • This loop increments i by 1 each time in the update area.
  • 3 4 5 6 7 8 9 10 11 12
  • The value of i starts at 3 and this loop will execute until i equals 12. The last time through the loop the value of i is 12 at the begininng and then it will be incremented to 13 which stops the loop since 13 is not less than or equal to 12.

    Q-242: How many times does the following method print a *?

    for (int i = 3; i < 9; i++)
    {
       System.out.print("*");
    }
    
  • 9
  • This would be true if i started at 0.
  • 7
  • Note that it stops when i is 9.
  • 6
  • Since i starts at 3 and the last time through the loop it is 8 the loop executes 8 - 3 + 1 times = 6 times.
  • 10
  • This would be true if i started at 0 and ended when i was 10. Does it?

    Q-243: What does the following code print?

    int x = -5;
    while (x < 0)
    {
       x++;
       System.out.print(x + " ");
    }
    
  • 5 4 3 2 1
  • x is initialized (set) to -5 to start.
  • -5 -4 -3 -2 -1
  • x is incremented (x++) before the print statement executes.
  • -4 -3 -2 -1 0
  • x is set to -5 to start but then incremented by 1 so it first prints -4.

    Q-244: How many times does the following method print a *?

    for (int i = 5; i <= 12; i++)
    {
       System.out.print("*");
    }
    
  • 7
  • This would be true if it stopped when i was 12, but it loops when i is 12.
  • 8
  • Note that it stops when i is 13 so 13 - 5 is 8.
  • 12
  • This would be true if i started at 1.
  • 13
  • This would be true if i started at 0.

    Q-245: How many times does the following method print a *?

    for (int i = 1; i < 5; i++)
    {
       System.out.print("*");
    }
    
  • 4
  • The loop starts with i = 1 and loops as long as it is less than 5 so i is 1, 2, 3, 4.
  • 5
  • This would be true if the condition was i <= 5.
  • 6
  • This would be true if i started at 0 and ended when it reached 6 (i <= 5).

    Q-246: How many times does the following method print a *?

    for (int i = 0; i <= 8; i++)
    {
       System.out.print("*");
    }
    
  • 7
  • This would be true if i started at 1 and ended when it reached 8.
  • 8
  • This would be true if the loop ended when i reached 8.
  • 9
  • This loop starts with i = 0 and continues till it reaches 9 so (9 - 0 = 9).

    Q-247: How many times does the following method print a *?

    for (int x = 0; x < 5; x++)
    {
       System.out.print("*");
    }
    
  • 4
  • This would be true if x started at 1 instead of 0.
  • 5
  • The loop starts with x = 0 and ends when it reaches 5 so 5 - 0 = 5.
  • 6
  • This would be true if the condition was x <= 5 instead of x = 5.

    Q-248: How many times does the following method print a *?

    for (int x = 2; x < 8; x++)
    {
       System.out.print("*");
    }
    
  • 6
  • This loop starts with x = 2 and continues while it is less than 8 so 8 - 2 = 6.
  • 7
  • This would be true if the loop ended when x was 9 instead of 8 (x <= 8).
  • 8
  • This would be true if the loop started with x = 0.
Next Section - 2. Nested For Loops