Branching and Looping Examples, cont’d. Remember the generic triple jump world…

69
Branching and Looping Examples, cont’d

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…

Branching and Looping Examples, cont’d

Page 2: Branching and Looping Examples, cont’d. Remember the generic triple jump world…

Remember the generic triple jump world…

Page 3: Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Page 4: Branching and Looping Examples, cont’d. Remember the generic triple jump world…

Let’s change this to be interactive.

Page 5: Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Page 6: Branching and Looping Examples, cont’d. Remember the generic triple jump world…

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…
Page 8: Branching and Looping Examples, cont’d. Remember the generic triple jump world…

• 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…
Page 10: Branching and Looping Examples, cont’d. Remember the generic triple jump world…

• 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…

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…

• 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…
Page 14: Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Page 15: Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Page 16: Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Page 17: Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Page 18: Branching and Looping Examples, cont’d. Remember the generic triple jump world…

• 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…
Page 20: Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Page 21: Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Page 22: Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Page 23: Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Page 24: Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Page 25: Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Page 26: Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Page 27: Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Page 28: Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Page 29: Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Page 30: Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Page 31: Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Page 32: Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Page 33: Branching and Looping Examples, cont’d. Remember the generic triple jump world…

• genericTripleJumpWhile.a2w

• genericTripleJumpLoop.a2w

Page 34: Branching and Looping Examples, cont’d. Remember the generic triple jump world…

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…

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…
Page 37: Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Page 38: Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Page 39: Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Page 40: Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Page 41: Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Page 42: Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Page 43: Branching and Looping Examples, cont’d. Remember the generic triple jump world…

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…
Page 45: Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Page 46: Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Page 47: Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Page 48: Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Page 49: Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Page 50: Branching and Looping Examples, cont’d. Remember the generic triple jump world…

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…
Page 52: Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Page 53: Branching and Looping Examples, cont’d. Remember the generic triple jump world…

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…
Page 55: Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Page 56: Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Page 57: Branching and Looping Examples, cont’d. Remember the generic triple jump world…

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…
Page 59: Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Page 60: Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Page 61: Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Page 62: Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Page 63: Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Page 64: Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Page 65: Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Page 66: Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Page 67: Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Page 68: Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Page 69: Branching and Looping Examples, cont’d. Remember the generic triple jump world…