Skip to main content

Section 5.4 Loops-WE2-P1

Subsection 5.4.1 Subgoals for Evaluating a Loop

  1. Diagram which statements go together.

  2. Define and initialize variables

    1. Determine the start condition.

    2. Determine the update condition.

    3. Determine the termination condition.

    4. Determine body that is repeated.

  3. Trace the loop.

    1. For every iteration of the loop, write down the values.

Subsection 5.4.2 Loops-WE2-P1

Exercises Exercises

1.

    Q6: What is the value of total after the execution of the folowing code?

    int total = 0;
    for (int x = 50; x >= 0; x -= 5) {
       total += x;
    }
    
  • 50

  • 0

  • 5

  • 270

  • 275

2.

    Q7: What is the output of the following loop?

    for (int y = 0; y < 10; y++) {
       System.out.print(y * y + " ");
    }
    
  • 0 1 4 9 16 25 36 49 64 81

  • 1 4 9 16 25 36 49 64 81

  • 1 4 9 16 25 36 49 64

  • 0 1 2 3 4 5 6 7 8 9 10

  • 1 2 3 4 5 6 7 8 9 10

3.

    Q8: What is the output of the following loop?

    for (int y = 100; y < 10; y--) {
       System.out.println(y);
    }
    
  • 100

  • 10

  • 100 90 80 70 60 50 40 30 20

  • 100 90 80 70 60 50 40 30 20 10

  • no output produced

4.

Q9: What are the values of a and b after the folowing code executes?

You have attempted of activities on this page.