while Loops() - AP Computer Science A
Card 1 of 30
What type must the while loop condition evaluate to?
What type must the while loop condition evaluate to?
Tap to reveal answer
boolean. Condition must evaluate to true or false.
boolean. Condition must evaluate to true or false.
← Didn't Know|Knew It →
Find the error: while (x < 10) x++;
Find the error: while (x < 10) x++;
Tap to reveal answer
Missing braces: while (x < 10) { x++; }. Single statements need braces for clarity and safety.
Missing braces: while (x < 10) { x++; }. Single statements need braces for clarity and safety.
← Didn't Know|Knew It →
Identify the problem: while (true); { println('Hello'); }
Identify the problem: while (true); { println('Hello'); }
Tap to reveal answer
Unreachable code due to infinite loop. Semicolon creates empty loop body; unreachable code follows.
Unreachable code due to infinite loop. Semicolon creates empty loop body; unreachable code follows.
← Didn't Know|Knew It →
Identify the error: int i = 0; while i < 10 { i++; }
Identify the error: int i = 0; while i < 10 { i++; }
Tap to reveal answer
Missing parentheses: while (i < 10) { i++; }. Condition must be enclosed in parentheses.
Missing parentheses: while (i < 10) { i++; }. Condition must be enclosed in parentheses.
← Didn't Know|Knew It →
How many times will the loop execute? int i = 0; while (i < 5) { i++; }
How many times will the loop execute? int i = 0; while (i < 5) { i++; }
Tap to reveal answer
5 times. Loop runs while $i < 5$: iterations 0, 1, 2, 3, 4.
5 times. Loop runs while $i < 5$: iterations 0, 1, 2, 3, 4.
← Didn't Know|Knew It →
What is the risk of modifying loop variables inside a while loop?
What is the risk of modifying loop variables inside a while loop?
Tap to reveal answer
Potential for infinite loops or logic errors. Incorrect modifications can break loop termination.
Potential for infinite loops or logic errors. Incorrect modifications can break loop termination.
← Didn't Know|Knew It →
Identify if this is a valid loop: while (x = 5) { x--; }
Identify if this is a valid loop: while (x = 5) { x--; }
Tap to reveal answer
No, condition must be a boolean expression. Assignment ($=$) is not a boolean comparison ($==$).
No, condition must be a boolean expression. Assignment ($=$) is not a boolean comparison ($==$).
← Didn't Know|Knew It →
What is the output? int x = 10; while (x > 5) { x--; } System.out.println(x);
What is the output? int x = 10; while (x > 5) { x--; } System.out.println(x);
Tap to reveal answer
- Loop decrements from 10 until $x = 5$, then stops.
- Loop decrements from 10 until $x = 5$, then stops.
← Didn't Know|Knew It →
Identify the error: while (x < 5) x += 1;
Identify the error: while (x < 5) x += 1;
Tap to reveal answer
No error, but braces improve readability. Single statements work but braces prevent errors.
No error, but braces improve readability. Single statements work but braces prevent errors.
← Didn't Know|Knew It →
What is the initial condition in a while loop?
What is the initial condition in a while loop?
Tap to reveal answer
The condition checked before the first loop iteration. First evaluation determines if loop body executes.
The condition checked before the first loop iteration. First evaluation determines if loop body executes.
← Didn't Know|Knew It →
What is the loop control variable?
What is the loop control variable?
Tap to reveal answer
A variable that influences the loop's continuation. Variable that determines when loop terminates.
A variable that influences the loop's continuation. Variable that determines when loop terminates.
← Didn't Know|Knew It →
Identify if this loop will execute: int i = 5; while (i < 0) { i--; }
Identify if this loop will execute: int i = 5; while (i < 0) { i--; }
Tap to reveal answer
No, it will not execute. Condition $5 < 0$ is initially false.
No, it will not execute. Condition $5 < 0$ is initially false.
← Didn't Know|Knew It →
What statement can exit a loop prematurely?
What statement can exit a loop prematurely?
Tap to reveal answer
break. Immediately exits the loop structure.
break. Immediately exits the loop structure.
← Didn't Know|Knew It →
Identify the infinite loop: while (true) { // do something }
Identify the infinite loop: while (true) { // do something }
Tap to reveal answer
This is an infinite loop. Condition true is always true, never terminates.
This is an infinite loop. Condition true is always true, never terminates.
← Didn't Know|Knew It →
How do you prevent an infinite loop?
How do you prevent an infinite loop?
Tap to reveal answer
Ensure the loop condition becomes false. Modify loop variables to eventually make condition false.
Ensure the loop condition becomes false. Modify loop variables to eventually make condition false.
← Didn't Know|Knew It →
Identify if this loop will execute: while (1 == 1) { break; }
Identify if this loop will execute: while (1 == 1) { break; }
Tap to reveal answer
Yes, but it exits immediately. Break statement causes immediate exit after one iteration.
Yes, but it exits immediately. Break statement causes immediate exit after one iteration.
← Didn't Know|Knew It →
How can you modify a while loop to count down?
How can you modify a while loop to count down?
Tap to reveal answer
Decrement the loop control variable. Change increment to decrement for countdown behavior.
Decrement the loop control variable. Change increment to decrement for countdown behavior.
← Didn't Know|Knew It →
Identify the loop: while (x < 10) { x++; }
Identify the loop: while (x < 10) { x++; }
Tap to reveal answer
A simple counting loop. Basic incrementing loop pattern for iteration.
A simple counting loop. Basic incrementing loop pattern for iteration.
← Didn't Know|Knew It →
Identify the loop type: while (x > 0) { x--; }
Identify the loop type: while (x > 0) { x--; }
Tap to reveal answer
A decrementing loop. Loop variable decreases each iteration.
A decrementing loop. Loop variable decreases each iteration.
← Didn't Know|Knew It →
What is the output? int x = 1; while (x++ < 3) { System.out.print(x); }
What is the output? int x = 1; while (x++ < 3) { System.out.print(x); }
Tap to reveal answer
- Pre-increment: prints 2, 3 as $x$ increases before comparison.
- Pre-increment: prints 2, 3 as $x$ increases before comparison.
← Didn't Know|Knew It →
What is the output? int x = 3; while (x-- > 0) { System.out.print(x); }
What is the output? int x = 3; while (x-- > 0) { System.out.print(x); }
Tap to reveal answer
- Post-decrement: prints 2, 1, 0 as $x$ decreases.
- Post-decrement: prints 2, 1, 0 as $x$ decreases.
← Didn't Know|Knew It →
What is the basic syntax of a while loop in Java?
What is the basic syntax of a while loop in Java?
Tap to reveal answer
while (condition) { statements }. Standard format: keyword, condition in parentheses, then body.
while (condition) { statements }. Standard format: keyword, condition in parentheses, then body.
← Didn't Know|Knew It →
Which part of a while loop is evaluated before each iteration?
Which part of a while loop is evaluated before each iteration?
Tap to reveal answer
The loop condition. Pre-test evaluation determines if loop body executes.
The loop condition. Pre-test evaluation determines if loop body executes.
← Didn't Know|Knew It →
What type must the while loop condition evaluate to?
What type must the while loop condition evaluate to?
Tap to reveal answer
boolean. Condition must evaluate to true or false.
boolean. Condition must evaluate to true or false.
← Didn't Know|Knew It →
What happens if the condition of a while loop never becomes false?
What happens if the condition of a while loop never becomes false?
Tap to reveal answer
The program enters an infinite loop. Condition never becomes false, creating endless execution.
The program enters an infinite loop. Condition never becomes false, creating endless execution.
← Didn't Know|Knew It →
Identify the keyword used to initiate a while loop in Java.
Identify the keyword used to initiate a while loop in Java.
Tap to reveal answer
while. The reserved word that starts loop syntax.
while. The reserved word that starts loop syntax.
← Didn't Know|Knew It →
Can a while loop have multiple conditions?
Can a while loop have multiple conditions?
Tap to reveal answer
Yes, using logical operators. Combine conditions with $&&$, $||$, or $!$ operators.
Yes, using logical operators. Combine conditions with $&&$, $||$, or $!$ operators.
← Didn't Know|Knew It →
What is a post-test loop?
What is a post-test loop?
Tap to reveal answer
A loop that evaluates its condition after executing. Condition evaluated after body execution each time.
A loop that evaluates its condition after executing. Condition evaluated after body execution each time.
← Didn't Know|Knew It →
Identify the error: while (x < 10) { System.out.print(x); }
Identify the error: while (x < 10) { System.out.print(x); }
Tap to reveal answer
Infinite loop if x < 10 is always true. Missing increment creates infinite loop condition.
Infinite loop if x < 10 is always true. Missing increment creates infinite loop condition.
← Didn't Know|Knew It →
What is a pre-test loop?
What is a pre-test loop?
Tap to reveal answer
A loop that evaluates its condition before executing. Condition checked before body execution each time.
A loop that evaluates its condition before executing. Condition checked before body execution each time.
← Didn't Know|Knew It →