boolean & if statementsathena.ecs.csus.edu/~jacksocj/handouts/csc10a_slides_chapter4_s… ·...

42
1 Boolean & If Statements Chapter 4 Introduction to Decision Structures Chapter 4.1

Upload: others

Post on 05-Apr-2020

17 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Boolean & If Statementsathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter4_S… · Structures Chapter 4.2 Dual Alternative Decision Structures selects between two different

1

Boolean &

If Statements

Chapter 4

Introduction to

Decision

Structures

Chapter 4.1

Page 2: Boolean & If Statementsathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter4_S… · Structures Chapter 4.2 Dual Alternative Decision Structures selects between two different

2

A decision structure allows a

program to perform actions

only under certain conditions

Found in virtually every

programming language

Introduction to Decision Structures

1/21/2017 Sacramento State - CSc 10A

If Statement

• gives a single, optional alternative

• very common in programming

If-Then-Else

• gives dual (two) alternatives

• a form of the If Statement

Case structure

• gives multiple alternative decisions

• not supported in Flowgorithm

1/21/2017 Sacramento State - CSc 10A

Different Types of Decisions

Page 3: Boolean & If Statementsathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter4_S… · Structures Chapter 4.2 Dual Alternative Decision Structures selects between two different

3

Decision structures rely on having the

computer compare data and make a

decision based on the result

For this, computers use Boolean Logic

• basis of all computer (and human) logic

• Boolean values can be either True or False

• understanding Boolean logic is essential to

knowing how to program

1/21/2017 Sacramento State - CSc 10A

Boolean Logic

A Relational Operator

determines whether a specific

relationship exists between

two values

The most basic way to test

two pieces of data

Returns either True or False

1/21/2017 Sacramento State - CSc 10A

Relational Operators

Page 4: Boolean & If Statementsathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter4_S… · Structures Chapter 4.2 Dual Alternative Decision Structures selects between two different

4

Operator Name

== Equal To

!= Not Equal To

> Greater Than

>= Greater Than or Equal

< Less Than

<= Less Than or Equal

1/21/2017 Sacramento State - CSc 10A

Relational Operators

1/21/2017 Sacramento State - CSc 10A

Relational Examples

1 == 4

24 != 31

11 < 10

100 >= 100

False

True

False

True

Page 5: Boolean & If Statementsathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter4_S… · Structures Chapter 4.2 Dual Alternative Decision Structures selects between two different

5

An action only occurs if the

Boolean expression is True

Otherwise, nothing will occur

A diamond symbol is used in

flowcharts

If Statements

1/21/2017 Sacramento State - CSc 10A

If Condition Then

Statements

End If

1/21/2017 Sacramento State - CSc 10A

Book Pseudocode

Either True or False

Multiple

statements

Page 6: Boolean & If Statementsathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter4_S… · Structures Chapter 4.2 Dual Alternative Decision Structures selects between two different

6

Declare Integer age

Input age

If age >= 21 Then

Display "Kegger!"

End If

1/21/2017 Sacramento State - CSc 10A

Example in Pseudocode

1/21/2017 Sacramento State - CSc 10A

Example in a Flowchart

Page 7: Boolean & If Statementsathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter4_S… · Structures Chapter 4.2 Dual Alternative Decision Structures selects between two different

7

22

Kegger!

1/21/2017 Sacramento State - CSc 10A

Example Output

20

1/21/2017 Sacramento State - CSc 10A

Example 2 Output

Nothing.

The Display Statement is

not executed

Page 8: Boolean & If Statementsathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter4_S… · Structures Chapter 4.2 Dual Alternative Decision Structures selects between two different

8

Declare Integer age

Input age

If age >= 21 Then

Display "Kegger!"

End If

1/21/2017 Sacramento State - CSc 10A

Example

Only executed

if true

Declare Integer guess

Display "Year CSUS founded? "

Input guess

If guess == 1947 Then

Display "Correct!"

End If

1/21/2017 Sacramento State - CSc 10A

Example 2

