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

Rational Expressions and selection structures Relational operators Logical operators Selection structures

Embed Size (px)

Citation preview

Rational Expressionsand 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

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)

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

Relational Expressions

Operand Relational Operand operator

price < 34.98

expression

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

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

Equality Operators

Equal to = =

Not Equal to !=Note this!

Values of Equality Expressions

a-b a == b a !=b

Zero 1 0

Non zero 0 1

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)

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

More Logical Operators

Negation (unary) !

Logical and &&

Logical or ||

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

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

a && !c || d

1F

2F

Logical Operators: Examples

3 T

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

a && b || !c || d

1F

2F

3F

Logical Operators: Examples

4 T

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

Common Errors!

= = means means equalityequality

= used for assignmentused for assignment

FALSE is zero

TRUE is nonzero

Boolean operators give a Boolean result

Control StructuresDecisions or Selections or Branches

ifswitch? operator

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

Two types of IF

Single Alternative Decision

An action is taken if the condition is

true, otherwise the control goes to

the next statement.

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

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

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

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.

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”;}

}

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) ;

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

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

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.

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.

if ... elseif ... else Examples

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

if … else example

if ( j < k ){ min = j;

k = k * 3;}else {

min = k;j = j * 3;

}

Application: Finding the minimum of three values

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

Syntax if (expression1){ statement1

} else if (expression2) { statement2

} . . .

else if (expressionN) { statementN

} else { last statement

} next statement

Chained if...elseif...else Example

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

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!

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