CS 105 Lecture 5 Logical Operators; Switch Statement Wed, Feb 16, 2011, 5:11 pm 1
16 quizzes taken Average: 37.9 Median: 40.5 Quiz 1 Results 16 Scores: 45 45 44 43 43 42 41 41 40 36 36 36 34 31 28 21 Avg 37.9 Std dev 6.8 7 6 5 4 3 2 1 Grade Distribution 45-49 40-44 35-39 30-34 25-29 20-24 Below 20 2
If statement Syntax if (condition) Must have ()!! statement(s); Body of if stmt/true Branch of if stmt Condition must evaluate to true or false. Two types of operators for conditionals: Relational: <, >, >=, <= Equality: ==,!= 3
If- statement Syntax if (condition) Must have ()!! statement(s); True Branch statement(s); False Branch Depending on condition, either do the true branch or the false branch (but not both). 4
if- Example if (mygrade >= 60) cout << You passed! << endl; cout << How about them Cubs? << endl; 5
Nested if- Statements You can use an if- statement as the statement for a true branch or false branch. The result is a nested if- statement. The inner if- is indented relative to the outer if-. if (mygrade >= 80) if (mygrade >= 90) cout << You have an A! << endl; cout << You have a B! << endl; cout << We ll give you a C. << endl; 6
Nested if- with Braces Here's the same code with explicit curly braces. if (mygrade >= 80) if (mygrade >= 90) cout << You have an A! << endl; cout << You have a B! << endl; cout << We ll give you a C. << endl; 7
Cascaded if- Cascaded if-: Use nested if- in false branch. Easier to understand than when we nest within the true branch. Examples if (mygrade > 90) cout << A! << endl; if (mygrade > 80) cout << B! << endl; if (mygrade > 70) cout << C! << endl; cout << Oh-oh! << endl; if (mygrade > 90) cout << A! << endl; if (mygrade > 80) cout << B! << endl; if (mygrade > 70) cout << C! << endl; cout << Oh-oh! << endl; 8
Decision Tree A nested if- statement implements a decision tree start at the top and follow the path determined by answers to questions. 9
Boolean Type A Boolean value is either true or false In C++, type bool Example: bool done = false;... if (currentletter == 'Z') done = true;... Conditions are Boolean if/ conditionals must evaluate to true or false, and are therefore called boolean expressions In C++, any non-zero value is considered true; any expression evaluating to zero is considered false. But we usually write true and false 10
Logical Operators A logical operator connects two or boolean more expressions together. && is logical and is logical or &&: Both operands must be true for entire expression to be true : One or both of the operands must be true for entire expression to be true. if (numstudents > MIN && numstudents < MAX) classrun = true; if (numstudents > MAX numinstructors == 0) classrun = false; 11
Operator Precedence ()! (not) *, /, % +, - (as in x-y) <, <=, >, >= (Relational Operators) ==,!= (Equality Operators) && (Logical AND) (Logical OR) = (ASSIGNMENT) 12
Order of Operations Precedence: Level of importance of operations Multiplicative operators have higher precedence than additive operators: *, /, % Higher +, - Lower Associativity: Order of operation for equal level precedence Most operators have left-to-right associativity Use parentheses to force different precedence of operations (or to emphasize actual precedence) 13
Multiple Logical Operators if ( num1 > MAX num2 == 0 && num3 == 0) cout << num1 is > MAX, or, both num2 and num3 are zero ; cout << num1 is <= MAX, " << "and, one or both of num2 and num3 are nonzero << endl; 14
Switch Statements Also Called switch/case statement Just Case in other languages Selects among several different actions Can only select from integer or character If an integer value is matched, statements under control of that case block are executed 15
Switch/Case Example int numpassengers; cout << Enter Passengers: ; cin >> numpassengers; switch(numpassengers) case 0: zeropassengers(); break; case 1: onepassenger(); break; default: manypassengers(); break; 16
Switch Case Example char menuitem; cout << "Enter Menu Selection: "; cin >> menuitem; switch (menuitem) case 'O': orderfunction(); break; case 'C': checkoutfunction(); break; default: errorfunction(); break; 17
Announcements Exam 1 Next Week in Lecture 50 minutes 10 % of Total Grade Covers Everything through Lecture this Week Quiz 1 Topics (terminology, variables, constants, basics) Type casting if-then- Compound statements Relational operators Logical operators Switch/Case Statements 18