Back to Student Worksheet
CS Grade 9-12 Answer Key

CS: Loops and Control Flow

Tracing, writing, and reasoning about loops and decisions

Answer Key
Name:
Date:
Score: / 15

CS: Loops and Control Flow

Tracing, writing, and reasoning about loops and decisions

CS - Grade 9-12

Instructions: Read each problem carefully. Show your work in the space provided. For tracing problems, list the important variable values and the final output.
  1. 1

    Trace this pseudocode and write the final output: x = 0 for i = 1 to 4: x = x + i print(x)

    Make a small table with columns for i and x.

    The final output is 10. The loop adds 1, 2, 3, and 4 to x, so x becomes 0 + 1 + 2 + 3 + 4 = 10.
  2. 2

    Trace this pseudocode and write the final output: count = 0 n = 10 while n > 1: n = n / 2 count = count + 1 print(count)

    The final output is 4. The values of n are 10, 5, 2.5, 1.25, and then 0.625 after the fourth loop, so the loop runs 4 times before n is no longer greater than 1.
  3. 3

    Explain the difference between a for loop and a while loop. Give one situation where each type is a good choice.

    Think about whether you know the number of repetitions before the loop starts.

    A for loop is usually best when the number of repetitions is known, such as looping through 20 quiz scores. A while loop is usually best when the loop should continue until a condition changes, such as asking for a password until the correct one is entered.
  4. 4

    Write pseudocode that prints all even numbers from 2 through 20, including 2 and 20.

    One correct answer is: for n = 2 to 20 step 2: print(n) This works because the loop starts at 2 and increases by 2 each time.
  5. 5

    The following loop is intended to print the numbers 1 through 5. Find and fix the error: i = 1 while i < 5: print(i) i = i + 1

    Check whether the value 5 satisfies the loop condition.

    The condition should be i <= 5 instead of i < 5. The fixed loop is: i = 1 while i <= 5: print(i) i = i + 1 This prints 1, 2, 3, 4, and 5.
  6. 6

    Trace this code and write the final output: for i = 1 to 3: for j = 1 to 2: print(i, j)

    The output is: 1 1 1 2 2 1 2 2 3 1 3 2 The outer loop runs 3 times, and for each outer loop value, the inner loop runs 2 times.
  7. 7

    A program uses this condition: if temperature >= 100: print("Boiling") else: print("Not boiling") What will the program print when temperature is 100? Explain why.

    The symbol >= means greater than or equal to.

    The program will print "Boiling" because 100 is equal to 100, so the condition temperature >= 100 is true.
  8. 8

    Write pseudocode that asks a user for a number until they enter a positive number. Then print "Accepted".

    One correct answer is: number = input("Enter a positive number: ") while number <= 0: number = input("Enter a positive number: ") print("Accepted") This repeats until the input is greater than 0.
  9. 9

    Identify whether this loop is an infinite loop. If it is infinite, explain how to fix it: x = 5 while x > 0: print(x) x = x + 1

    Look at whether x is moving toward making the condition false.

    This is an infinite loop because x starts at 5 and keeps increasing, so x is always greater than 0. One fix is to change x = x + 1 to x = x - 1 so the loop eventually stops.
  10. 10

    Trace this pseudocode and write the final value of total: total = 0 for n = 1 to 6: if n % 2 == 0: total = total + n print(total)

    The final value of total is 12. The condition is true for even values of n, so the loop adds 2, 4, and 6.
  11. 11

    A break statement stops a loop early. Trace this pseudocode and write the output: for n = 1 to 10: if n == 4: break print(n)

    A break exits the loop immediately.

    The output is: 1 2 3 When n becomes 4, the break statement stops the loop before 4 is printed.
  12. 12

    A continue statement skips the rest of the current loop iteration. Trace this pseudocode and write the output: for n = 1 to 5: if n == 3: continue print(n)

    The output is: 1 2 4 5 When n equals 3, the continue statement skips the print statement for that iteration.
  13. 13

    The flowchart starts with score. If score is at least 90, it outputs A. Otherwise, if score is at least 80, it outputs B. Otherwise, if score is at least 70, it outputs C. Otherwise, it outputs Needs improvement. Write equivalent pseudocode.

    Use an if, else if, else if, else structure.

    One correct answer is: if score >= 90: print("A") else if score >= 80: print("B") else if score >= 70: print("C") else: print("Needs improvement") This checks the score ranges in order from highest to lowest.
  14. 14

    Write pseudocode that counts how many numbers in this list are greater than 50: scores = [72, 45, 88, 50, 39, 91]

    One correct answer is: count = 0 for score in scores: if score > 50: count = count + 1 print(count) The printed count is 3 because 72, 88, and 91 are greater than 50.
  15. 15

    The diagram shows a 3 by 4 grid of squares. Write nested-loop pseudocode that would visit every square once and print its row and column.

    Use one loop for rows and another loop inside it for columns.

    One correct answer is: for row = 1 to 3: for column = 1 to 4: print(row, column) This visits each of the 3 rows and all 4 columns in each row, for 12 total squares.
LivePhysics™.com CS - Grade 9-12 - Answer Key