Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Branching and Looping Examples, cont’d. Remember the generic triple jump world…"— Presentation transcript:

1 Branching and Looping Examples, cont’d

2 Remember the generic triple jump world…

3

4 Let’s change this to be interactive.

5

6 User input functions in Alice ask user for a number ask user for yes or no ask user for a string

7

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.

9

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.

13

14

15

16

17

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.

19

20

21

22

23

24

25

26

27

28

29

30

31

32

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

36

37

38

39

40

41

42

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 }

44

45

46

47

48

49

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 }

51

52

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 }

54

55

56

57 Let’s generalize sailTo so that we can sail to any object.

58

59

60

61

62

63

64

65

66

67

68

69


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

Similar presentations


Ads by Google