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 Boolean Values True or False? –Boolean values are abstract concepts used in computations? TRUE!

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

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

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 Penguin falls down into the hole Flowchart

9 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Decision Structure 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 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!

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

15 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Nested If/Else Instructions 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 Tutorial 4-2 called Creating a single alternative decision structure And….. Create Tutorial 4-3 on page 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

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

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

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


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