Branching and Looping Examples, cont’d. Remember the generic triple jump world…
-
Upload
dana-harris -
Category
Documents
-
view
219 -
download
1
Transcript of Branching and Looping Examples, cont’d. Remember the generic triple jump world…
![Page 1: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/1.jpg)
Branching and Looping Examples, cont’d
![Page 2: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/2.jpg)
Remember the generic triple jump world…
![Page 3: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/3.jpg)
![Page 4: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/4.jpg)
Let’s change this to be interactive.
![Page 5: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/5.jpg)
![Page 6: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/6.jpg)
User input functions in Alice
• ask user for a number• ask user for yes or no• ask user for a string
![Page 7: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/7.jpg)
![Page 8: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/8.jpg)
• 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.
![Page 9: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/9.jpg)
![Page 10: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/10.jpg)
• 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.
![Page 11: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/11.jpg)
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.
![Page 12: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/12.jpg)
• 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.
![Page 13: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/13.jpg)
![Page 14: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/14.jpg)
![Page 15: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/15.jpg)
![Page 16: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/16.jpg)
![Page 17: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/17.jpg)
![Page 18: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/18.jpg)
• 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.
![Page 19: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/19.jpg)
![Page 20: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/20.jpg)
![Page 21: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/21.jpg)
![Page 22: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/22.jpg)
![Page 23: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/23.jpg)
![Page 24: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/24.jpg)
![Page 25: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/25.jpg)
![Page 26: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/26.jpg)
![Page 27: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/27.jpg)
![Page 28: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/28.jpg)
![Page 29: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/29.jpg)
![Page 30: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/30.jpg)
![Page 31: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/31.jpg)
![Page 32: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/32.jpg)
![Page 33: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/33.jpg)
• genericTripleJumpWhile.a2w
• genericTripleJumpLoop.a2w
![Page 34: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/34.jpg)
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
}
![Page 35: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/35.jpg)
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
![Page 36: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/36.jpg)
![Page 37: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/37.jpg)
![Page 38: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/38.jpg)
![Page 39: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/39.jpg)
![Page 40: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/40.jpg)
![Page 41: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/41.jpg)
![Page 42: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/42.jpg)
![Page 43: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/43.jpg)
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
}
![Page 44: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/44.jpg)
![Page 45: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/45.jpg)
![Page 46: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/46.jpg)
![Page 47: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/47.jpg)
![Page 48: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/48.jpg)
![Page 49: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/49.jpg)
![Page 50: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/50.jpg)
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
}
![Page 51: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/51.jpg)
![Page 52: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/52.jpg)
![Page 53: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/53.jpg)
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
}
![Page 54: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/54.jpg)
![Page 55: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/55.jpg)
![Page 56: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/56.jpg)
![Page 57: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/57.jpg)
Let’s generalize sailTo so that we can sail to any object.
![Page 58: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/58.jpg)
![Page 59: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/59.jpg)
![Page 60: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/60.jpg)
![Page 61: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/61.jpg)
![Page 62: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/62.jpg)
![Page 63: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/63.jpg)
![Page 64: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/64.jpg)
![Page 65: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/65.jpg)
![Page 66: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/66.jpg)
![Page 67: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/67.jpg)
![Page 68: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/68.jpg)
![Page 69: Branching and Looping Examples, cont’d. Remember the generic triple jump world…](https://reader037.fdocuments.net/reader037/viewer/2022110209/56649e315503460f94b22a61/html5/thumbnails/69.jpg)