Download presentation
Presentation is loading. Please wait.
Published byDana Harris Modified over 9 years ago
1
Branching and Looping Examples, cont’d
2
Remember the generic triple jump world…
4
Let’s change this to be interactive.
6
User input functions in Alice ask user for a number ask user for yes or no ask user for a string
8
Start Alice and open genericTripleJump Look at world. my first method Note that the statements there form a linear sequence. We want to modify this algorithm.
10
The first step is to add an If/Else instruction to the method. Drag a copy of the If/Else tile from the bottom of the editor area. Drop it into the method just below the three jump instructions.
11
Adding If/Else instructions When adding an If/Else, you will be prompted to choose between true “and” “false” as an initial value for the condition. This value is merely a placeholder. We will always replace this value with a Boolean expression of a condition we want to test.
12
Replace the placeholder condition in the If/Else instruction with a function. We need the function that will allow us to ask the user a yes/no question.
18
We want Alice to jump only if the user answers yes to our query. Need to move the instruction that causes Alice to jump to the If clause of our If/Else instruction.
33
genericTripleJumpWhile.a2w genericTripleJumpLoop.a2w
34
A Sentinel Sailing Loop Sailboat will turn to face the object Will sail one meter forward This process continues until the sailboat is within 5 meters of the object While (NOT (the sailboat is within 5 meters of the [object])) { turn to face [object] move forward 1 meter }
35
A Sentinel Sailing Loop Let’s create a sail to method that will work with any object. It should accept the target object as an input parameter. This will be similar to the way the primitive move method accepts direction and amount
43
A Sentinel Sailing Loop Sailboat will turn to face the object Will sail one meter forward This process continues until the sailboat is within 5 meters of the object While (NOT (the sailboat is within 5 meters of the [object])) { turn to face [object] move forward 1 meter }
50
A Sentinel Sailing Loop Sailboat will turn to face the object Will sail one meter forward This process continues until the sailboat is within 5 meters of the object While (NOT (the sailboat is within 5 meters of the [object])) { turn to face [object] move forward 1 meter }
53
A Sentinel Sailing Loop Sailboat will turn to face the object Will sail one meter forward This process continues until the sailboat is within 5 meters of the object While (NOT (the sailboat is within 5 meters of the [object])) { turn to face [object] move forward 1 meter }
57
Let’s generalize sailTo so that we can sail to any object.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.