QUESTION IMAGE
Question
question: 9
what is the last thing printed by the following program?
var start = 30;
var stop = 10;
for(var i = start; i >= stop; i-=5){
if(i % 2 == 0){
println(i * 2);
} else {
println(i);
}
}
10
20
30
60
Step1: Analyze the for loop
The for loop starts with i = 30 (since start = 30), and continues as long as i >= stop (where stop = 10), with i decrementing by 5 each iteration.
Step2: Check each iteration
- When
i = 30:30 % 2 = 0, so print30 * 2 = 60. - When
i = 25:25 % 2 != 0, so print25. - When
i = 20:20 % 2 = 0, so print20 * 2 = 40. - When
i = 15:15 % 2 != 0, so print15. - When
i = 10:10 % 2 = 0, so print10 * 2 = 20.
Snap & solve any problem in the app
Get step-by-step solutions on Sovi AI
Photo-based solutions with guided steps
Explore more problems and detailed explanations
20