Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS 105 Lecture 5 Logical Operators; Switch Statement Wed, Feb 16, 2011, 5:11 pm.

Similar presentations


Presentation on theme: "1 CS 105 Lecture 5 Logical Operators; Switch Statement Wed, Feb 16, 2011, 5:11 pm."— Presentation transcript:

1 1 CS 105 Lecture 5 Logical Operators; Switch Statement Wed, Feb 16, 2011, 5:11 pm

2 2 Quiz 1 Results 16 quizzes taken Average: 37.9 Median: 40.5 16 Scores: 45 4544 43 43 42 41 41 4036 36 3634 312821 Avg 37.9Std dev 6.8

3 3 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: ==, != If statement

4 4 Syntax  if (condition) ← Must have ()!! { statement(s); ← True Branch } else { statement(s); ← False Branch } Depending on condition, either do the true branch or the false branch (but not both). If-else statement

5 5 if-else Example if (myGrade >= 60) { cout << “You passed!” << endl; } else { cout << “How about them Cubs?” << endl; }

6 6  if (myGrade >= 80) if (myGrade >= 90) cout << “You have an A!” << endl; else cout << “You have a B!” << endl;else cout << “We’ll give you a C.” << endl; You can use an if-else statement as the statement for a true branch or false branch. The result is a nested if-else statement. The inner if-else is indented relative to the outer if-else. Nested if-else Statements

7 7 Here's the same code with explicit curly braces.  if (myGrade >= 80)  { if (myGrade >= 90) { cout << “You have an A!” << endl; } else { cout << “You have a B!” << endl; } }else  { cout << “We’ll give you a C.” << endl;  } Nested if-else with Braces

8 8 Cascaded if-else: Use nested if-else in false branch. Easier to understand than when we nest within the true branch. Examples  if (myGrade > 90) cout 80) cout 70) cout << “C!” << endl; else cout << “Oh-oh!” << endl; Cascaded if-else  if (myGrade > 90) cout 80) cout 70) cout << “C!” << endl; else cout << “Oh-oh!” << endl;

9 9 Decision Tree A nested if-else statement implements a decision tree — start at the top and follow the path determined by answers to questions.

10 10 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/else 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

11 11 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 || numInstructors == 0) classRun = false; Logical Operators

12 12 Operator Precedence () ! (not) *, /, % +, - (as in x-y), >= (Relational Operators) ==, != (Equality Operators) && (Logical AND) || (Logical OR) = (ASSIGNMENT)

13 13 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)

14 14 Multiple Logical Operators if ( num1 > MAX || num2 == 0 && num3 == 0) cout MAX, or, both num2 and num3 are zero”; else cout << “num1 is <= MAX, " << "and, one or both of num2 and num3 are nonzero” << endl;

15 15 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

16 16 Switch/Case Example int numPassengers; cout << “Enter Passengers: “; cin >> numPassengers; switch(numPassengers) { case 0: zeroPassengers(); break; case 1: onePassenger(); break; default: manyPassengers(); break; }

17 17 Switch Case Example char menuItem; cout << "Enter Menu Selection: "; cin >> menuItem; switch (menuItem) { case 'O': orderFunction(); break; case 'C': checkoutFunction(); break; default: errorFunction(); break; }

18 18 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-else Compound statements Relational operators Logical operators Switch/Case Statements


Download ppt "1 CS 105 Lecture 5 Logical Operators; Switch Statement Wed, Feb 16, 2011, 5:11 pm."

Similar presentations


Ads by Google