130 likes | 262 Views
This guide covers essential programming concepts in C++, including nested if-else statements, boolean types, and switch statements. You will learn to evaluate grades using conditional statements, understand the boolean data type as either true or false, and utilize logical operators to connect expressions. Additionally, you will explore switch statements for selecting between multiple actions based on integer or character input. Prepare effectively for Exam 1 next week by reviewing these key topics, which encompass all content through Lab 4.
E N D
Quiz 1 Exam 1 Next Week
Nested if Statements 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;
if/else (Cascaded) • Sequence of if/else Statements • Example: if (myGrade > 90) cout << “A!” << endl; else if (myGrade > 80) cout << “B!” << endl; else if (myGrade > 70) cout << “C!” << endl; else cout << “Oh-oh!” << endl;
Boolean Type • A Boolean Value Is One of Either “True” or “False” • In C++, Type bool • Example: bool done = false; . . . if (currentLetter == ‘Z’) done = true; else done = false; … if (done) return(0); . . . • 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
Logical Operators • A Logical Operator Is One Used to Further Specify True or False in an Expression • Connects Two or More Expressions Together • && Is Logical “AND” • || Is Logical ‘OR” • &&: Both Operands Must Be True for Entire Expression to Be True • ||: Only One (or Both) of the Operands Must Be True for Entire Expression to Be True
Logical Operators if (numStudents > MIN && numStudents < MAX) classRun = true; if (numStudents > MAX || numInstructors == 0) classRun = false;
Operator Precedence • () • ! (not) • *, /, % • +, - • <, <=, >, >= (Relational Operators) • ==, != (Equality Operators) • && (Logical AND) • || (Logical OR) • = (ASSIGNMENT)
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 Differing Precedence of Operations
Multiple Logical Operators if ( num1 > MAX || num2 == 0 && num3 == 0) cout << “num1 is MAX or something is 0”; cout << “I think…..” << endl;
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
Switch/Case Example const double BASERATE = 104.50; double rate; int numPassengers; cout << “Enter Passengers: “; cin >> numPassengers; switch(numPassengers) { case 2: rate = BASERATE * 0.80; break; case 4: rate = BASERATE * 0.75; break; case 5: rate = BASERATE * 0.55; break; default: rate = BASERATE; break; }
Switch Case Example char menuItem; cout << "Enter Menu Selection: "; cin >> menuItem; switch(menuItem) { case 'O': case ‘o': /* code to do ordering goes here*/ break; case 'C': case ‘c': /* code to do checkout goes here*/ break; default: cout << “Unknown option” << endl; break; }
Announcements • Exam 1 Next Week in Lecture • Everything through Lab 4 • 50 minutes • 10 % of Total Grade • Covers Everything through Lecture this Week • Quiz 1 Topics (terminology, variables, constants, basics) • if-then-else • Compound statements • Relational operators • Logical operators • Switch/Case Statements