Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3 Selection Statements

Similar presentations


Presentation on theme: "Chapter 3 Selection Statements"— Presentation transcript:

1 Chapter 3 Selection Statements
Boolean Logic boolean variables can have only 2 possible values -- true or false bool flag = true; Relational Operators Ordering < <= > >= Equality == != Warning: Don’t use = to compare! Evaluate: (7 == 7) (7 != 7) (4 >= 5) (4 <= (5 + 3)) (‘A’ == ‘a’) (‘B’ >= ‘A’)

2 IF Statements Syntax: if (BooleanExpression) Single C++ Statement; where Statement is any C++ statement. Semantics: If BooleanExpression is true, then execute the Single C++ Statement. (Otherwise, skip the Statement.) a = 4; b = 5; if (a > b) a = a + b; cout << a; cout << b;

3 What if we want to execute several statements in the then-part?
Make a compound statement by using { and } if (num1 > num2) { num1 = num1 + 1; num2 = num2 - 1; } //if What is the output of these C++ statements? (i) int x = 7; if(x > 8) x = x * 2; cout << x; } cout << “Bye!”; (ii) int x = 7;

4 Write code that reads in 2 numbers, and then prints the larger number, followed by the smaller number. cout << “enter two numbers ”; cin >> num1 >> num2; if (num1 > num2) { cout << num1 << “ is larger”; cout << num2 << “ is smaller”; } if (num1 <= num2) cout << num1 << “ is smaller”; cout << num2 << “is larger”;

5 if (BooleanExpression) Statement1; else Statement2; Symantics
Can you write code to just print out the larger of the two? if … else Syntax if (BooleanExpression) Statement1; else Statement2; Symantics If BooleanExpression is true, execute Statement1; otherwise, execute Statement2. if (num1 > num2) cout << "Larger one:" << num1; cout << "Larger one:" << num2;

6 Boolean Operators Truth Table Examples AND (&&) P Q (P && Q)
True True True True False False False True False False False False OR (||) P Q (P || Q) True False True False True True NOT (!) P !(P) True False False True

7 Operator Precedence Chart
First: the unary operators: +, -, and ! Second: the binary arithmetic operators: *, /, % Third: the binary arithmetic operators: +, - Fourth: the comparison operators: <, >, <=, >= Fifth: the Boolean operators: ==, != Sixth: the Boolean operator && Seventh: the Boolean operator || Eighth: the assignment operator = TIP: use the ()s for an expression to make the meaning of the expression perfectly clear.

8 What is the output of these C++ statements with the given values of x and y?
if ((x >= y) || (x == 3)) { x = x - 1; y = y + 1; } //if else x = x + 1; y = y - 1; } //else cout << x << endl; cout << y; Case 1. x = 5 and y = 3 Case 2. x = 3 and y = 5 Case 3. x = 4 and y = 5

9 Nested if…else Statements
if (cond1) statement1; else if (cond2) statement2; else if (cond3) statement3; else statement4; if (temp >= 100) ans = “Very Hot”; else if (temp >= 90) ans = “Hot”; else if (temp >= 70) ans = “Warm”; else if (temp >= 50) ans = “Cool”; ans = “Cold”; Write code that assigns a letter grade for a student, based on his/her score.

10 #include <iostream>
use namespace std; int main() { int score; char letterGrade; cout << "Enter your score "; cin >> score; if (score > 89) letterGrade = ‘A’; else if (score > 79) letterGrade = ‘B’; else if (score > 69) letterGrade = ‘C’; else if (score > 59) letterGrade = ‘D’; else letterGrade = ‘F’; cout << "Your letter grade is :" << letterGrade; return 0; }

11 if (gender == ‘M’) if (age < 21) premium = ; else premium = ; IMPORTANT: an else is matched with the most recent non-matched if!!! Indentation does NOT matter { } //if

12 switch statements The switch statement provides an alternative to nested if/else statements. Syntax: switch (switch-expression) { case value1: statement(s)1; break; case value2: statement(s)2; case valueN: statement(s)N; default: statement(s)-for-default; }

13 Rules of switch statement
The switch-expression must yield a value of int (byte, long) or char type since each case evaluates an == test. The value1, …, and valueN must have the same data type as the value of the switch-expression. They cannot contain variables in the expression. The keyword break is optional. The break statement immediately ends the switch statement. The case statements are checked in sequential order, but the order of the cases (including the default case) does not matter. However, it is good programming style to follow the logical sequence of the cases and place the default case at the end. See pages for more detail.

14 int score = ???; switch (score/10) { case 10: case 9: letterGrade = ‘A’; break; case 8: letterGrade = ‘B’; case 7: letterGrade = ‘C’; case 6: letterGrade = ‘D’; default: letterGrade = ‘F’; } //switch cout << “Your letter grade is “ << letterGrade); //Rewrite using nested if-else.

15 Write an equivalent nested if-else.
//suppose score is out of 100 ans = score / 10; switch (ans) { case 0: case 1: case 2: case 3: case 4: case 5: cout << “Grade is F”; break; case 6: cout << “Grade is D”; case 7: cout << “Grade is C”; case 8: cout << Grade is B”; case 9: case 10: cout << Grade is A”; default: cout << “invalid grade!”; } // switch Write an equivalent nested if-else.

16 Formatting Floating Point Output
setprecision(n) is used to specify the number of digits to the right of the decimal point. fixed specifies that trailing 0s will be printed. Using setprecision(2) will allow a currency look to your floating point output. cout << “Your total is $” << fixed << setprecision(2) << total << endl; setwidth(n) is used to specify the width of the output field. Using them together you can generate tables of numbers where the decimal places all match up. cout << setwidth(10) << fixed << setprecision(4) << num << endl; Tells the computer to output num with 5 spaces or digits to the left of the decimal point and 4 digits to the right. The decimal point itself counts as part of the width. Must #include <iomanip> for these to work.

17 C++ string The string datatype in C++ is actually an object of the class string. We can use it as a primitive variable, but it is not a primitive. #include <string> int main() { string first; cout << “enter first name “; cin >> first; cout << “you entered “ << first ; return 0; }

18 Chapter 3 Skip Section 3.10 for now Review Questions 3.1 – 3.4 3.14 – 3.20 3.22 – 3.23 3.27 3.33 – 3.34


Download ppt "Chapter 3 Selection Statements"

Similar presentations


Ads by Google