CS: Loops and Control Flow
Tracing, writing, and reasoning about loops and decisions
Tracing, writing, and reasoning about loops and decisions
CS - Grade 9-12
- 1
Trace this pseudocode and write the final output: x = 0 for i = 1 to 4: x = x + i print(x)
- 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
Explain the difference between a for loop and a while loop. Give one situation where each type is a good choice.
- 4
Write pseudocode that prints all even numbers from 2 through 20, including 2 and 20.
- 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
Trace this code and write the final output: for i = 1 to 3: for j = 1 to 2: print(i, j)
- 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
Write pseudocode that asks a user for a number until they enter a positive number. Then print "Accepted".
- 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
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
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
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
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
Write pseudocode that counts how many numbers in this list are greater than 50: scores = [72, 45, 88, 50, 39, 91]
- 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.
More CS Worksheets
CS: Algorithms and Flowcharts
Grade 6-8 · 12 problems
CS: Arrays and Lists
Grade 9-12 · 12 problems
CS: Big-O Notation and Algorithm Efficiency
Grade 9-12 · 12 problems
CS: Binary Numbers and Number Systems
Grade 6-8 · 12 problems
More Grade 9-12 Worksheets
Linear Equations
Math · 8 problems
Cell Biology
Biology · 8 problems
Reading Comprehension
Language Arts · 8 problems
Historical Thinking & Evidence
Social Studies · 8 problems