Page 9: Boolean & If Statementsathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter4_S… · Structures Chapter 4.2 Dual Alternative Decision Structures selects between two different

9

Year CSUS founded? 1992

1/21/2017 Sacramento State - CSc 10A

Increment Example Output

Year CSUS founded: 1947

Correct!

1/21/2017 Sacramento State - CSc 10A

Increment Example Output

Page 10: Boolean & If Statementsathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter4_S… · Structures Chapter 4.2 Dual Alternative Decision Structures selects between two different

10

Dual Alternative

Decision

Structures

Chapter 4.2

Dual Alternative Decision

Structures selects between

two different groups of

statements

If the Boolean expression is

True it executes one group

and, if False, the other

1/21/2017 Sacramento State - CSc 10A

Dual Alternative Structures

Page 11: Boolean & If Statementsathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter4_S… · Structures Chapter 4.2 Dual Alternative Decision Structures selects between two different

11

The If Statement has an optional "else"

clause

This denotes the group that executes in the

expression is False

It goes before the "End If" since it is part of

the same If-Statement

1/21/2017 Sacramento State - CSc 10A

The Else Clause

If Condition Then

Statements

else

Statements

End If

1/21/2017 Sacramento State - CSc 10A

Book Pseudocode

Processed if the

Condition is false

Page 12: Boolean & If Statementsathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter4_S… · Structures Chapter 4.2 Dual Alternative Decision Structures selects between two different

12

If condition Then

statement

statement

Else

statement

statement

End if

If temp < 40 Then

Display "It's cold"

Display "Get a coat!"

Else

Display "It's warm"

Display "Get water!"

End if

Pseudocode

1/21/2017 Sacramento State - CSc 10A

Declare Integer age

Input age

If age >= 21 Then

Display "Kegger! :)"

Else

Display "Milk! :("

End If

1/21/2017 Sacramento State - CSc 10A

Dual Example in Pseudocode

Page 13: Boolean & If Statementsathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter4_S… · Structures Chapter 4.2 Dual Alternative Decision Structures selects between two different

13

1/21/2017 Sacramento State - CSc 10A

Dual Example in a Flowchart

22

Kegger! :)

1/21/2017 Sacramento State - CSc 10A

Else Example Output

Page 14: Boolean & If Statementsathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter4_S… · Structures Chapter 4.2 Dual Alternative Decision Structures selects between two different

14

18

