Sign in to save

Bookmark this page so you can find it later.

Sign in to save

Bookmark this page so you can find it later.

CS Grade 9-12

CS: Loops and Control Flow

Tracing, writing, and reasoning about loops and decisions

View Answer Key
Name:
Date:
Score: / 15

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)

  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)

  3. 3

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

  4. 4

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

  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

  6. 6

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

  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.

  8. 8

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

  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

  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)

  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)

  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)

  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.

  14. 14

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

  15. 15
    A three-row by four-column grid with arrows showing row-by-row traversal.

    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.

LivePhysics™.com CS - Grade 9-12

More CS Worksheets

See all CS worksheets

More Grade 9-12 Worksheets

See all Grade 9-12 worksheets