Download presentation
Presentation is loading. Please wait.
Published byBlaise Richardson Modified over 8 years ago
1
CSE202: Lecture 5The Ohio State University1 Selection Structures
2
CSE202: Lecture 5The Ohio State University2 Consider the following code: int main() { int x; x = 46; x++; cout << x << endl; return 0; } Its control flow Flow of Control (1)
3
CSE202: Lecture 5The Ohio State University3 Flow of Control (2) Notice in the previous slide, that the flow of control is completely sequential. In other words, every statement is executed in order. This makes for powerless and probably insignificant programs. Can we do better? Can the flow of control not be as deterministic?
4
CSE202: Lecture 5The Ohio State University4 Selection Structures We want to add some branching to the flow of control, which allow certain statements to be executed out of sequential order, or skipped entirely. C++ allows for this with selection structures: - if-then-else statements - switch statements
5
CSE202: Lecture 5The Ohio State University5 if–then Statement Syntax The if-then statement is the first selection structure we examine. Basically, it says: “IF some condition is true, THEN run a sequence of statements.” The if-then syntax is shown below: if (conditional expression) { statements executed if condition is true }
6
CSE202: Lecture 5The Ohio State University6 if–else Statement Syntax (1) We can optionally extend the if-then statement to an if-then-else statement, meaning: “IF some condition is true, THEN run a sequence of statements. ELSE, run a sequence of other statements.” The ELSE portion is optional because it is not always the case that the program should take action if some condition is false.
7
CSE202: Lecture 5The Ohio State University7 if–else Statement Syntax (2) The if-then-else syntax is shown below: if (conditional expression) { statements executed if condition is true } else { statements executed if condition is false }
8
CSE202: Lecture 5The Ohio State University8 Example of an if-then-else Statement... int main() { double x(0.0); cout << “Enter a number: “; cin >> x; // Handle divide by zero! if (x == 0) { cout << “ERROR: Input must be not be 0!” << endl; } else { cout << “The fraction is: “ << 1/x << endl; } return 0; }
9
CSE202: Lecture 5The Ohio State University9 Notice how of the flow of control is able to branch and select which statements to execute, and which ones to avoid from running! Also notice that after the selection structure execution, the code returns back to sequential operation.
10
CSE202: Lecture 5The Ohio State University10 Nested Conditional Statements... int main() { int age(0); cout << "Enter your age: "; cin >> age; if (age >= 15) { if (age == 15) { cout << "You can get a learners permit." << endl; } else { cout << "You can get a license." << endl; } } else { cout << "You are too young to drive." << endl; } return 0; } What is the output on input: 10 15 20
11
CSE202: Lecture 5The Ohio State University11 Nested Conditional Statements... int main() { int age(0); cout << "Enter your age: "; cin >> age; if (age >= 18) { cout << "You can vote." << endl; if (age >= 35) { cout << "You can become President." << endl; } } else { cout << "You are too young to vote." << endl; } return 0; } What is the output on input: 10 20 40 Does the else clause execute on input 20? Why or why not?
12
CSE202: Lecture 5The Ohio State University12 Selection Criteria The if- conditions are common relational expressions. The outcomes of the expression is Boolean; it can only have two outcomes: true or false (or 1 or 0 respectively). In the previous example, age >= 16 is a relational expression that evaluates to either true or false depending on what the user inputs!
13
CSE202: Lecture 5The Ohio State University13 Relational Operators (1) OperatorMeaningExample < less than x < 0 > greater than speed > 65 <= less than or equal to age <=17 >= greater than or equal to gpa >= 3.5 == equal to initial == ‘a’ != not equal to divisor != 0 Relational operators return 0 and 1
14
CSE202: Lecture 5The Ohio State University14 Relational Operators (2) Relational operators can compare character data, for example ‘a’ < ‘b’ evaluates to true (why is this true?) ‘G’ > ‘M’ evaluates to 0 (or false) ‘E’ != ‘e’ evaluates to 1 (or true) 2 < 0 evaluates to 0
15
CSE202: Lecture 5The Ohio State University15 Ascii Table
16
CSE202: Lecture 5The Ohio State University16 booleanExpr.cpp // Examples of Boolean expressions #include using namespace std; int main() { double x(10); cout << "(4 != 4) evaluates to " << (4 != 4) << endl; cout << "('a' <= 'b') evaluates to " << ('a' <= 'b') << endl; cout << "('B' < 'b') evaluates to " << ('B' < 'b') << endl; cout 1.0) evaluates to " 1.0) << endl; cout << "(10 == 10) evaluates to " << (10 == 10) << endl; cout << "(x == 10) evaluates to " << (x == 10) << endl; cout << "(x = 8) evaluates to " << (x = 8) << endl; return 0; }
17
CSE202: Lecture 5The Ohio State University17 Logical Operators (1) Every one of these expressions only take 2 operands, so it is NOT possible to use the familiar expression 18 < x < 60 in order to test if x is a number in between 18 and 60. Instead, to build more complicated expressions, we can use the logical operators used in Boolean algebra: –AND (in C++, && ) –OR (in C++, || ) –NOT (in C++, ! )
18
CSE202: Lecture 5The Ohio State University18 Logical Operators (2) Going back to the previous example, if you wanted to verify that a variable x is between 18 and 60 inclusive, you could use the following expression: (age >= 18 && age <= 60) AND, OR are both binary operators, NOT is a unary operator. –If an expression has an && operator, then both operands must evaluate to 1 for the entire expression to be 1 –The || operator returns 1 if either operand evaluates to 1 –The unary ! Operator returns 1 if its operand is 0, and 0 if its operand is 1
19
CSE202: Lecture 5The Ohio State University19 The NOT Operator NOT is a unary operator (it only takes one operand), so how does it work? Let’s say that now we want to check if x is not between 18 and 60. We simply use: !(age >= 18 && age <= 60) The NOT operator only takes one operand, evaluates it to true or false, then flips the result.
20
CSE202: Lecture 5The Ohio State University20 logicalOperators1.cpp // Examples of logical operators #include using namespace std; int main() { int x(5); int y(25); if (x < 7 && y < 12) { cout << “true” << endl; } else { cout << “false” << endl; }; if (x < 7 || y < 12) { cout << “true” << endl; } else { cout << “false” << endl; }; if (x < 7 && !(y < 12)) { cout << “true” << endl; } else { cout << “false” << endl; }; return 0; }
21
CSE202: Lecture 5The Ohio State University21 logicalOperators2.cpp // Examples of logical operators #include using namespace std; int main() { int x(5); int y(25); if ((x 7 && y > 12)) { cout << “true” << endl; } else { cout << “false” << endl; }; if ((x 7 || y > 12)) { cout << “true” << endl; } else { cout << “false” << endl; }; return 0; }
22
Exercises Give a boolean expression which is true if x is a non-negative number strictly less than 50; Give a boolean expression which is true if x is not in the range [25-30]. (Multiple solutions.) CSE202: Lecture 5The Ohio State University22
23
CSE202: Lecture 5The Ohio State University23 Example of Boolean variables... int main() { int age(0); bool flag_discount(false); cout << "Enter your age: "; cin >> age; if (age < 18) { flag_discount = true; } if (age >= 65) { flag_discount = true; } if (flag_discount) { cout << "You receive a discount." << endl; } else { cout << "No discount." << endl; }...
24
CSE202: Lecture 5The Ohio State University24 Note on Truth Any number that is greater than 0 is considered to be a Boolean true in C++ Relational and logical operators only ever return 0 and 1
25
CSE202: Lecture 5The Ohio State University25 Order of (more) Operations OperatorAssociativity ! unary - ++ -- right to left * / % left to right + - left to right >= left to right == != left to right && left to right || left to right = += -= *= /= right to left
26
CSE202: Lecture 5The Ohio State University26 comparisonExample.cpp // comparison example #include using namespace std; int main() { int year(0); cout << "Enter year: "; cin >> year; int k = year%4; if (k == 0) { cout << year << " is a US presidential election year." << endl; } else { cout << year << " is NOT a US presidential election year." << endl; } return 0; }
27
CSE202: Lecture 5The Ohio State University27 > comparisonExample Enter year: 2007 2007 is NOT a US presidential election year. > comparisonExample Enter year: 2008 2008 is a US presidential election year. … int k = year%4; if (k == 0) // if k is equal to 0,... { cout << year << " is a US presidential election year." << endl; } else { cout << year << " is NOT a US presidential election year." << endl; } …
28
CSE202: Lecture 5The Ohio State University28 comparisonError.cpp // comparison error #include using namespace std; int main() { int year(0); cout << "Enter year: "; cin >> year; int k = year%4; if (k = 0) { cout << year << “ is a US pres election year.” << endl; } else { cout << year << “ is NOT a US presidential election year.” << endl; } return 0; }
29
CSE202: Lecture 5The Ohio State University29 > comparisonError Enter year: 2007 2007 is NOT a US presidential election year. > comparisonError Enter year: 2008 2008 is NOT a US presidential election year. … int k = year%4; if (k = 0) { cout << year << " is a US presidential election year." << endl; } else { cout << year << " is NOT a US presidential election year." << endl; } …
30
CSE202: Lecture 5The Ohio State University30 comparisonError2.cpp // comparison example #include using namespace std; int main() { int year(0); cout << "Enter year: "; cin >> year; int k = year%4; if (k == 0) // if k is equal to 0,... { cout << year << " is a US presidential election year." << endl; }; else { cout << year << " is NOT a US presidential election year." << endl; }; return 0; }
31
CSE202: Lecture 5The Ohio State University31 comparisonError2.cpp … int k = year%4; if (k == 0) // if k is equal to 0,... { cout << year << " is a US presidential election year." << endl; }; else { cout << year << " is NOT a US presidential election year." << endl; }; … What’s the error? (Try compiling this program.)
32
CSE202: Lecture 5The Ohio State University32 Warning Use ‘ == ’ (equal to), not ‘ = ’ (assignment). Use ‘ && ’ not ‘ & ’. Use ‘ || ’, not ‘ | ’. Do not put a semi-colon before an “else” clause.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.