5.1.1. Loops-WE1-P1ΒΆ
Subgoals for Evaluating a Loop
Diagram which statements go together.
Define and initialize variables
Determine the start condition.
Determine the update condition.
Determine the termination condition.
Determine body that is repeated.
Trace the loop.
For every iteration of the loop, write down the values.
Loops-WE1-P1
- See diagram for answer A
- See diagram for answer B
- See diagram for answer C
- See diagram for answer D
- See diagram for answer E
Q1: What is the output of the following loop?
int i = 0;
while (i < 3) {
System.out.println("hi");
i++;
}

- 10 9 8 7 6 5 4 3 2 1
- 9 8 7 6 5 4 3 2 1
- 9 8 7 6 5 4 3 2
- 10 9 8 7 6 5 4 3 2
- compiler error
Q2: What is the output of the following loop?
int i = 10;
while (i > 1) {
System.out.println(i + " ");
i--;
}
- 10 11 12 13 14 15 16
- 10 9 8 7 6 5 4 3 2
- 10 9 8 7 6 5 4 3 2 1
- 1 2 3 4 5 6 7 8 9 10
- infinite loop
Q3: What is the output of the following loop?
int i = 10;
while (i > 1) {
System.out.println(i + " ");
i++;
}
- 0 5 10 15 20 25 30 35 40 45 50
- 0 5 10 15 20 25 30 35 40 45
- 5 10 15 20 25 30 35 40 45 50
- 5 10 15 20 25 30 35 40 45
- 55
Q4: What is the output of the following loop?
int i = 0;
int total = 0;
while (i <= 50) {
total += i;
i += 5;
}
System.out.println(i);
- 0
- 1
- 99
- 100
- 101
Q5: What is the value of counter
after the execution of the folowing code?
int counter = 0;
while (counter > 100) {
if (counter % 2 == 1)
System.out.println(counter + " is odd.");
else
System.out.println(counter + " is even.");
}
counter++;
System.out.println(counter);
You have attempted of activities on this page