Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.

Similar presentations


Presentation on theme: "Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition."— Presentation transcript:

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 Chapter 4 Objectives To understand: what values can be stored in a Boolean variable what sequence structures are and when used what decision structures are and when used the difference between dual-alternative and single-alternative decision structures what nested instructions are what relational operators are and how they are used 4-2

3 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Today’s Agenda 1.Boolean Values 2.The If / Else Decision Structure 3.Nested If / Else Decision Structures 4.Relational Comparisons and Logical Operators 4-3

4 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-4 4.1

5 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-5 4.1

6 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-6 4.1

7 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-7 4.2

8 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-8 TRUE FALSE 4.2

9 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-9 TRUE FALSE 4.2

10 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Decision Structure 4-10 4.2 Penguin falls down into the hole Flowchart

11 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Decision Structure 4-11 4.2 If/Else Tile

12 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-12 Empty If/Else instruction 4.2

13 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-13 4.2

14 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Single-Alternative Decision Structures 4-14 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

15 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-15 4.2

16 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Nested If/Else Instructions 4-16 4.2 Start Penguin faces hole Within 2 meters “Too Far!” True Hole wider? “Drats!”Penguin walks Penguin falls in End True False

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 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-19 4.3


Download ppt "Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition."

Similar presentations


Ads by Google