Download presentation
Presentation is loading. Please wait.
Published byXzavier Wainscott Modified over 9 years ago
1
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition by Tony Gaddis Chapter 4: Decision Structures
2
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Boolean Values True or False? –Boolean values are abstract concepts used in computations? TRUE! 4-2 4.1
3
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Boolean Variables Boolean Variables hold only two values True False Initial Value is usually set to be “TRUE” 4-3 4.1
4
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Boolean Functions Boolean Functions return one value from the choice of True False Also primitive functions that are Boolean User prompted to answer Yes (true) or No (false) 4-4 4.1
5
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The If/Else Decision Structure If/Else decision executes one set of instructions if a Boolean condition is true and a different set if the condition is false All previous instructions have been consecutive (sequence structure), that is: One After The Other 4-5 4.2
6
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley If/Else If/Else is a decision structure Instruction tests a condition (which gives a Boolean value) and then based on a true value does task “A” or task “B” if the value was false If the hole is wider than the penguin penguin falls down the hole Else penguin says “Drats!” End If 4-6 TRUE FALSE 4.2
7
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley If/Else BOTH actions will NOT be performed…only ONE can be! The penguin falling or speaking are instructions that are conditionally executed –They do NOT always execute –Executed only under certain conditions If the hole is wider than the penguin penguin falls down the hole Else penguin says “Drats!” End If 4-7 TRUE FALSE 4.2
8
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Decision Structure 4-8 4.2 Penguin falls down into the hole Flowchart
9
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Decision Structure 4-9 4.2 If/Else Tile
10
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley If/Else Note to empty “slots” in instruction. –IF part –ELSE part The TRUE placeholder is replaced with the Boolean variable or a Boolean function Instructions tiles added to both the IF part and the ELSE part More than one instruction can be added to both the IF and ELSE part! 4-10 Empty If/Else instruction 4.2
11
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Task…. Complete tutorial 4-1 on page 154-157 over Creating an If/Else instruction within a programming environment. 4-11
12
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Single-Alternative Decision Structures If/Else is a dual-alternative decision structure Two paths of execution: one following TRUE and the other following FALSE Single-alternative decision structures are similar to the dual-alternative The ELSE part is empty! 4-12 4.2
13
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Single-Alternative Decision Structures 4-13 Start Penguin turns to face hole Hole wider than penguin? End Penguin walks to center of hole Penguin falls into hole 4.2 T F
14
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Nested If/Else Instructions An If/Else instruction is placed inside another If/Else instruction or nested. The inner If/Else executes only if the outer If/Else is true. 4-14 4.2
15
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Nested If/Else Instructions 4-15 4.2 Start Penguin faces hole Within 2 meters “Too Far!” True Hole wider? “Drats!”Penguin walks Penguin falls in End True False
16
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Task #2 Complete the tutorial on page 160-163 Tutorial 4-2 called Creating a single alternative decision structure And….. Create Tutorial 4-3 on page 165-171 called Creating nested If/Else instructions. 4-16
17
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Relational Comparisons Relational Operators used to compare values and determine whether relationships such as greater than, less than, or equal to exist Compare two values and determine how they relate to each other OperatorMeaning = Equal to ! =Not equal to >Greater than > =Greater than or equal to <Less than < =Less than or equal to 4-17 4.3
18
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Relational Operators Binary…operate on two pieces of data Can use to create conditions in an If/Else instruction Alice uses “a” and “b” for placeholders –Drag the desired tile and replace either the “a” or “b” with a value…such as an object 4-18 4.3
19
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Task #3 Create and utilize relational operators in tutorial 4-4 called using relational operators on pages 173-176 And….. Complete tutorial 4-5 on page 177 called testing and objects color property 4-19
20
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Logical Operators Tests more than true/false…can do complex testing! –Test two conditions to see if they BOTH are true! –Or ONLY one condition is true! –Or NEITHER condition is true! 4-20 4.3
21
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Skills Task….. Students should complete the following skills programs(choose 2 of the 3) valued at 50 points each 2,4,5 Students should complete the following challenge programs that will be worth 100 points each. 6 and 8 4-21
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.