for Loops - AP Computer Science A

  1. What does the 'break' statement do in a for loop?
    Exits the loop immediately. Terminates loop execution completely and immediately.
  2. What does the loop control variable do in a for loop?
    Controls the number of iterations. Tracks and manages the loop's progression through iterations.
  3. How many times will the loop execute: for(int i = 0; i < 4; i++) { }?
    4 times. Condition i<4i < 4 is true for values 0, 1, 2, 3.
  4. Identify the keyword used to skip an iteration in a for loop.
    continue. Jumps to next iteration, skipping remaining body code.
  5. What is a potential risk of using nested for loops?
    Increased time complexity; potential inefficiency. Multiple nested loops create exponential time growth.
  6. Convert the for loop: for(int i = 0; i < 3; i++) { System.out.println(i); }
    int i = 0; while(i < 3) { System.out.println(i); i++; }. While equivalent with explicit variable management.
  7. What is the output of for(int i = 3; i >= 0; i--) { System.out.print(i); }?
    3210. Counts down from 3 to 0, printing each value.
  8. Identify the components of a for loop in Java.
    Initialization, condition, update, and body. Four essential parts that control loop execution.
  9. What part of the for loop is executed first?
    Initialization. Sets up the loop control variable before any iterations.
  10. Identify the error: for(int i = 0; i < 10; i--) { }
    i-- causes an infinite loop. Decrementing makes ii more negative, never reaching 10.
  11. What keyword is used to exit a for loop prematurely?
    break. Immediately terminates loop execution.
  12. Identify the error: for(int i = 0; i < 3; i++) { System.out.print(i)
    Missing closing brace '}' for loop. Loop body needs closing brace to match opening.
  13. What loop type is more suitable for counting iterations?
    for loop. Built-in counter makes for loops ideal for counting.
  14. What is the output of for(int i = 5; i > 0; i--) { System.out.print(i); }?
    54321. Counts down from 5 to 1, printing each value.
  15. Which loop structure is preferable for indefinite iteration?
    while loop. Better for unknown iteration counts or condition-based loops.
  16. Find the error: for(int i = 0; i < 3; i++) System.out.println(i);
    No error; valid loop with single statement body. Single statements don't require braces in Java.
  17. What is the primary purpose of a for loop in programming?
    To iterate a block of code a specific number of times. Provides controlled repetition with a predetermined count.
  18. Identify the error: for(int i = 0; i <= 5; i++) { System.out.print(j); }
    j is not defined within the loop. Variable jj was never declared or initialized.
  19. How can a for loop iterate over an array in Java?
    for(int i = 0; i < array.length; i++) { }. Standard pattern using array length for safe iteration.
  20. Find the error: for(int i = 0; i < 10; i++) { System.out.println(i) }
    Missing semicolon after System.out.println(i);. Statement needs semicolon after println call.
  21. Convert the loop: for(int i = 1; i <= 5; i++) { total *= i; }
    int i = 1; while(i <= 5) { total *= i; i++; }. Equivalent while loop for factorial calculation.
  22. What is the syntax for a basic for loop in Java?
    for(initialization; condition; update) { body }. Standard structure with semicolons separating three parts.
  23. What is the result of nesting a for loop within another for loop?
    Nested loops allow multi-dimensional iteration. Creates loops within loops for complex patterns.
  24. Convert the for loop: for(int i = 0; i < 5; i++) { sum += i; }
    int i = 0; while(i < 5) { sum += i; i++; }. Equivalent while loop with manual variable management.
  25. What keyword can alter the flow of a loop iteration?
    continue. Skips current iteration and proceeds to next one.
  26. What is the initial value of 'i' in the loop: for(int i = 0; i < 3; i++) { }?
    0. Initialization sets starting value for loop variable.
  27. What is the scope of a variable declared in a for loop?
    Limited to the for loop block. Loop variable exists only within the loop brackets.
  28. What happens if the condition in a for loop is always true?
    The loop will run indefinitely. True condition never allows loop to terminate naturally.
  29. What condition causes a for loop to terminate?
    When the loop condition evaluates to false. Boolean condition controls when loop stops executing.
  30. What is the role of the update expression in a for loop?
    To modify the loop variable for the next iteration. Updates the counter variable after each iteration completes.
  31. What is the purpose of the 'continue' statement in a for loop?
    To skip the current iteration and continue with the next. Skips remaining body code and moves to next iteration.
  32. What happens if the update expression is omitted in a for loop?
    It results in a potential infinite loop. Without variable modification, loop may never terminate.
  33. What is the result of omitting the condition in a for loop?
    The loop becomes infinite. Missing condition defaults to true, causing endless execution.
  34. Which part of the for loop is executed after each iteration?
    Update. Modifies the loop variable at the end of each cycle.
  35. What is the output of the code: for(int i = 0; i < 2; i++) { for(int j = 0; j < 2; j++) { System.out.print(i+j); } }?
    0121. Inner loop runs completely for each outer loop iteration.
  36. What is the output of for(int i = 0; i < 3; i++) { System.out.print(i); }?
    012. Prints ii values 0, 1, 2 consecutively.
  37. Determine the output: for(int i = 2; i < 5; i++) { System.out.print(i); }
    234. Prints ii values 2, 3, 4 consecutively.

Study these flashcards