Back to Student Worksheet
CS Grade 6-8 Answer Key

CS: Loops and Conditional Statements

Using repetition and decisions in programs

Answer Key
Name:
Date:
Score: / 12

CS: Loops and Conditional Statements

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.

    Make a small table with the value of count before each print statement.

    The output is 1, 2, 3, and 4 on separate lines because the loop prints count each time and stops after count becomes 5.
  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.

    A correct answer is: if number % 2 == 0, print Even; else, print Odd. The condition checks whether there is no remainder after dividing by 2.
  3. 3

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

    Add the value of i to total once for each loop pass.

    The final value of total is 15 because the loop adds 1 + 2 + 3 + 4 + 5.
  4. 4

    Explain the difference between a loop and a conditional statement.

    A loop repeats a block of code multiple times, while a conditional statement chooses which block of code to run based on whether a condition is true or false.
  5. 5

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

    Check whether the loop condition can ever become false.

    The bug is that x never changes inside the loop, so the loop may run forever. A fix is to add x = x + 1 inside the loop after the print statement.
  6. 6

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

    A correct answer is: for number from 10 down to 6, print number. This loop starts at 10 and decreases by 1 until it prints 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).

    Check the conditions in order from top to bottom.

    The output is B because 82 is not at least 90, but it is at least 80.
  8. 8

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

    A correct answer is: score = 0; repeat 6 times: score = score + 10. After the loop, the score is 60.
  9. 9

    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).

    First check the age condition, then check the ticket condition only if the age condition is true.

    The program prints Enter because age is at least 12 and hasTicket is true.
  10. 10

    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.

    A while loop is better because the program does not know ahead of time how many tries the user will need.
  11. 11

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

    Include 32 in the Freezing condition.

    A correct answer is: if temperature <= 32, print Freezing; else, print Not freezing. The condition checks whether the temperature is 32 or lower.
  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.

    The final value of x is 9 because the loop only adds the even values of i, which are 2 and 4, to the starting value 3.
LivePhysics™.com CS - Grade 6-8 - Answer Key