1 / 69

Branching and Looping Examples, cont’d

Branching and Looping Examples, cont’d. Remember the generic triple jump world…. Let’s change this to be interactive. User input functions in Alice. ask user for a number ask user for yes or no ask user for a string. Start Alice and open genericTripleJump Look at world. my first method

deiter
Download Presentation

Branching and Looping Examples, cont’d

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Branching and Looping Examples, cont’d

  2. Remember the generic triple jump world…

  3. Let’s change this to be interactive.

  4. User input functions in Alice • ask user for a number • ask user for yes or no • ask user for a string

  5. 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.

  6. 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.

  7. 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.

  8. 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.

  9. 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.

  10. genericTripleJumpWhile.a2w • genericTripleJumpLoop.a2w

  11. 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 }

  12. 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

  13. 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 }

  14. 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 }

More Related