while and for Loops
Free AP Computer Science A multiple-choice practice for topics 2.7–2.8, Unit 2 (Selection and Iteration). Answer five random questions, check your work, and review the essential knowledge behind each one.
Unit 2: Selection and Iteration. Topics 2.7–2.8. This set has 55 questions. Each time the page loads you get five of them, chosen at random. Pick an answer, press Check answer, and read the explanation for every choice. Reload the page or use New questions for a fresh five.
Topics assessed
- 2.7 while Loops
- 2.8 for Loops
Practice questions
Loading questions…
Essential knowledge for this set
These are the essential knowledge (EKS) statements from the AP Computer Science A Course and Exam Description that the 55 questions in this set assess, grouped by topic and learning objective. The table under each question links here.
Topic 2.7: while Loops
2.7.A — Identify when an iterative process is required to achieve a desired result.
- 2.7.A.1 Iteration is a form of repetition. Iteration statements change the flow of control by repeating a segment of code zero or more times as long as the Boolean expression controlling the loop evaluates to true.
- 2.7.A.2 An infinite loop occurs when the Boolean expression in an iterative statement always evaluates to true.
- 2.7.A.3 The loop body of an iterative statement will not execute if the Boolean expression initially evaluates to false.
- 2.7.A.4 Off by one errors occur when the iteration statement loops one time too many or one time too few.
2.7.B — Develop code to represent iterative processes using while loops and determine the result of these processes.
- 2.7.B.1 A while loop is a type of iterative statement. In while loops, the Boolean expression is evaluated before each iteration of the loop body, including the first. When the expression evaluates to true, the loop body is executed. This continues until the Boolean expression evaluates to false, whereupon the iteration terminates.
Topic 2.8: for Loops
2.8.A — Develop code to represent iterative processes using for loops and determine the result of these processes.
- 2.8.A.1 A for loop is a type of iterative statement. There are three parts in a for loop header: the initialization, the Boolean expression, and the update.
- 2.8.A.2 In a for loop, the initialization statement is only executed once before the first Boolean expression evaluation. The variable being initialized is referred to as a loop control variable. The Boolean expression is evaluated immediately after the loop control variable is initialized and then following each execution of the increment statement until it is false. In each iteration, the update is executed after the entire loop body is executed and before the Boolean expression is evaluated again.
- 2.8.A.3 A for loop can be rewritten into an equivalent while loop (and vice versa).