Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a...

23
Chapter 5 Logic; Got Any?

Transcript of Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a...

Page 1: Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a program Control Structure A statement used to alter.

Chapter 5

Logic; Got Any?

Page 2: Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a program Control Structure A statement used to alter.

Flow of Control

Flow of ControlThe order in which the computer

executes statements in a program Control Structure

A statement used to alter the normally sequential flow of control

Selection (Chapter 5)Iteration (Chapter 6)

Page 3: Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a program Control Structure A statement used to alter.

Selection

Choosing between one or more options

Example:If statementsSwitch statements

Page 4: Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a program Control Structure A statement used to alter.

Boolean Data

We state an assertion and check it’s validityIf the assertion is true, we do one

thing.Else we do some other thing.

We use bool data typesTwo valuestrue and false

Page 5: Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a program Control Structure A statement used to alter.

Logical Expressions

Boolean Variables and ConstantseventFound = false;

Relational OperatorslessThan = ( i < j );

Page 6: Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a program Control Structure A statement used to alter.

Relational Operators

Operator==

!=

>

<

>=

<=

Relationship TestedEquality

Inequality

Greater Than

Less Than

Greater Than or Equal To

Less Than or Equal To

Page 7: Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a program Control Structure A statement used to alter.

Logical Operators

Three operatorsAnd &&

• Both relationship must be true for the and to be true

Or ||• One relationship must be true for the or to

be trueNot !

• Inverts the relationships truth value

Page 8: Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a program Control Structure A statement used to alter.

And Truth Table

Value of x Value of y Value of x && y

true true true

true false false

false true false

false false false

Page 9: Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a program Control Structure A statement used to alter.

Or Truth Table

Value of x Value of y Value of x || y

true true true

true false true

false true true

false false false

Page 10: Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a program Control Structure A statement used to alter.

Not Truth Table

Value of x Value of !x

true false

false true

Page 11: Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a program Control Structure A statement used to alter.

Short Circuit

If the value of a statement can be determined before the entire statement must be evaluated, than do it

Page 12: Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a program Control Structure A statement used to alter.

Precedence of Operators

! Unary + Unary - Highest Precedence

Lowest Precedence

* / %

+ -

< <= > >=

== !=

&&

||

=

Page 13: Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a program Control Structure A statement used to alter.

The if Statement

If-Then-Else Formif ( Expression )

Statement A

else

Statement B

Page 14: Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a program Control Structure A statement used to alter.

Expressions and Statements

An expression is a statement that evaluates to a logic value

A statement is what you want the computer to do

Page 15: Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a program Control Structure A statement used to alter.

Example

if ( hours <= 40.0 )

pay = rate * hours;

else

pay = rate * ( 40.0 + ( hours -40.0 ) * 1.5);

cout << pay;

Page 16: Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a program Control Structure A statement used to alter.

Compound Statements

Not limited to one statement Use { and } to indicate statements that

are to be grouped together

Page 17: Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a program Control Structure A statement used to alter.

Example

if ( divisor != 0 ){

result = dividend / divisor;cout << “Division performed.\n”;

}else{

cout << “Division by zero is not allowed\n”;result = 9999;

}

Page 18: Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a program Control Structure A statement used to alter.

If-Then Form

Sometimes there is only one choice Use:

if ( Expression )

Statement

Page 19: Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a program Control Structure A statement used to alter.

Nested If’s

You can stack ifs as deep as you wantif ( month == 1 )

cout << “January”;else if ( month == 2 )

cout << “February”;…

elsecout << “December”;

Page 20: Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a program Control Structure A statement used to alter.

Dangling Else

Where does the else go?

if ( average < 70.0 )

if ( average < 60.0 )

cout << “FAILING”;

else

cout << “Barely Passing ”;

Page 21: Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a program Control Structure A statement used to alter.

How about this one

if ( average >= 60.0 )

if ( average < 70.0 )

cout << “Barely Passing ”;

else

cout << “FAILING”;

Page 22: Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a program Control Structure A statement used to alter.

Correct Version

if ( average >= 60.0 )

{

if ( average < 70.0 )

cout << “Barely Passing ”;

}

else

cout << “FAILING”;

Page 23: Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a program Control Structure A statement used to alter.

Testing streams

You can test whether the last operation performed on a stream failed or notif ( cin )if ( cin.fail() )if ( !inFile )if ( !inFile.fail() )