Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 1400 Chapter 4, sections 1 - 6. Relational operators Less than< Greater than> Less than or equal<= Greater than or equal>= Equal== Not equal!=

Similar presentations


Presentation on theme: "CS 1400 Chapter 4, sections 1 - 6. Relational operators Less than< Greater than> Less than or equal<= Greater than or equal>= Equal== Not equal!="— Presentation transcript:

1 CS 1400 Chapter 4, sections 1 - 6

2 Relational operators Less than< Greater than> Less than or equal<= Greater than or equal>= Equal== Not equal!=

3 Relational expressions A comparison is a relational expression A simple relational expression compares two operands and is either true or false Examples cost < 56 time >= 1.5 25 != age

4 bool variables A bool variable can hold true or false Examples: bool answer, flag; answer = age < 25; flag = cost == price;

5 What is “truth”? In C++: true is non-zero, false is zero The words true and false are actually reserved constants. bool valid, correct; valid = true; correct = false;

6 Condition statements Recommended forms if (rel-expr) { statements } else { statements }

7 Condition statements… Each condition statement divides a program into two separate paths –the true path –the false path (which may be absent) Only one of these two paths are taken!

8 Examples… Write a program to output ADULT if a person’s age is 21 or over. Write a program to output ADULT for 21 and over and MINOR otherwise. Write a program to output TWENTY-ONE if a person’s age is exactly 21.

9 What do braces mean? Braces are a way to associate a set of statements to be treated as one statement. –In other words, any place you can put one statement, you can put a set of statements in braces! A set of statements in braces is commonly referred to as a block.

10 Condition statements revisited… General forms if (rel-expr) statement statement else statement

11 Braces are not required for a single statement But an else is always associated with the closest possible if above it! Example: Skill level >1 is advanced, level >2 is advanced master, lower levels are beginners. if (skill > 1) cout << “advanced “; if (skill > 2) cout << “master”; else cout << “beginner”;

12 Solution – use braces! if (skill > 1) {cout << “advanced “; if (skill > 2) {cout << “master”; } else cout << “beginner”;

13 Nested conditions (if/else/if) Example: Output a letter grade based upon a test score –90 to 100 is an A –80 to below 90 is a B –70 to below 80 is a C –60 to below 70 is a D –below 60 is an F Solution: Divide the program into two parts Stepwise Decomposition!

14 cout << “Enter grade: “; cin >> grade; if (grade >= 90) {cout << “A”; } else { } grade must be B or below First, the A’s and non A’s // cloud of non A’s

15 Now divide the non-A’s into two parts (B’s and non B’s)… if (grade >= 80) {cout << “B”; } else { } grade must be C or below // cloud of non B’s

16 and so on… if (grade >= 70) {cout << “C”; } else { } grade must be D or below // cloud of non C’s

17 Finally, grade must be D or F if (grade >= 60) cout << “D”; else cout << “F”;

18 #include using namespace std; int main() {float grade; cout << "Enter grade: "; cin >> grade; if (grade >= 90) {cout << "A"; } else {if (grade >= 80) {cout << "B"; } else {if (grade >= 70) {cout << "C"; } else {if (grade >= 60) cout << "D"; else cout << "F"; }

19 #include using namespace std; int main() {float grade; cout << "Enter grade: "; cin >> grade; if (grade >= 90) cout << "A"; else if (grade >= 80) cout << "B"; else if (grade >= 70) cout << "C"; else if (grade >= 60) cout << "D"; else cout << "F"; } Braces are actually not needed for a single statement!

20 Examples… Input a grade and output “passing” if the grade is 60 or above. Input an age and output one of three clasifications: adult (21 to 64), minor (under 21), or senior (65 and over). Input a price and validate with the message “valid” if the price is between $1500 and $3500 (inclusive)

21 Question: What would the output be for inputs of 1, 2, and 3? int answer; cin >> answer; if (answer == 1) cout << “A “; if (answer < 3) cout << “B”; else cout << “C”;

22 Example; A person 15 years of age may acquire a driver’s lic. with permission from parents. A person over 15 is eligible without parental permission. A person under 15 is not eligible. Write a small program to input an age and output one of 3 messages: “permission required”, “eligible”, or “not eligible”.

23 Example; A person 15 years of age and female or 16 years of age and male may acquire a driver’s lic. with permission from parents. A person over 16 is eligible without parental permission. A person under 15 is not eligible.

24 Summary relational operators relational expressions bool variables truth condition statements braces and blocks stepwise decomposition


Download ppt "CS 1400 Chapter 4, sections 1 - 6. Relational operators Less than< Greater than> Less than or equal<= Greater than or equal>= Equal== Not equal!="

Similar presentations


Ads by Google