If-then logic and loops using "repeat-until" are key parts of any computer language. In particular, these allow the construction of games with goals and scoring.
Control : If-then and Repeat-until
Objectives
-
Introduce If-then logic and loops.
Materials
-
Per Student Pair:
1 computer per two students (one computer per student is better) -
Per Class:
computer with projector or SMART board.
Key Questions
- What happens when you run the logic in step 6? (Nothing, because the computer only checks for the condition once, and then moves on.)
- What happens when you run the logic in step 7? (When you press the space key, the sprite moves up and to the right.)
What To Do
Preparation:
Ensure that all the computers work, and are connected to the internet.
Set up the instructor computer and projector or SMART board. Log in to the Scratch website.
Acitvity:
Instructor
- Explain that the next goal will be to control a sprite using the arrow keys.
- On the screen or SMART Board, open a new scratch project and select the Control tab.
- Explain that an “if-then” block will do what is inside it, as long as the condition in the block is true.
- Select the sensing tab, and show students where to find the “key pressed” block.
- Have students follow along on their own computers as you assemble this logic:
- Have students run the program. What happens?
- Have students follow along on their own computers and add in a “forever” block like this:
- Have students run the program. What happens?
- (optional) Create this logic; it has the same result as the logic in step 7.
Students:
- Create a program where a sprite is controlled by the four arrow keys on the keyboard. Pressing the arrow should move the sprite in the corresponding direction.
Modification: Use the “repeat-until” block and the “touching_?” block to change your program so that the sprite changes when it bumps into another sprite
Extensions
- A possible solution to the student challenge is posted at https://scratch.mit.edu/projects/95446588/