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 6-8

CS: Loops and Conditional Statements

Using repetition and decisions in programs

View Answer Key

Practice tracing loops, choosing conditional statements, and explaining how programs make decisions.

Read each problem carefully. Show your work in the space provided.

Name:
Date:
Score: / 12

Using repetition and decisions in programs

CS - Grade 6-8

Instructions: Read each problem carefully. Show your work in the space provided.
  1. 1

    Trace this code and write the output: count = 1; while count <= 4: print(count); count = count + 1.

  2. 2

    A program should print Even if a number is divisible by 2 and Odd if it is not. Write an if-else statement in pseudocode for this task.

  3. 3

    Trace this for loop and write the final value of total: total = 0; for i from 1 to 5: total = total + i.

  4. 4
    Diagram showing a repeated loop path beside a branching conditional path.

    Explain the difference between a loop and a conditional statement.

  5. 5
    Diagram of an endless loop caused by a missing update step.

    Find the bug in this code and explain how to fix it: x = 1; while x < 10: print(x).

  6. 6

    Write a loop in pseudocode that prints the numbers 10, 9, 8, 7, and 6.

  7. 7

    Trace this code and write the output: score = 82; if score >= 90: print(A); else if score >= 80: print(B); else: print(C).

  8. 8
    Game character collecting coins repeatedly to add points.

    A game gives 10 points for each coin collected. Write pseudocode that uses a loop to add points for 6 coins.

  9. 9
    Nested decision diagram with age and ticket choices leading to different outcomes.

    Trace this nested decision and write what is printed: age = 13; hasTicket = true; if age >= 12: if hasTicket == true: print(Enter); else: print(Buy ticket); else: print(Too young).

  10. 10
    Diagram showing repeated password attempts until the lock opens.

    Choose whether a for loop or while loop is better for this task and explain why: keep asking for a password until the user enters the correct password.

  11. 11

    Write an if-else statement in pseudocode that prints Freezing if temperature is 32 or below, and Not freezing otherwise.

  12. 12

    Trace this code and write the final value of x: x = 3; for i from 1 to 4: if i % 2 == 0: x = x + i.

LivePhysics™.com CS - Grade 6-8

More CS Worksheets

See all CS worksheets

More Grade 6-8 Worksheets

See all Grade 6-8 worksheets