while Loops() - AP Computer Science A
- What is the basic syntax of a while loop in Java?
- while (condition) { statements }. Standard format: keyword, condition in parentheses, then body.
- Identify the loop: while (x < 10) { x++; }
- A simple counting loop. Basic incrementing loop pattern for iteration.
- Can a while loop have multiple conditions?
- Yes, using logical operators. Combine conditions with &&, , or operators.
- What is the output? int x = 1; while (x++ < 3) { System.out.print(x); }
- 23. Pre-increment: prints 2, 3 as increases before comparison.
- What happens if the condition of a while loop never becomes false?
- The program enters an infinite loop. Condition never becomes false, creating endless execution.
- Identify the error: int i = 0; while i < 10 { i++; }
- Missing parentheses: while (i < 10) { i++; }. Condition must be enclosed in parentheses.
- What is an infinite loop?
- A loop that never terminates. Loop condition never becomes false.
- Identify the infinite loop: while (true) { // do something }
- This is an infinite loop. Condition true is always true, never terminates.
- How can you modify a while loop to count down?
- Decrement the loop control variable. Change increment to decrement for countdown behavior.
- Identify the problem: while (true); { println('Hello'); }
- Unreachable code due to infinite loop. Semicolon creates empty loop body; unreachable code follows.
- Identify the loop type: while (x > 0) { x--; }
- A decrementing loop. Loop variable decreases each iteration.
- How many times will the loop execute? int i = 0; while (i < 5) { i++; }
- 5 times. Loop runs while : iterations 0, 1, 2, 3, 4.
- Identify the error: while (x < 5) x += 1;
- No error, but braces improve readability. Single statements work but braces prevent errors.
- What is the risk of modifying loop variables inside a while loop?
- Potential for infinite loops or logic errors. Incorrect modifications can break loop termination.
- What does the continue statement do in a loop?
- Skips the current iteration and proceeds to the next. Jumps to condition check, skipping remaining body.
- What statement can exit a loop prematurely?
- break. Immediately exits the loop structure.
- What type must the while loop condition evaluate to?
- boolean. Condition must evaluate to true or false.
- How do you prevent an infinite loop?
- Ensure the loop condition becomes false. Modify loop variables to eventually make condition false.
- What is the common use of a while loop?
- To repeat a task while waiting for a condition to change. Typical use for event-driven or input-dependent processing.
- Determine if the loop will run: while (x > 0) { x--; } if x is initialized to 0.
- No, it will not run. Initial condition is false when .
- What is the primary purpose of a while loop?
- To repeat a block of code as long as a condition is true. This defines the fundamental concept of iterative control.
- Identify if this is a valid loop: while (x = 5) { x--; }
- No, condition must be a boolean expression. Assignment () is not a boolean comparison ().
- What is the initial condition in a while loop?
- The condition checked before the first loop iteration. First evaluation determines if loop body executes.
- What is a post-test loop?
- A loop that evaluates its condition after executing. Condition evaluated after body execution each time.
- Which part of a while loop is evaluated before each iteration?
- The loop condition. Pre-test evaluation determines if loop body executes.
- What is the difference between while and do-while loops?
- do-while executes at least once; while may not. Different timing of condition evaluation affects behavior.
- What is the output? int x = 10; while (x > 5) { x--; } System.out.println(x);
- 5. Loop decrements from 10 until , then stops.
- Identify if this loop will execute: while (1 == 1) { break; }
- Yes, but it exits immediately. Break statement causes immediate exit after one iteration.
- Identify the keyword used to initiate a while loop in Java.
- while. The reserved word that starts loop syntax.
- Identify if this loop will execute: int i = 5; while (i < 0) { i--; }
- No, it will not execute. Condition is initially false.
- Find the error: while (x < 10) x++;
- Missing braces: while (x < 10) { x++; }. Single statements need braces for clarity and safety.
- What is the output? int x = 3; while (x-- > 0) { System.out.print(x); }
- 210. Post-decrement: prints 2, 1, 0 as decreases.
- What is the loop control variable?
- A variable that influences the loop's continuation. Variable that determines when loop terminates.
- Identify the error: while (x < 10) { System.out.print(x); }
- Infinite loop if x < 10 is always true. Missing increment creates infinite loop condition.
- When does a while loop terminate?
- When the loop condition evaluates to false. Boolean expression controls loop continuation.
- Determine if this loop will execute: while (false) { doSomething(); }
- No, it will not execute. False condition prevents any loop execution.
- What is a pre-test loop?
- A loop that evaluates its condition before executing. Condition checked before body execution each time.