Top Banner
Rational Expressions and selection structures Relational operators Logical operators Selection structures
38

Rational Expressions and selection structures Relational operators Logical operators Selection structures.

Jan 02, 2016

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Rational Expressions and selection structures Relational operators Logical operators Selection structures.

Rational Expressionsand selection structures

Relational operators Logical operators Selection structures

Page 2: Rational Expressions and selection structures Relational operators Logical operators Selection structures.

We have seen normal expressions

Any combination of variables, constants and functions that can be evaluated to yield a result

Typically involve operators

Page 3: Rational Expressions and selection structures Relational operators Logical operators Selection structures.

Relational Expressions

compare operands used in decision making, in

controlling the flow of your programs

evaluate to 1 (true) or 0 (false) Note any expression x = 2 evaluates

to value returned. Any non zero value is deemed true

Show example if (0.000001)

Page 4: Rational Expressions and selection structures Relational operators Logical operators Selection structures.

Relational Operators

less than <greater than >less than or equal to <=greater than or equal to >=a - b < 0 is equivalent to (a - b) < 0

Page 5: Rational Expressions and selection structures Relational operators Logical operators Selection structures.

Relational Expressions

Operand Relational Operand operator

price < 34.98

expression

Page 6: Rational Expressions and selection structures Relational operators Logical operators Selection structures.

Values of Relational Expressions

a-b a < b a > b a <=b a >=b

Positive 0 1 0 1

Zero 0 0 1 1

Negative

1 0 1 0

Page 7: Rational Expressions and selection structures Relational operators Logical operators Selection structures.

Relational Operators Examples

Valida < 3a > b-1.1 >= (2.2 * x + 3.3)a < b < c // syntactically correct but confusing

Not Valida =< b // out of ordera < = b // space not alloweda >> b // shift operation

Page 8: Rational Expressions and selection structures Relational operators Logical operators Selection structures.

Equality Operators

Equal to = =

Not Equal to !=Note this!

Page 9: Rational Expressions and selection structures Relational operators Logical operators Selection structures.

Values of Equality Expressions

a-b a == b a !=b

Zero 1 0

Non zero 0 1

Page 10: Rational Expressions and selection structures Relational operators Logical operators Selection structures.

Equality Operators Examples

Validc == 'A'k != -2y == 2 * z - 5

Not Correcta = b // assignment statement a = = b - 1 // space not allowedy =! z // this is equivalent to y = (!z)

Page 11: Rational Expressions and selection structures Relational operators Logical operators Selection structures.

Programming Technique Allowing for numerical Accuracy

Many decimal numbers cannot be exactly represented in binary by a finite number of bits. Thus testing for exact equality can fail.

Use the technique:|operand1 - operand2| < epsilon

Ex. x/y == 17fabs(x/y - 17) < 0.000001

Page 12: Rational Expressions and selection structures Relational operators Logical operators Selection structures.

More Logical Operators

Negation (unary) !

Logical and &&

Logical or ||

Page 13: Rational Expressions and selection structures Relational operators Logical operators Selection structures.

Logical Operators: Examples

Valida && ba || b && c!(a < b) && c3 && (-2 * a + 7)

Not Valida && // one operand missinga | | b // extra space not alloweda & b // this is a bitwise operation&b // the address of b

Page 14: Rational Expressions and selection structures Relational operators Logical operators Selection structures.

int a = 0, b = 3, c = 1, d =4;

a && !c || d

1F

2F

Logical Operators: Examples

3 T

Page 15: Rational Expressions and selection structures Relational operators Logical operators Selection structures.

int a = 0, b = 3, c = 1, d =4;

a && b || !c || d

1F

2F

3F

Logical Operators: Examples

4 T

Page 16: Rational Expressions and selection structures Relational operators Logical operators Selection structures.

Truth Tables for &&, ||, !

a b a || b a && b !a

0 0 0 0 1

0 1 1 0 1

1 0 1 0 0

1 1 1 1 0

Page 17: Rational Expressions and selection structures Relational operators Logical operators Selection structures.

Common Errors!

= = means means equalityequality

= used for assignmentused for assignment

FALSE is zero

TRUE is nonzero

Boolean operators give a Boolean result

Page 18: Rational Expressions and selection structures Relational operators Logical operators Selection structures.

Control StructuresDecisions or Selections or Branches

ifswitch? operator

Page 19: Rational Expressions and selection structures Relational operators Logical operators Selection structures.

Simple single alternative if Compound multi-alternative if...else

Two types of IF

Page 20: Rational Expressions and selection structures Relational operators Logical operators Selection structures.

Single Alternative Decision

An action is taken if the condition is

true, otherwise the control goes to

the next statement.

Page 21: Rational Expressions and selection structures Relational operators Logical operators Selection structures.

Syntaxif (expression) {

statement}If expression is true, statement is executed; otherwise statement is skipped.

no ;

Single Alternative Decision

Example:if (stomach if (stomach == empty) { empty) {

eat a mars bar; eat a mars bar;

}}

note: 2 == signs

Page 22: Rational Expressions and selection structures Relational operators Logical operators Selection structures.

Syntaxif (expression) {

statement

}Recall that an expression is any

combination of variables, constants, or function calls that evaluate to a value.

