Download presentation
Presentation is loading. Please wait.
Published byMildred McKinney Modified over 9 years ago
2
Rational Expressions and selection structures Relational operators Logical operators Selection structures
3
We have seen normal expressions Any combination of variables, constants and functions that can be evaluated to yield a result Typically involve operators
4
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)
5
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
6
Relational Expressions Operand Relational Operand operator price < 34.98 expression
7
Values of Relational Expressions a-ba < ba > ba <=ba >=b Positive0101 Zero0011 Negative1010
8
Relational Operators Examples Valid a b -1.1 >= (2.2 * x + 3.3) a < b < c// syntactically correct but confusing Not Valid a = > b// shift operation
9
Equality Operators Equal to= = Not Equal to!= Note this!
10
Values of Equality Expressions a-ba == ba !=b Zero10 Non zero01
11
Equality Operators Examples Valid c == 'A' k != -2 y == 2 * z - 5 Not Correct a = b // assignment statement a = = b - 1// space not allowed y =! z// this is equivalent to y = (!z)
12
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 == 17 fabs(x/y - 17) < 0.000001
13
More Logical Operators Negation (unary) ! Logical and&& Logical or||
14
Logical Operators: Examples Valid a && b a || b && c !(a < b) && c 3 && (-2 * a + 7) Not Valid a && // one operand missing a | | b // extra space not allowed a & b// this is a bitwise operation &b// the address of b
15
int a = 0, b = 3, c = 1, d =4; a && !c || d 1 F 2 F Logical Operators: Examples 3 T
16
int a = 0, b = 3, c = 1, d =4; a && b || !c || d 1 F 2 F 3 F Logical Operators: Examples 4 T
17
Truth Tables for &&, ||, ! aba || ba && b!a 00001 01101 10100 11110
18
Common Errors! means equality = =means equality used for assignment =used for assignment FALSE is zero TRUE is nonzero Boolean operators give a Boolean result
19
Control Structures Decisions or Selections or Branches if switch ? operator
20
Simple single alternative if Compound multi-alternative if...else Two types of IF
21
Single Alternative Decision An action is taken if the condition is true, otherwise the control goes to the next statement.
22
Syntax if (expression) { statement } If expression is true, statement is executed; otherwise statement is skipped. no ; Single Alternative Decision if (stomach empty) { eat a mars bar; Example: if (stomach == empty) { eat a mars bar;} = note: 2 = signs
23
Syntax if (expression) { statement } Recall that an expression is any combination of variables, constants, or function calls that evaluate to a value. e.g.5x + y a = 3 + j Nn++ f(12.3, a, “Yvonne”) Single Alternative Decision
24
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
25
The nested if Statement Syntax { if (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.
26
if Nested if Example if (number == secretnumber){ cout << “You guessed it!”; } if (number != secretnumber){ cout secretnumber){ cout << “You guessed too high.\n”; } else { cout << “You guessed too low.\n”;}
27
Examples Valid: Valid: if (y != 0.0) z = x/y; if (a < b && b < c) { d = a + b + c; cout << "All OK\n"; } Not Valid Not Valid: if b == a area = a * a; if (a < b) && (b < c) if (a < b) ; Valid But... semi colon! if (a < b) ;
28
if if Problems Using = in place of == if (toss == 7) cout << “You win the bet.”; What is the difference between these two? if (toss == 7) cout << “You win the bet.”; if (toss = 7) cout << “You win the bet.”; Chris demonstrated using debugger
29
If 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
30
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.
31
if-else The if-else Statement Syntax if (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.
32
if... else if... else Examples if (stomach == empty){ eat a pizza; eat a mars bar; } else { eat a salad; }
33
if … else example if ( j < k ){ min = j; k = k * 3; } else { min = k; j = j * 3; }
34
Application: Finding the minimum of three values
35
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
36
Syntax if (expression1){ statement1 } else if (expression2) { statement2 }... else if (expressionN) { statementN } else { last statement } next statement if...else Chained if...else Example
37
if...else Application of Chained if...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
38
else which if does the else belong to? The Dangling else which if does 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!
39
Review Relational 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 true More complex conditions using && || and ! use if…else to choose between two alternatives nested if statements contain another if in body get 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.