Back to Student Worksheet
CS Grade 6-8 Answer Key

CS: Block-Based Programming: Events and Conditionals

Using events to start code and conditions to choose actions

Answer Key
Name:
Date:
Score: / 15

CS: Block-Based Programming: Events and Conditionals

Using events to start code and conditions to choose actions

CS - Grade 6-8

Instructions: Read each problem carefully. Use clear explanations and write block steps in order when asked.
  1. 1

    In block-based programming, what is an event? Give one example of an event that could start a script.

    Think about what starts a set of blocks.

    An event is something that happens and causes a script to run. One example is when the green flag is clicked, when a key is pressed, or when a sprite is clicked.
  2. 2

    A sprite should jump when the player presses the space key. Which event block would best start this script, and what action block could come after it?

    The script should start with an event block such as 'when space key pressed'. An action block such as 'change y by 20' could come after it to make the sprite jump upward.
  3. 3

    A program has this condition: if score > 10, then show the message 'Level up'. If the score is 8, will the message appear? Explain.

    Compare the current value of score to the number in the condition.

    The message will not appear because 8 is not greater than 10. The condition is false, so the blocks inside the if block do not run.
  4. 4

    Write the logic for an if-else block that changes a sprite to the 'happy' costume if it is touching a star, and changes it to the 'normal' costume otherwise.

    The logic is: if the sprite is touching a star, switch to the happy costume. Else, switch to the normal costume.
  5. 5

    A script says: when green flag clicked, forever, if right arrow key pressed, change x by 5. Describe what the sprite does while the program runs.

    The forever block makes the program keep checking again and again.

    The sprite keeps checking whether the right arrow key is pressed. Whenever the key is pressed, the sprite moves 5 steps to the right by increasing its x position.
  6. 6

    A student puts this code under when green flag clicked: if touching edge, then bounce. The sprite only checks once at the start. What block should the student add so the sprite keeps checking for the edge?

    The student should put the if touching edge block inside a forever loop. That way, the sprite keeps checking during the whole program.
  7. 7

    A sprite has two scripts. One script starts when the sprite is clicked and plays a sound. Another script starts when the up arrow key is pressed and moves the sprite up. Can both scripts be used in the same project? Explain.

    Different event blocks can start different scripts.

    Yes, both scripts can be used in the same project because each script listens for a different event. Clicking the sprite plays the sound, and pressing the up arrow moves the sprite.
  8. 8

    A weather app uses conditionals. If raining is true, it shows 'Bring an umbrella'. Else, it shows 'No umbrella needed'. What message appears when raining is false?

    The message 'No umbrella needed' appears because the condition raining is true is false, so the program runs the else part.
  9. 9

    Design a short block-based plan for collecting a coin. When the player touches the coin, the score should increase by 1 and the coin should hide.

    The program needs to keep checking for the touching condition.

    A correct plan is: when green flag clicked, forever check if the player is touching the coin. If the condition is true, change score by 1 and hide the coin.
  10. 10

    A game has lives = 3. The code says: if lives = 0, then broadcast game over, else keep playing. What happens when lives equals 3?

    The game keeps playing because lives equals 3, not 0. The condition is false, so the else branch runs.
  11. 11

    A condition says: if touching enemy and shieldOn = false, then lose 1 life. The player is touching an enemy, but shieldOn = true. Does the player lose a life? Explain.

    For and, every part of the condition must be true.

    The player does not lose a life because both parts of an and condition must be true. The player is touching the enemy, but shieldOn = false is not true.
  12. 12

    A start button sprite should begin the game when clicked. Write two actions that could happen after the event 'when this sprite clicked'.

    After the start button is clicked, the program could set gameStarted to true and hide the start button. It could also broadcast a start game message to other sprites.
  13. 13

    Explain the difference between an if block and an if-else block.

    Think about whether the program has one possible action or two possible paths.

    An if block runs its inside blocks only when a condition is true. An if-else block chooses between two paths: one path runs when the condition is true, and the other path runs when the condition is false.
  14. 14

    A maze game uses this logic: when green flag clicked, forever, if touching wall, go back to start. Why is the if block placed inside the forever loop?

    The if block is inside the forever loop so the game keeps checking whether the player is touching a wall. If it checked only once, the player could touch a wall later without being sent back.
  15. 15

    A sprite should stop moving right when its x position is greater than 220. Write a conditional rule that would help control this movement.

    Use the x position to decide whether movement is still allowed.

    A correct rule is: if x position is less than or equal to 220, then change x by a positive amount. Otherwise, do not move right or set the x position to 220.
LivePhysics™.com CS - Grade 6-8 - Answer Key