1 / 26

# Decision Structures - PowerPoint PPT Presentation

Decision Structures. Chapter 4. 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

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about ' Decision Structures' - leandra-knapp

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### Decision Structures

Chapter 4

• 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

Boolean Variables

• Boolean Variables hold only two values

• True

• False

• Initial Value is usuallyset to be “TRUE”

• What it looks like in variable definitions

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)

If/Else Decision Structure

• Used to check a condition and make a decision

• If a Boolean condition is

• True then one set of instructions are executed

• False then a different set is executed

• All previous instructions have been consecutively executed

• One

• After

• The

• Other

• Used to check a condition and make a decision

• 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

• 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

TRUE

FALSE

Flowchart

Penguin falls down into the hole

Decision Structure

If/Else Tile

If/Else Statement Location

Empty If/Else instruction

Using If/Else Instruction

• Two empty “slots” in instruction

• IF part

• 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

Single-Alternative Decision Structures

• If/Else is a dual-alternative decision structure

• Two paths of execution

• One following TRUE

• Other following FALSE

• Single-alternative decision structures are similar to the dual-alternative

• The ELSE part is empty

Start

Penguin turns toface hole

Holewider thanpenguin?

Penguin walks to center of hole

Penguin fallsinto hole

End

Single-Alternative Decision Structures

T

F

Nested If/Else Instructions

• When an If/Else instruction is placed inside another If/Else instruction

• The inner If/Elseexecutes only if the outer If/Else istrue

Nested If/Else Instructions

Start

Penguin faces hole

Within 2 meters

False

True

“Too Far!”

Hole wider?

False

True

“Drats!”

Penguin walks

Penguin falls in

End

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

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

• Concept illustrated

• Relational operations are defined using relational operators

• Can check for multiple conditions at one time with Boolean logic

• Boolean logic operators are used to build an expression composed of multiple conditions

• And

• Boolean: A and B: both must be true to be true

• In Alice: Both A and B

• Or

• Boolean: A or B: one or both must be true to be true

• In Alice: Either A or B, or both

• Not

• Inverses what you feed it

• Example: if A = True then “not A” is False

• In Alice: not A

Then choose functions

Select desired Boolean function

Drag to conditional instruction like If/Else Condition

Boolean Logic in Functions

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!

• Can combine multiple conditions

• Example:

• Penguin within 2 meters

• And circle is wider than penguin

• Similar to nested If/Else but does not do what is in second Else statement

• Penquin say “too far away“

• Sometimes you might want to see what a property of an object is

• Color

• Opacity

• isShowing

• 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

• Select desired comparison

• Another menu appears allowing you to select value that you want to compare property to

• Ex: color property will give list of colors

• Frog and ladybug are in garden

• Frog turns to face ladybug

• If ladybug’s color is red

• Frog says “EEK! Go away!”

• Else

• Frog says “Good Morning”

• Endif