# Decision Structures Chapter 4. Chapter 4 Objectives To understand: o What values can be stored in a Boolean variable o What sequence structures are and.

## Presentation on theme: "Decision Structures Chapter 4. Chapter 4 Objectives To understand: o What values can be stored in a Boolean variable o What sequence structures are and."— Presentation transcript:

Decision Structures Chapter 4

Chapter 4 Objectives To understand: o What values can be stored in a Boolean variable o What sequence structures are and when used o What decision structures are and when used o The difference between dual-alternative and single- alternative decision structures o What nested instructions are o What relational operators are and how they are used 4-2

Boolean Variables Boolean Variables hold only two values o True o False o Initial Value is usually set to be “TRUE” What it looks like in variable definitions 4-3 4.1

Boolean Functions Boolean Functions return one value from the choice of o True o False Also primitive functions that are Boolean o User prompted to answer Yes (true) or No (false) Ask the following question: 4-4 4.1

If/Else Decision Structure Used to check a condition and make a decision If a Boolean condition is o True then one set of instructions are executed o False then a different set is executed All previous instructions have been consecutively executed o One o After o The o Other 4-5 4.2

If/Else Flow Chart Used to check a condition and make a decision

Storyboard A storyboard design for this conditional statement is: BOTH actions will NOT be performed…only ONE can be! The penguin falling or speaking are instructions that are conditionally executed o They do NOT always execute o Executed only under certain conditions If the hole is wider than the penguin penguin falls down the hole Else penguin says “Drats!” End If TRUE FALSE

Decision Structure 4-8 4.2 Penguin falls down into the hole Flowchart

If/Else Statement Location 4-9 4.2 If/Else Tile

Using If/Else Instruction Two empty “slots” in instruction o IF part o ELSE part Choose placeholder or true/false after dragging to location Replace placeholder with Boolean variable or a Boolean function Instructions tiles added to both the IF part and ELSE part More than one instruction can be added to both the IF and ELSE part 4-10 Empty If/Else instruction 4.2

Single-Alternative Decision Structures If/Else is a dual-alternative decision structure o Two paths of execution  One following TRUE  Other following FALSE Single-alternative decision structures are similar to the dual-alternative o The ELSE part is empty 4-11 4.2

Single-Alternative Decision Structures 4-12 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

Nested If/Else Instructions When an If/Else instruction is placed inside another If/Else instruction The inner If/Else executes only if the outer If/Else is true 4-13 4.2

Nested If/Else Instructions 4-14 4.2 Start Penguin faces hole Within 2 meters “Too Far!” True Hole wider? “Drats!”Penguin walks Penguin falls in End True False

Relational Comparisons  Relational Operators are used to compare values and determine whether relationships exist  Greater than  Less than  Equal to  Compare two values and determine how they relate to each other OperatorMeaning = Equal to ! =Not equal to >Greater than > =Greater than or equal to { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/3599135/slides/slide_15.jpg", "name": "Relational Comparisons  Relational Operators are used to compare values and determine whether relationships exist  Greater than  Less than  Equal to  Compare two values and determine how they relate to each other OperatorMeaning = Equal to .", "description": "=Not equal to >Greater than > =Greater than or equal to

Relational Operators  Used to write your own comparison  In If/Else and other tests  Operates on two pieces of data  a and b  Alice uses “a” and “b” for placeholders Drag the desired tile and replace either the “a” or “b” with a value 4-16 4.3

Demonstration in Robot Example Concept illustrated o Relational operations are defined using relational operators

Boolean Logic Can check for multiple conditions at one time with Boolean logic Boolean logic operators are used to build an expression composed of multiple conditions o And  Boolean: A and B: both must be true to be true  In Alice: Both A and B o Or  Boolean: A or B: one or both must be true to be true  In Alice: Either A or B, or both o Not  Inverses what you feed it Example: if A = True then “not A” is False  In Alice: not A

Boolean Logic in Functions Choose World object Then choose functions o Select desired Boolean function o Drag to conditional instruction like If/Else Condition

Logical Operators Tests more than true/false…can do complex testing! o Test two conditions to see if they BOTH are true! o Or ONLY one condition is true! o Or NEITHER condition is true! 4-20 4.3

Example Can combine multiple conditions Example: o Penguin within 2 meters o And circle is wider than penguin Similar to nested If/Else but does not do what is in second Else statement o Penquin say “too far away“ 4-21

Testing Value of Object’s Property Sometimes you might want to see what a property of an object is o Color o Opacity o isShowing 4-22

How to Testing Object’s Property Create an empty If/Else instruction Select object to test Click properties tab Drag desired property’s tile and drop onto If/Else instruction’s condition Menu appears showing all comparisons to perform o Select desired comparison Another menu appears allowing you to select value that you want to compare property to o Ex: color property will give list of colors 4-23

Storyboard Frog and ladybug are in garden Ladybug says “Excuse me” Frog turns to face ladybug If ladybug’s color is red o Frog says “EEK! Go away!” Else o Frog says “Good Morning” Endif 4-24

Property Test Demonstration 4-25

Homework Read chapter 4 Answer questions in handout Do lab assignments after handing in answered questions Due one week after assigned with 1 week grace

Download ppt "Decision Structures Chapter 4. Chapter 4 Objectives To understand: o What values can be stored in a Boolean variable o What sequence structures are and."

Similar presentations