28
1 Conditions Logical Expressions Selection Control Structures Chapter 5

1 Conditions Logical Expressions Selection Control Structures Chapter 5

Embed Size (px)

Citation preview

Page 1: 1 Conditions Logical Expressions Selection Control Structures Chapter 5

1

ConditionsLogical Expressions

Selection Control Structures

Chapter 5

Page 2: 1 Conditions Logical Expressions Selection Control Structures Chapter 5

2

The Judge says, "Listen up!"

Who was George Boole andwill he help me decide anything?

IF I cannot figure this out,

THEN I will smash this infernal machine !

Page 3: 1 Conditions Logical Expressions Selection Control Structures Chapter 5

3

Flow of Control

Defn => The order in which the computer executes statements in a program

Control StructureStatements are normally executed in a sequential

flowControl structure statements alter the normal,

sequential flow

Page 4: 1 Conditions Logical Expressions Selection Control Structures Chapter 5

4

Selection or Branching Statements

Cause the computer to choose between two alternative actions

?

stmt 1 stmt 2

FalseTrue

Page 5: 1 Conditions Logical Expressions Selection Control Structures Chapter 5

5

Logical Expressions

Also called “Boolean” expressionsUsually the “condition” which is checkedExamples:

?

stmt 1 stmt 2

FalseTrue

Page 6: 1 Conditions Logical Expressions Selection Control Structures Chapter 5

6

Boolean Data

Some languages (Pascal) have a Boolean type with actual values TRUE and FALSE

C and C++ accomplish this with int values FALSE with the int value 0 (zero) TRUE with any nonzero value

Does anything get printed?

Page 7: 1 Conditions Logical Expressions Selection Control Structures Chapter 5

7

A Boolean Constant

It is possible to create your own Boolean constants

Use another preprocessor statement, the #define

Is anything printed?

Page 8: 1 Conditions Logical Expressions Selection Control Structures Chapter 5

8

Relational Operators

Used to evaluate how quantities are relatedTypically they are mathematical inequality

symbols < > <= >=

Note => =<Some variations in C

equal = =not equal !=

Examples

Page 9: 1 Conditions Logical Expressions Selection Control Structures Chapter 5

9

Logical Operators

Used to combine logical (Boolean) expressionsOperators

AND use &&OR use ||NOT use !

&& and || are binary operatorstwo operands

! is a unary operatorone operand

Is anything printed?

Page 10: 1 Conditions Logical Expressions Selection Control Structures Chapter 5

10

Short-Circuit Evaluation

ConsiderIf the first condition is FALSE, no need to check

second condition (why?)if we do check the second, it is a run time error

(why?)

“Short-circuit evaluation”evaluate in L to R orderevaluation stops as soon as final truth value can be

determined

Page 11: 1 Conditions Logical Expressions Selection Control Structures Chapter 5

11

Precedence of Operators

!* / %+ -< <= > >== = ! =&& | | =

High precedence

Low precedence

See also page A1, Appendix B

Page 12: 1 Conditions Logical Expressions Selection Control Structures Chapter 5

12

Changing English Statements to Logical Expressions

We say “x greater than 10 and less than 20”But

Computer Syntax is different

Page 13: 1 Conditions Logical Expressions Selection Control Structures Chapter 5

13

Proper Logical Expressions

We are really wanting to AND two comparisons(x greater than 10) AND (x less than 20)

Is anything printed?

Page 14: 1 Conditions Logical Expressions Selection Control Structures Chapter 5

14

Interesting Phenomenon with Floating Point Values

Calculated values may be algebraically equal but evaluate differently

This is due to conversion to and from binary, internal round off.

Page 15: 1 Conditions Logical Expressions Selection Control Structures Chapter 5

15

Relational Operators with Floating Point Values

Do not compare floating point numbers for equality Operands for = = must match bit for binary bit Algebraically equal is not bitwise equal Instead, compare for closeness

Page 16: 1 Conditions Logical Expressions Selection Control Structures Chapter 5

16

The If-Else Form

Note the syntaxThe condition expression is usually a comparisonIt must be enclosed in parenthesesC++ does not use the “then” key wordSemicolons ; follow statements

if (abs (n1 - n2) < 0.0001)cout << "close" << endl;

elsecout << "not equal" << endl;

if (abs (n1 - n2) < 0.0001)cout << "close" << endl;

elsecout << "not equal" << endl;

?

stmt 1 stmt 2

FalseTrue