e.g. 5 x + y a = 3 + jN n++ f(12.3, a, “Yvonne”)

Single Alternative Decision

Page 23: Rational Expressions and selection structures Relational operators Logical operators Selection structures.

Single Alternative Decision

Example:if (grade >= 90){ cout << Congratulations!\n”;cout << “Your grade is “;cout << grade << “.\n";

}

note braces

Good Practice:Always add braces to if control structures

Page 24: Rational Expressions and selection structures Relational operators Logical operators Selection structures.

The nested if Statement

Syntaxif (expression) {{ statement; statement; if (expression){ statement; statement; } }}

Example:if (u > v) {{

a = 1;b = 2;

if ( u > z) {x =11;y = 12;

}}}

The nested if statement is itself an if statement.

Page 25: Rational Expressions and selection structures Relational operators Logical operators Selection structures.

Nested ifif Example

if (number == secretnumber){ cout << “You guessed it!”;}

if (number != secretnumber){ cout << “that’s not the number.\n”;

if (number > secretnumber){ cout << “You guessed too high.\n”;}

else { cout << “You guessed too low.\n”;}

}

Page 26: Rational Expressions and selection structures Relational operators Logical operators Selection structures.

Examples

Valid:Valid:if (y != 0.0) z = x/y;

if (a < b && b < c) { d = a + b + c; cout << "All OK\n";

}

Not ValidNot Valid:if b == a area = a * a;

if (a < b) && (b < c)if (a < b) ;

Valid But... semi Valid But... semi colon!colon!if (a < b) ;

Page 27: Rational Expressions and selection structures Relational operators Logical operators Selection structures.

ifif Problems

Using Using == in place of in place of ====

What is the difference between these two?

if (toss == 7)if (toss == 7)

cout << “You win the bet.”;cout << “You win the bet.”;

if (toss = 7)if (toss = 7)cout << “You win the bet.”;cout << “You win the bet.”;

Chris demonstrated using debugger

Page 28: Rational Expressions and selection structures Relational operators Logical operators Selection structures.

If problemsIf problems

not adding braces.

if (grade > 70)cout << “Well done << endl;cout << “You got a first”;

This line will always be executed

Demonstrate this Chris

Page 29: Rational Expressions and selection structures Relational operators Logical operators Selection structures.

Double Alternative Decision using if … else structure

An action (or set of actions) is taken if the condition is true, another action (or set of actions) is taken if the condition is false, then the control goes to the next statement.

Page 30: Rational Expressions and selection structures Relational operators Logical operators Selection structures.

The if-elseif-else StatementSyntaxif (expression) {statement(s)1

}else {statement(s)2

}If expression is nonzero then statement1 is executed

and statement2 is skipped. If expression is zero statement1 is skipped and statement2 is executed.

Page 31: Rational Expressions and selection structures Relational operators Logical operators Selection structures.

if ... elseif ... else Examples

if (stomach == empty){ eat a pizza; eat a mars bar;}else { eat a salad;}

Page 32: Rational Expressions and selection structures Relational operators Logical operators Selection structures.

if … else example

if ( j < k ){ min = j;

k = k * 3;}else {

min = k;j = j * 3;

}

Page 33: Rational Expressions and selection structures Relational operators Logical operators Selection structures.

Application: Finding the minimum of three values

Page 34: Rational Expressions and selection structures Relational operators Logical operators Selection structures.

Finding the Minimum of Three Values

int x, y, z, min;cout << “Input three integers: “;cin >> x >> y >> z;

if (x < y){min = x;

}else{

min = y;

}

if (z < min){min = z;

}

cout << “The minimum value is “ << min <<‘\n’;

Demonstrate this Chris

Page 35: Rational Expressions and selection structures Relational operators Logical operators Selection structures.

Syntax if (expression1){ statement1

} else if (expression2) { statement2

} . . .

else if (expressionN) { statementN

} else { last statement

} next statement

Chained if...elseif...else Example

Page 36: Rational Expressions and selection structures Relational operators Logical operators Selection structures.

Application of Chained if...elseif...else statements

if (total >=70) {grade = ‘A’;

}else if (total >= 60){

grade = ‘B’;}else if (total >= 50){

grade = ‘C’;{else if (total >= 40){

grade = ‘D’;{else {

grade = ‘F’;}next statement

Page 37: Rational Expressions and selection structures Relational operators Logical operators Selection structures.

The Dangling elseelse which if does which if does the else belong to?the else belong to?

if (avg >= 40.0)if (avg < 50.0)

cout << “Passing, but marginal”; else

cout << “Failing”;

if (avg >= 40.0) {if (avg < 50.0)

cout << “Passing,but marginal”; }else

cout << “Failing”;Note good indentation will help you and use braces even when only one statement!

Page 38: Rational Expressions and selection structures Relational operators Logical operators Selection structures.

ReviewRelational expressions are used to compare

if result is true expression evaluates to 1 if result is false expression evaluates to 0

other expression that evaluate to non zero are deemed trueMore complex conditions using && || and !use if…else to choose between two alternativesnested if statements contain another if in bodyget into habit of always using braces even when only one statement is to be executed.multi-way selection using if-else chains.watch out for = and == confusion