Skip to main content

Section 7.4 Loops-WE2-P1

Subgoals for Evaluating a Loop.

  1. Identify loop parts
    1. Determine start condition
    2. Determine update condition
    3. Determine termination condition
    4. Determine body that is repeated
  2. Trace the loop
    1. For every iteration of loop, write down values

Subsection 7.4.1

Exercises Exercises

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
  • Correct
  • 1 4 9 16 25 36 49 64 81
  • Incorrect
  • 1 4 9 16 25 36 49 64
  • Incorrect
  • 0 1 2 3 4 5 6 7 8 9 10
  • Incorrect
  • 1 2 3 4 5 6 7 8 9 10
  • Incorrect
3.
Q8: What is the output of the following loop?
for (int y = 100; y < 10; y--) {
   System.out.println(y);
}
Given
int a = 0, b = 0;
for (int x = 1; x <= 15; x++) {
    if (x % 2 == 0)
        a += x;
    else
        b += x;
}
System.out.println("a is " + a);
System.out.println("b is " + b);
4.
Q9: What are the values of a and b after the above code executes?
The value of a is and the value of b is .
You have attempted of activities on this page.