Summer Computing Workshop PROGRAMMING WITH SCRATCH Session 3 Conditional Branching Conditional branching is used to alter the normal flow of execution depending on the value of a Boolean expression The use of conditional branching allows programs to become much more flexible The primary block associated with this behavior is the if block The triangle shaped area is called the predicate or conditional part of the structure. This is what determines if the consequent (internal) blocks are executed The triangle shaped blocks in Scratch always represent a true or false value. This “typing” is not as definite in Scratch as in other languages, but it is there nonetheless and certainly prepares you for the much more strict typing of languages you will encounter in the future. Conditional Branching – If Block Let’s take a look at an example Starting at the top, the sprite will move 100 steps in whatever direction it’s pointing Then the computer asks itself the question “Does 2 equal 3?” Obviously it does not so the green comparison block evaluates to false Since the predicate is false, the turn block is skipped The normal flow of execution is then resumed and the second move block is encountered As a result, this script causes the sprite to move a total of 200 steps Conditional Branching – If Block What if we change the numbers so the comparison returns true? Since 2 does equal 2, the comparison returns true and the turn block is executed Then, as before, the normal flow of execution resumes and the remaining blocks beneath the if block are executed Therefore, this snippet of code moves the sprite 100 steps, takes a 90 degree right turn, and then travels 100 more steps Conditional Branching – If/Else Block The if block isn’t the only block capable of branching based on a condition in Scratch. The if/else block is equally, and sometimes even more, useful. The structure is very similar to the if block with the only difference being the addition of the “else” or alternate area The top part of the if/else block behaves the same way the if block does, but while the blocks in the consequent area are executed if the condition is true, the blocks in the alternate (else) area are executed only if the condition is false By definition, it is impossible for blocks contained in both the consequent and alternate areas to be executed. Once the decision is made, it is strictly one or the other Conditional Branching – If/Else Block Let’s look at an example of if/else block behavior The sprite moves 50 steps then reaches the if/else predicate The comparison evaluates to false since 5 is NOT less than 2 Due to the false value of the condition, the turn 45 block is skipped and the turn 135 block is executed instead To make the sprite movements more visible we can use the pen tool to draw a line as the sprite moves The Pen Tool To make the sprite movements more visible we can use the pen tool to draw a line as the sprite moves Click the pen category in the upper area of the blocks palette Drag the script block to the scripts area, but don’t link it to the existing Click the block, you should see a colored rectangle appear near the lock symbol in the sprite info area Now, whenever the sprite moves, it’s path will be marked by a line. The size and color of this line can be altered if you want, but the default settings will work for now After a bit of drawing, the stage can become quite colorful. When that happens, the block can be used to erase the lines Conditional Branching – User Input In the previous examples, the condition was unable to change. The comparison would evaluate to either true or false and stay that way forever because the relation of numbers never changes. Let’s modify the previous bit of code so the condition changes based on input from the user With this small change, the true flexibility of the if/else structure begins to emerge Now, every time the script is ran, a different branch can be taken. The number of choices is limited to two in this case, but through the power of nesting, this limitation is no longer an issue Conditional Branching – Nesting What if the question asked had more than 2 valid answers? A single if/else block is unable to handle a third result, and even if it could, there is no way to select it since all Boolean expressions give a true or false answer This problem is addressed with a technique know as nesting. By placing if/else blocks inside of other if/else blocks, powerful decision trees can be created to handle any number of situations On Your Own Create a program that asks the user which direction the sprite should move, then prompt for a distance to move in that direction Once you have completed the above project, modify it so the user is asked the questions twice in a row instead of just once (get direction, ask distance, move, ask direction, ask distance, move) Session 3 Questions 1. How might the flow of execution be affected by a conditional branch? 2. What part of an if/else control structure determines which sequence of blocks is executed? 3. The two different types of input are single-character input and ______ input. 4. Asking for a name is an example of what kind of input? 5. The expression in the predicate (conditional) part of an if/else structure evaluates to a _____ or _____ value. 6. What part of the structure would execute if the predicate value was false?