Presentation is loading. Please wait.

Presentation is loading. Please wait.

Institute for Personal Robots in Education (IPRE)‏ CSC 170 Computing: Science and Creativity.

Similar presentations


Presentation on theme: "Institute for Personal Robots in Education (IPRE)‏ CSC 170 Computing: Science and Creativity."— Presentation transcript:

1 Institute for Personal Robots in Education (IPRE)‏ CSC 170 Computing: Science and Creativity

2 The code we’ve considered executed all statements sequentially Correct steps in the correct order is our first step to programming We often need our programs to choose what to do based on changing situations Guarded Action pattern: Only do it if a Boolean expression is true Alternative selection: Choose one action or another CONDITIONALS: TO DO OR TO DO 2

3 3 Pattern: Guarded Action Problem: Execute an action only under certain conditions General if (true-or-false-condition is true) Form execute a set of blocks CodeExample: THE GUARDED ACTION PATTERN

4 FLOWCHART VIEW OF GUARDED ACTION After the boolean expression of the if statement evaluates, the true- part executes only when the boolean expression is true. boolean expression False block-1 block-n True Part

5 A boolean-expression a.k.a. predicate in Snap! is any expression that evaluates to true or false Snap! Has 3 predicate blocks Snap has 3 boolean operators: and or, not BOOLEAN EXPRESSIONS (PREDICATES) 5

6 COMPLETE TRUTH TABLE 6

7 Situations arise that require a program to select between one set of actions or another Withdraw or deposit money Pass or fail the entrance requirements This is the Alternative Action Pattern Choose between two alternate sets of actions ALTERNATIVE SELECTION 7

8 ALTERNATIVE ACTION Pattern: Alternative Action Problem: Must choose one action from 2 alternatives Outline: if (true-or-false-condition is true) execute action-1 execute action-1 else else execute action-2 execute action-2CodeExample:

9 FLOW CHART VIEW OF ALTERNATIVE ACTION 9 logical expression False block-1 block-n block-1 block-n True False Part True Part

10 We often need our programs to perform things repeatedly “Forever” Keep it going pattern A specific number of times, when we already know Determinate loop pattern Until something goal happens to stop the looping Indeterminate loop pattern LOOPING: RUN THE SAME TASKS REPEATEDLY 10

11 Pattern Name: Forever Loop Problem: Need to do something happening while the program is running Solution: SNAP ! forever block FOREVER LOOPS 11

12 Pattern Name: Determinate Loop Problem: Need to do something exactly n times, where n is already known Solution: Snap ! repeat or for blocks DETERMINATE LOOPS 12 // Java code (not on the test) Turtle turtle = new Turtle(0, 0); int n = 5; repeat { turtle.move(100); turtle.turnLeft(72); n = n - 1; } until (n == 1);

13 This for block is provided when a changing variable ( length below) is needed DETERMINATE LOOPS WITH THE FOR BLOCK 13

14 In the current version of SNAP !, the for block isn't installed automatically You will need to import it once for each new SNAP ! project in which you use it To do so, click on the icon at the top of the screen and select Import Tools NEED TO IMPORT TOOLS FOR THE FOR LOOP 14

15 Take one minute to write down what this script “says” ACTIVE LEARNING 15

16 Pattern Name: Indeterminate Loop Problem: Need to do something repeatedly until a goal is reached Code Demo: Ask the user for a even number in a given range Will use a repeat until block Will use a custom made block to abstract a complex boolean expression INDETERMINATE LOOPS 16


Download ppt "Institute for Personal Robots in Education (IPRE)‏ CSC 170 Computing: Science and Creativity."

Similar presentations


Ads by Google