Milk! :(

1/21/2017 Sacramento State - CSc 10A

Else Example Output

Comparing

Strings

Chapter 4.3

Page 15: Boolean & If Statementsathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter4_S… · Structures Chapter 4.2 Dual Alternative Decision Structures selects between two different

15

Most languages allow you to

compare strings

Textual data is often used to

make decisions

The same rules that apply to

comparing numbers, applies

to strings

1/21/2017 Sacramento State - CSc 10A

Comparing Strings

1/21/2017 Sacramento State - CSc 10A

name1 == name2

month != "February"

String Equality

You can test two string variables (or literals) for

equality

You can also test if one string is greater or less

than another string (allows for sorting strings)

Page 16: Boolean & If Statementsathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter4_S… · Structures Chapter 4.2 Dual Alternative Decision Structures selects between two different

16

String comparisons are generally case

sensitive

This means that uppercase and lower case

letters are not the same

Why? Let's look back at ASCII…

1/21/2017 Sacramento State - CSc 10A

Case Sensitivity

NUL SOL STX ETX EOT ENQ ACK BEL BS HT LF VT FF CR SO SI

DLE CD1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC FS GS RS US

sp ! " # $ % & ' ( ) * + , - . /

0 1 2 3 4 5 6 7 8 9 : ; < = > ?

@ A B C D E F G H I J K L M N O

P Q R S T U V W X Y Z [ \ ] ^ _

` a b c d e f g h i j k l m n o

p q r s t u v w x y z { | } ~ DEL

1/21/2017 Sacramento State - CSc 10A

ASCII Chart Review

Page 17: Boolean & If Statementsathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter4_S… · Structures Chapter 4.2 Dual Alternative Decision Structures selects between two different

17

Each character has a unique value

The following is how "Moe" is stored in ASCII

Character Binary Decimal

M 01001101 77

o 01101111 111

e 01100101 101

1/21/2017 Sacramento State - CSc 10A

ASCII Codes

1/21/2017 Sacramento State - CSc 10A

Comparing Letters

A 01000001 65

B 01000010 66

C 01000011 67

D 01000100 68

E 01000101 69

F 01000110 70

a 01100001 97

b 01100010 98

c 01100011 99

d 01100100 100

e 01100101 101

f 01100110 102

"A" < "a"

ASCII Value

Page 18: Boolean & If Statementsathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter4_S… · Structures Chapter 4.2 Dual Alternative Decision Structures selects between two different

18

1/21/2017 Sacramento State - CSc 10A

Letter Examples

"a" == "A"

"a" > "A"

"abC" < "abc"

"dog" != "cat"

False

True

True

True

Declare String answer

Display "Do you love programming?"

Input answer

If answer = "y" Then

Display "Most excellent!"

End If

1/21/2017 Sacramento State - CSc 10A

Example

Page 19: Boolean & If Statementsathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter4_S… · Structures Chapter 4.2 Dual Alternative Decision Structures selects between two different

19

Do you love programming?

y

Most excellent!

1/21/2017 Sacramento State - CSc 10A

Example Output

Do you love programming?

Y

1/21/2017 Sacramento State - CSc 10A

Example Output

Nothing!

"y" is not equal to "Y"

Page 20: Boolean & If Statementsathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter4_S… · Structures Chapter 4.2 Dual Alternative Decision Structures selects between two different

20

Nested

Decision

Structures

Chapter 4.4

In programming, control

structures can be put inside

other structures

This is called nesting and

allows complex control

1/21/2017 Sacramento State - CSc 10A

Nested Decision Structures

Page 21: Boolean & If Statementsathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter4_S… · Structures Chapter 4.2 Dual Alternative Decision Structures selects between two different

21

By nesting…

• you can put almost anything in

anything

• this allows you to create

complex programs

1/21/2017 Sacramento State - CSc 10A

Nested Decision Structures

Often If Statements are

embedded in other If

Statements

This can give multiple

branches in the program

… and have a conditional

statement dependent on

another

1/21/2017 Sacramento State - CSc 10A

Nested If Statements

Page 22: Boolean & If Statementsathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter4_S… · Structures Chapter 4.2 Dual Alternative Decision Structures selects between two different

22

1/21/2017 Sacramento State - CSc 10A

Nested Decision Structures

1/21/2017 Sacramento State - CSc 10A

Harry Potter Example

False TrueSeeks

Power?

Sorting Hat

Intellectual?

TrueFalse

RavenclawHufflepuff Gryffindorr Slytherin

Selfish?

TrueFalse

Page 23: Boolean & If Statementsathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter4_S… · Structures Chapter 4.2 Dual Alternative Decision Structures selects between two different

23

Sometimes an If Statement is

nested on the else clause of

another If Statement

It allows what is called an "If-

Else Chain"

1/21/2017 Sacramento State - CSc 10A

If-Else Chain

It can make nested logic

simpler to write

Basically, the chain checks

multiple expressions and

selects the first one that is

true

1/21/2017 Sacramento State - CSc 10A

If-Else Chain

Page 24: Boolean & If Statementsathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter4_S… · Structures Chapter 4.2 Dual Alternative Decision Structures selects between two different

24

If score < 60 Then

Display "Grade is F."

Else If score < 70 Then

Display "Grade is D."

Else If score < 80 Then

Display "Grade is C."

Else If score < 90 Then

Display "Grade is B."

Else

Display "Grade is A."

End If

1/21/2017 Sacramento State - CSc 10A

If-Else Chain

You must indent each nested

block of statements

Otherwise your program

quickly becomes unreadable

1/21/2017 Sacramento State - CSc 10A

Indenting is Vital

Page 25: Boolean & If Statementsathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter4_S… · Structures Chapter 4.2 Dual Alternative Decision Structures selects between two different

25

Logical

Operators

Chapter 4.6

Logical Operators are used

between comparisons to

create complex Boolean

expressions

In Boolean logic, there are

just 3 operators – which are

easy to learn and master

1/21/2017 Sacramento State - CSc 10A

Logical Operators

Page 26: Boolean & If Statementsathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter4_S… · Structures Chapter 4.2 Dual Alternative Decision Structures selects between two different

26

Name Rules

AndTrue only if both operands are True

If either is False, the result is False

OrTrue if either operand is True

False if both operands are False

NotTrue if the operand is False

False if the operand is True

1/21/2017 Sacramento State - CSc 10A

Logical Operators

p q not p p or q p and q

True True False True True

True False False True False

False True True True False

False False True False False

1/21/2017 Sacramento State - CSc 10A

Boolean Table

Page 27: Boolean & If Statementsathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter4_S… · Structures Chapter 4.2 Dual Alternative Decision Structures selects between two different

27

1/21/2017 Sacramento State - CSc 10A

AND Example

1 < 2 and 10 < 40

Result: True

True True

1/21/2017 Sacramento State - CSc 10A

AND Example 2

1 < 2 and 12 < 4

Result: False

True False

Page 28: Boolean & If Statementsathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter4_S… · Structures Chapter 4.2 Dual Alternative Decision Structures selects between two different

28

1/21/2017 Sacramento State - CSc 10A

OR Example

1 < 2 or 1 < 4

Result: True

True True

1/21/2017 Sacramento State - CSc 10A

OR Example 2

1 < 2 or 6 < 4

Result: True

True False

Page 29: Boolean & If Statementsathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter4_S… · Structures Chapter 4.2 Dual Alternative Decision Structures selects between two different

29

1/21/2017 Sacramento State - CSc 10A

OR Example 3

3 < 2 or 6 < 4

False

Result: False

False

1/21/2017 Sacramento State - CSc 10A

NOT Example

not (1 < 2)

Result: False

True

Page 30: Boolean & If Statementsathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter4_S… · Structures Chapter 4.2 Dual Alternative Decision Structures selects between two different

30

1/21/2017 Sacramento State - CSc 10A

NOT Example 2

not (3 < 2)

False

Result: True

1/21/2017 Sacramento State - CSc 10A

Examples

1 < 3 and 10 < 40

1 == 3 and 10 < 40

not 1 == 2

1 > 3 or 30 < 20

True

False

True

False

Page 31: Boolean & If Statementsathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter4_S… · Structures Chapter 4.2 Dual Alternative Decision Structures selects between two different

31

1/21/2017 Sacramento State - CSc 10A

Typical Precedence Levels

6 - (unary) not

5 * /

4 + -

3 == != > >= < <=

2 and

1 or

Highest

Lowest

1/21/2017 Sacramento State - CSc 10A

Calculate The Result

2 == 4 or 1 > 2 and 5 > 4 or 1 < 3

False or False and True or True

False or False or True

False or True

True

Page 32: Boolean & If Statementsathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter4_S… · Structures Chapter 4.2 Dual Alternative Decision Structures selects between two different

32

1/21/2017 Sacramento State - CSc 10A

Calculate The Result 2

not (1 < 4) and 5 > 4 or not (1 > 4)

not True and True or not False

False and True or True

False or True

True

If temp < 20 AND minutes > 12 Then

Display "Danger!"

Display "Temperature danger zone."

End If

1/21/2017 Sacramento State - CSc 10A

And Example

Page 33: Boolean & If Statementsathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter4_S… · Structures Chapter 4.2 Dual Alternative Decision Structures selects between two different

33

Declare String answer

Display "Do you love programming?"

Input answer

If answer = "y" or answer = "Y" Then

Display "Most excellent!"

End If

1/21/2017 Sacramento State - CSc 10A

Example

Do you love programming?

Y

Most excellent!

1/21/2017 Sacramento State - CSc 10A

Example Output

Page 34: Boolean & If Statementsathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter4_S… · Structures Chapter 4.2 Dual Alternative Decision Structures selects between two different

34

Range

Checking

Chapter 4.6

Range Checking is often

used to test is between two

values

Often used to prevent invalid

calculations

Range Checking

1/21/2017 Sacramento State - CSc 10A

Page 35: Boolean & If Statementsathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter4_S… · Structures Chapter 4.2 Dual Alternative Decision Structures selects between two different

35

1/21/2017 Sacramento State - CSc 10A

If x >= 0 AND x <= 100 Then

Display "Inside Range"

End If

Inside a Range

When checking for a number inside a range, use

AND

The following is only true if x is between 0 and

100

1/21/2017 Sacramento State - CSc 10A

If x < 20 OR x > 40 Then

Display "Outside range"

End If

Outside a Range

When checking for a number outside a range, use

OR

The following is true if x is outside the range

Page 36: Boolean & If Statementsathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter4_S… · Structures Chapter 4.2 Dual Alternative Decision Structures selects between two different

36

Declare Integer years, days

Input years

If years >= 1 and years <= 110 Then

Set days = years * 365;

Display "Days lived: ", days

Else

Display "Invalid Age!"

End If

1/21/2017 Sacramento State - CSc 10A

Range Checking Example

20

Days old: 7300

1/21/2017 Sacramento State - CSc 10A

Range Checking Example Output

Page 37: Boolean & If Statementsathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter4_S… · Structures Chapter 4.2 Dual Alternative Decision Structures selects between two different

37

300

Invalid Age!

1/21/2017 Sacramento State - CSc 10A

Range Checking Example Output

Boolean

Variables

Chapter 4.7

Page 38: Boolean & If Statementsathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter4_S… · Structures Chapter 4.2 Dual Alternative Decision Structures selects between two different

38

A variable of the Boolean

data type can hold one or two

values: true or false

It often holds the result of a

Boolean expression – which

will be used later in another

expression

1/21/2017 Sacramento State - CSc 10A

Boolean Variables

Declare Boolean isLunchTime

If time >= 12 and time <= 13 then

Set isLunchTime = True

Else

Set isLunchTime = False

End If

1/21/2017 Sacramento State - CSc 10A

Boolean Variable Example

Page 39: Boolean & If Statementsathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter4_S… · Structures Chapter 4.2 Dual Alternative Decision Structures selects between two different

39

Factoring

Cleaning up code

Often, when creating complex

if statements, code is

repeated in both the true

branch and the false branch

This redundant code can,

sometimes, moved outside

the loop

1/21/2017 Sacramento State - CSc 10A

Factoring

Page 40: Boolean & If Statementsathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter4_S… · Structures Chapter 4.2 Dual Alternative Decision Structures selects between two different

40

This only works if the

duplicate is at the beginning

of both the True branch and

the False branch

When it is done, the code is

"factored" out of the If

Statement

1/21/2017 Sacramento State - CSc 10A

Factoring

Input age

If age >= 18 then

Input name

Display name, " can vote"

Else

Input name

Display name, " cannot vote yet"

End If

1/21/2017 Sacramento State - CSc 10A

Example – What's Redundant?

Page 41: Boolean & If Statementsathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter4_S… · Structures Chapter 4.2 Dual Alternative Decision Structures selects between two different

41

1/21/2017 Sacramento State - CSc 10A

Example – What's Redundant?

Input age

If age >= 18 then

Input name

Display name, " can vote"

Else

Input name

Display name, " cannot vote yet"

End If

1/21/2017 Sacramento State - CSc 10A

Example – Redundant Code

Page 42: Boolean & If Statementsathena.ecs.csus.edu/~jacksocj/handouts/CSC10A_Slides_Chapter4_S… · Structures Chapter 4.2 Dual Alternative Decision Structures selects between two different

42

Input age

Input name

If age >= 18 then

Display name, " can vote"

Else

Display name, " cannot vote yet"

End If

1/21/2017 Sacramento State - CSc 10A

Example – Factored Out

1/21/2017 Sacramento State - CSc 10A

Example – Factored Out