Page 17: 1 Conditions Logical Expressions Selection Control Structures Chapter 5

17

Blocks -- Compound Statements

For the “then” or “else” portion of the statement we may wish multiple statements

Use curly brackets around the block of statements

if (abs (n1 - n2) < 0.0001) {

cout << "may not be equal but, " ;cout << "close" << endl;

} else

cout << "not equal" << endl;

if (abs (n1 - n2) < 0.0001) {

cout << "may not be equal but, " ;cout << "close" << endl;

} else

cout << "not equal" << endl;

Page 18: 1 Conditions Logical Expressions Selection Control Structures Chapter 5

18

The If (only) Form

The “else” portion is optionalIf the condition is false

nothing is done?

stmt 1

FalseTrue

if (n1 == n2) cout << "equal" << endl;

if (n1 == n2) cout << "equal" << endl;

Example:

Page 19: 1 Conditions Logical Expressions Selection Control Structures Chapter 5

19

A Common Mistake

Using the = operator (assignment)instead of the = = (comparison for equality)

Actual result … 5 assigned to x, then that true value used to determine path through the if statement

if (x = 5) cout << “ x = 5”;else cout << “x not equal to 5”;

if (x = 5) cout << “ x = 5”;else cout << “x not equal to 5”;

What gets printed?

Page 20: 1 Conditions Logical Expressions Selection Control Structures Chapter 5

20

The Nested IF

IF

Page 21: 1 Conditions Logical Expressions Selection Control Structures Chapter 5

21

Nested IF

Syntax calls for a “statement” after the if ( … )

That statement can be any kind of statement(List statements we know about)

It can be an if statementcoutcinassignmentif

if (x < 7) if (y > 5) cout << “hi mom”;

if (x < 7) if (y > 5) cout << “hi mom”;

Page 22: 1 Conditions Logical Expressions Selection Control Structures Chapter 5

22

The Dangling Else

How to determine which if the else goes withExample:

if (abs (x - 7)) if (x < 7) cout << “x approaches 7 from left”; else cout << “x approaches 7 from the right”; else cout << “x not close to 7”;

if (abs (x - 7)) if (x < 7) cout << “x approaches 7 from left”; else cout << “x approaches 7 from the right”; else cout << “x not close to 7”;

Rule : An else goes with the closest unmatched if

?

?

Page 23: 1 Conditions Logical Expressions Selection Control Structures Chapter 5

23

The Dangling Else

Rule : an else goes with the closest unmatched if

Consider … how do you force an else to go with a previous if?

if (x < y)

if (y > 3) cout << “message about y > 3”; else cout << “message about x and y”;

if (x < y)

if (y > 3) cout << “message about y > 3”; else cout << “message about x and y”;

if (x < y)

{ if (y > 3) cout << “message about y > 3”; } else cout << “message about x and y”;

if (x < y)

{ if (y > 3) cout << “message about y > 3”; } else cout << “message about x and y”;

Use { curly brackets } to nest the statements

Page 24: 1 Conditions Logical Expressions Selection Control Structures Chapter 5

24

Testing the State of an I/O Stream

The name of the input stream (used by itself) returns a valuereturns a 0 if it is NOT successfulit returns a NON zero value if it IS successful

Page 25: 1 Conditions Logical Expressions Selection Control Structures Chapter 5

25

Testing the State of an I/O Stream

When reading a file (a named input stream) we wish to know when it reaches the end

Since the name returns a 0 or non-0, this can be used as a Boolean value

Used to control program sequencing, control a file reading loop

Page 26: 1 Conditions Logical Expressions Selection Control Structures Chapter 5

26

Algorithm Walk-Through

Checking an algorithm (module) for correctnessWrite down what is supposed to be true

before an algorithm runsafter the algorithm runs

Check the source code to make sure these pre- and post-conditions are as specified

Page 27: 1 Conditions Logical Expressions Selection Control Structures Chapter 5

27

Implementation Phase

Use a “code walkthrough”think of it as “playing computer”execute the module by hand, changing values, etc. to

verify

Use an execution traceuse the watch window

Test control structuresuse data, program options that will execute each

branch of the program at least once

Page 28: 1 Conditions Logical Expressions Selection Control Structures Chapter 5

28

Testing and Debugging Hints

Beware of confusingthe assignment = with the equals = =the bitwise & with the logical &&the bitwise | with the logical | |

Use <= or >=, NEVER =< or =>Don’t forget { } around blocks of statements

controlled by an if or an elseEcho print input data, test for bad data