1 / 27

Boolean Expressions and Switch Statement in C++

Learn about boolean expressions, switch statement, and comparison of characters and strings in C++. Understand operator precedence, short-circuit evaluation, and complementing boolean expressions using logical operators and DeMorgan's theorem.

santawood
Download Presentation

Boolean Expressions and Switch Statement in C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Decision II

  2. Outline • Boolean expressions • switch statement (section 4.8) CSCE 106

  3. Table 4.7English Conditions as C++ Expressions CSCE 106

  4. Boolean Assignment • bool type values are true and false • Assignment statements have general form variable = expression; • E.g.: (for variable called same of type bool) same = true; same = (x == y); CSCE 106

  5. Comparing Characters and Strings • Letters are in typical alphabetical order • Upper and lower case significant • Digit characters are also ordered as expected • String objects require string library • Compares corresponding pairs of characters CSCE 106

  6. Table 4.8Examples of Comparisons CSCE 106

  7. Table 4.6Operator Precedence CSCE 106

  8. Example a x y z flag 3.0 4.0 2.0 false x + y / z <= 3.5 2.0 5.0 false CSCE 106

  9. Example b x y z flag 3.0 4.0 2.0 false ! flag || (y + z >= x - z) true 6.0 1.0 true true CSCE 106

  10. Short Circuit Evaluation (single == ‘y’ && gender == ‘m’ && age >= 18) • If single condition is false, gender and age conditions are not evaluated. (single == ‘y’ || gender == ‘m’ || age >= 18) • If single condition is true, gender and age conditions are not evaluated. CSCE 106

  11. Additional Assignment Examples • inRange = (n > -10) && (n < 10); • isLetter = ((‘A’ <= ch) && (ch <= ‘Z’)) || ((‘a’ <= ch) && (ch <= ‘z’)); • even = (n % 2 == 0); CSCE 106

  12. Writing bool Values • Boolean values are represented by integer values in C++ • 0 for false • non-zero (typically 1) for true • Outputting (or inputting) bool type values is done with integers CSCE 106

  13. Complements of Relational Operators < >= > <= = = != CSCE 106

  14. Examples Expression Complement Flag !Flag x>=1 !(x>=1) or x<1 x>5 !(x>5) or x<=5 CSCE 106

  15. Complements of Boolean Expressions (cont’d) • Complementing (or getting opposite of) boolean/logical expressions can be done in 2 ways: • using logical operator ! (not) • using DeMorgan’s Theorem CSCE 106

  16. DeMorgan’s Theorem It explains how to complement a compound logical expression !(exp1 && exp2) is the same as !exp1 || !exp2 !(exp1 || exp2) is the same as !exp1 && !exp2 CSCE 106

  17. Example Expression z <= x && x <= y Complement !(z <= x && x <= y) the equivalent using DeMorgan’s theorem !(z <= x) || !(x <= y) which is equivalent to (without the not “!”) z > x || x > y CSCE 106

  18. Table 4.7English Conditions as C++ Expressions CSCE 106

  19. Multiple Decision Exercise Please write a multiple decision C++ segment to evaluate a letter grade, input by the user, and output the corresponding phrase according to the following table: Grade Output A or a Excellent B or b Good C or c Fair D or d or F or f Poor anything else Invalid grade CSCE 106

  20. switch selector Code Segments switch (grade) { case ‘A’:case ‘a’: cout << “Excellent” << endl; break; case ‘B’:case ‘b’: cout << “Good” << endl; break; case ‘C’:case ‘c’: cout << “Fair” << endl; break; case ‘D’:case ‘d’:case ‘F’:case ‘f’: cout << “Fair” << endl; break; default: cout << “Invalid grade” << endl; } case label if (grade == ‘A’ || grade == ‘a’) cout << “Excellent” << endl; else if (grade == ‘B’ || grade == ‘b’) cout << “Good” << endl; else if (grade == ‘C’ || grade == ‘c’) cout << “Fair” << endl; else if (grade == ‘D’ || grade == ‘d’ || grade == ‘F’ || grade == ‘f’) cout << “Poor” << endl; else cout << “Invalid grade” << endl; optional CSCE 106

  21. The switch Control Statement switch (selector ) { caselabel1: statements1; break; caselabel2:statements2; break; . . . caselabel10:statements10; break; default: statementsd; // optional } CSCE 106

  22. switch Control Statement (cont’d) • Used in C++ to select one of several alternatives. • Alternative to multiple if statements in some cases. • Especially useful when the selection (called the switch selector) is based on the value of a single variable or a simple expression. • switch selector must be of type int, char, or bool only. CSCE 106

  23. switch Control Statement (cont’d) • switch selector value compared to each case label. When there is an exact match, statements for that case are executed. • If no case label matches the selector, the entire switch body is skipped unless it contains a default case label. • break is typically used between cases to avoid fall through. CSCE 106

  24. Listing 4.5switch statement to determine life expectancy of a lightbulb CSCE 106

  25. switch Control Statement (cont’d) • You have to use a formula to convert ranges to an integer, so as to be able to use a switch statement. • For example, if you are asked to use a switch statement to give the following corresponding outputs for some given ranges: Final GPA Honorary Degree 3.80 <= GPA <= 4.00 Highest Honours 3.60 <= GPA < 3.80 High Honours 3.40 <= GPA < 3.60 Honours 2.00 <= GPA < 3.40 Pass with no Honours CSCE 106

  26. switch Control Statement (cont’d) Here is one example for a formula that works, together with its corresponding case statements, provided that you check the range of GPA when input before: int x = GPA *10; switch (x) { case 40: case 39: case 38: cout<<“Highest Honours”; break; case 37: case 36: cout<<“High Honours”; break; case 35: case 34: cout<<“Honours”; break; default: cout<<“Pass with no Honours”; break; } CSCE 106

  27. Next lecture will be about looping construct in C++ CSCE 106

More Related