Are you looking for a fun, free coding activity that you can do with your class? CodeMonkey has you covered in Offline Coding Activities. You don’t need any coding experience to carry-out this activity – simply read the following exercise and then do it tomorrow in class! This week’s 15 minute-long activity will help students understand how until loops work in programming.
The following activity facilitates the understanding of the until loop. Until Loops repeat an action until a condition changes.
Some of our favorite childhood games are based on doing an action until the condition changes. For example, in freeze dance, you dance until the music stops and then you freeze. These activities reflect how the “until” loop works. Choose an activity to play according to your students’ age group and the size of your classroom. Two possible games are musical chairs or freeze dance. You are welcome to choose a different game.
Have your students play the game you selected. After the activity, talk to them and try to simplify the actions they took. For example, if you picked freeze dance, then the students danced during the music and when the music stopped, they froze. Write this on the board.
Ask your students to identify the types of loops they have learned. Have a brief discussion about simple loops (a sequence of instructions that repeats a specified number of times) and “for” loops (performs a set of actions on all objects in a collection).
In this lesson, we will use a new kind of loop: the “until” loop. This loop is very useful but also dangerous when used carelessly. It is useful because it helps us when we have a repetitive task to be done, but it could be dangerous to use because if we are not careful, it might go on forever.
The “until” loop contains a block of code that will repeat until “something happens”, or more accurately, until a specific condition is met. This condition is called a control expression or loop condition. The computer checks the condition at every repetition. If the answer is false, the loop will keep going. It will only stop once the answer is true. If we do not pay attention, we might give the “until” loop a condition that will not be met. This will cause the loop to keep going forever and could even cause the program to crash.
If we go back to our activity at the beginning of the lesson, we can now write it in pseudo-code as an “until” loop:
until music stopped
Observe that dance has an indentation because it is inside the loop, but freeze does not have an indentation because it is only carried out after after the loop ended. Ask your students, “What is the control expression for this ‘until’ loop?”
Answer: Music stopped. As long as we heard music, we kept dancing. We can translate this to computer language: the computer kept asking, “Did the music stop?” and kept getting a false answer as long as there was music playing. Once the computer asked, “Did the music stop?” and the answer was true, we stopped dancing. The control expression we will use in this lesson is the function “near”. The value returned by the function “near” will determine when the “until” loop will stop executing. Ask your students if they can think of other examples from everyday life that repeat until a certain condition is met (or until “something happens”), and write them in pseudo-code. Examples for possible answers:
until cream is solid
until time to sleep
watch favorite tv show
go to sleep
This offline coding activity is from Lesson 20 in Coding Adventure Part 2: Functions and Conditions. For more fun activities and a detailed curriculum guide on how to teach CodeMonkey, sign-up for our free teacher trial here.