while Loops() - AP Computer Science A

  1. What is the basic syntax of a while loop in Java?
    while (condition) { statements }. Standard format: keyword, condition in parentheses, then body.
  2. Identify the loop: while (x < 10) { x++; }
    A simple counting loop. Basic incrementing loop pattern for iteration.
  3. Can a while loop have multiple conditions?
    Yes, using logical operators. Combine conditions with &&, ∣∣||, or !! operators.
  4. What is the output? int x = 1; while (x++ < 3) { System.out.print(x); }
    23. Pre-increment: prints 2, 3 as xx increases before comparison.
  5. 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.
  6. Identify the error: int i = 0; while i < 10 { i++; }
    Missing parentheses: while (i < 10) { i++; }. Condition must be enclosed in parentheses.
  7. What is an infinite loop?
    A loop that never terminates. Loop condition never becomes false.
  8. Identify the infinite loop: while (true) { // do something }
    This is an infinite loop. Condition true is always true, never terminates.
  9. How can you modify a while loop to count down?
    Decrement the loop control variable. Change increment to decrement for countdown behavior.
  10. Identify the problem: while (true); { println('Hello'); }
    Unreachable code due to infinite loop. Semicolon creates empty loop body; unreachable code follows.
  11. Identify the loop type: while (x > 0) { x--; }
    A decrementing loop. Loop variable decreases each iteration.
  12. How many times will the loop execute? int i = 0; while (i < 5) { i++; }
    5 times. Loop runs while i<5i < 5: iterations 0, 1, 2, 3, 4.
  13. Identify the error: while (x < 5) x += 1;
    No error, but braces improve readability. Single statements work but braces prevent errors.
  14. 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.
  15. 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.
  16. What statement can exit a loop prematurely?
    break. Immediately exits the loop structure.
  17. What type must the while loop condition evaluate to?
    boolean. Condition must evaluate to true or false.
  18. How do you prevent an infinite loop?
    Ensure the loop condition becomes false. Modify loop variables to eventually make condition false.
  19. 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.
  20. Determine if the loop will run: while (x > 0) { x--; } if x is initialized to 0.
    No, it will not run. Initial condition x>0x > 0 is false when x=0x = 0.
  21. 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.
  22. Identify if this is a valid loop: while (x = 5) { x--; }
    No, condition must be a boolean expression. Assignment (==) is not a boolean comparison (====).
  23. What is the initial condition in a while loop?
    The condition checked before the first loop iteration. First evaluation determines if loop body executes.
  24. What is a post-test loop?
    A loop that evaluates its condition after executing. Condition evaluated after body execution each time.
  25. Which part of a while loop is evaluated before each iteration?
    The loop condition. Pre-test evaluation determines if loop body executes.
  26. 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.
  27. What is the output? int x = 10; while (x > 5) { x--; } System.out.println(x);
    5. Loop decrements from 10 until x=5x = 5, then stops.
  28. Identify if this loop will execute: while (1 == 1) { break; }
    Yes, but it exits immediately. Break statement causes immediate exit after one iteration.
  29. Identify the keyword used to initiate a while loop in Java.
    while. The reserved word that starts loop syntax.
  30. Identify if this loop will execute: int i = 5; while (i < 0) { i--; }
    No, it will not execute. Condition 5<05 < 0 is initially false.
  31. Find the error: while (x < 10) x++;
    Missing braces: while (x < 10) { x++; }. Single statements need braces for clarity and safety.
  32. What is the output? int x = 3; while (x-- > 0) { System.out.print(x); }
    210. Post-decrement: prints 2, 1, 0 as xx decreases.
  33. What is the loop control variable?
    A variable that influences the loop's continuation. Variable that determines when loop terminates.
  34. Identify the error: while (x < 10) { System.out.print(x); }
    Infinite loop if x < 10 is always true. Missing increment creates infinite loop condition.
  35. When does a while loop terminate?
    When the loop condition evaluates to false. Boolean expression controls loop continuation.
  36. Determine if this loop will execute: while (false) { doSomething(); }
    No, it will not execute. False condition prevents any loop execution.
  37. What is a pre-test loop?
    A loop that evaluates its condition before executing. Condition checked before body execution each time.

Study these flashcards