Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3 More Flow of Control Goals: To analyze the use of Boolean expressions To analyze the use of Boolean expressions To introduce the notion of enumerated.

Similar presentations


Presentation on theme: "Chapter 3 More Flow of Control Goals: To analyze the use of Boolean expressions To analyze the use of Boolean expressions To introduce the notion of enumerated."— Presentation transcript:

1

2 Chapter 3 More Flow of Control Goals: To analyze the use of Boolean expressions To analyze the use of Boolean expressions To introduce the notion of enumerated types To introduce the notion of enumerated types To explore the switch statement as an alternative to multiway if-else statements To explore the switch statement as an alternative to multiway if-else statements To examine the for -statement as a looping option To examine the for -statement as a looping option To demonstrate the design of good nested loops To demonstrate the design of good nested loops To view the conditional statement as a alternative to a simple if-else statement To view the conditional statement as a alternative to a simple if-else statement

3 Chapter 3CS 140Page 2 Precedence Rules for Boolean Expressions With the addition of Boolean operators, the precedence rules that C++ uses to evaluate expressions become more complex. Parentheses still take the highest precedence Parentheses still take the highest precedence Multiplication, division, and modulus come second. Multiplication, division, and modulus come second. Addition and subtraction take the next precedence. Addition and subtraction take the next precedence. Precedence ties are still handled in left-to-right fashion. Precedence ties are still handled in left-to-right fashion. Order-related inequality operators (, =) are next. Order-related inequality operators (, =) are next. The pure equality/inequality (==, !=) operators are next. The pure equality/inequality (==, !=) operators are next. The Boolean AND operator (&&) takes the next precedence. The Boolean AND operator (&&) takes the next precedence. The Boolean OR operator (||) takes the lowest precedence. The Boolean OR operator (||) takes the lowest precedence.

4 Chapter 3CS 140Page 3 Precedence Rules Examples int x = 7, y = 7, z = 7; if (x == y == z) cout << “YES”; cout << “YES”;else cout << “NO”; cout << “NO”; int a = 5, b = 4, c = 3; if (a < b < c) cout << “YES”; cout << “YES”;else cout << “NO”; cout << “NO”; Output: NO The left equality is checked and evaluates to true (numerical 1), which is not equal to the z-value! Output: NO The left equality is checked and evaluates to true (numerical 1), which is not equal to the z-value! Output: YES The left inequality is checked and evaluates to false (numerical 0), which is less than the c-value! Output: YES The left inequality is checked and evaluates to false (numerical 0), which is less than the c-value!

5 Chapter 3CS 140Page 4 Boolean Expressions - Short-Circuit Evaluation of && When a Boolean expression using the && operator is evaluated, the first subexpression is evaluated first. if it evaluates to false, then the second subexpression is ignored. if ((x != 0) && (y/x > 1)) z = 100; z = 100;else z = -1; z = -1; if ((y/x > 1) && (x != 0)) z = 100; z = 100;else z = -1; z = -1; When this code segment is encountered and x ’s value is zero, z will be assigned either the value 100 or the value -1. When this code segment is encountered and x ’s value is zero, the program crashes due to the attempt to divide by zero!

6 Chapter 3CS 140Page 5 Boolean Expressions - Short-Circuit Evaluation of || When a Boolean expression using the || operator is evaluated, the first subexpression is evaluated first. if it evaluates to true, then the second subexpression is ignored. if ((ct == 0) || (total/ct > 70)) cout << “NO PROBLEMO”; cout << “NO PROBLEMO”; if ((total/ct > 70) || (ct == 0)) cout << “NO PROBLEMO”; cout << “NO PROBLEMO”; When this code segment is encountered and ct ’s value is zero, the message is output. When this code segment is encountered and ct ’s value is zero, the program crashes due to the attempt to divide by zero!

7 Chapter 3CS 140Page 6 Enumerated Types To enhance the readability of one’s code, a programmer can define an enumerated type, which consists of a set of integer constants. #include using namespace std; enum MonthNumber { JANUARY = 1, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER }; void main() { MonthNumber monthCount = JANUARY; double monthlyRate = 12.34, totalCost = 0.0; cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); cout << "Computing Annual Costs:\n\n"; while (monthCount <= DECEMBER) { totalCost += monthlyRate; monthCount = MonthNumber(monthCount + 1); } cout << endl << endl << "Total: $" << totalCost << endl << endl; return; }

8 Chapter 3CS 140Page 7 switch Statements C++ switch statements provide a simple alternative to the use of convoluted nested if-else statements. if (month == 2) { if (year % 4 == 0) if (year % 4 == 0) daysInMonth = 29; daysInMonth = 29; else else daysInMonth = 28; daysInMonth = 28;} else if ((month == 4) || (month == 6) || (month == 6) || (month == 9) || (month == 11)) (month == 9) || (month == 11)) daysInMonth = 30; daysInMonth = 30;else daysInMonth = 31; daysInMonth = 31; switch (month) { case 2: { case 2: { if (year % 4 == 0) if (year % 4 == 0) daysInMonth = 29; daysInMonth = 29; else else daysInMonth = 28; daysInMonth = 28; break; break; } case 4: case 4: case 6: case 6: case 9: case 11: { daysInMonth = 30; break; } case 9: case 11: { daysInMonth = 30; break; } default: { daysInMonth = 31; break; } default: { daysInMonth = 31; break; }}

9 Chapter 3CS 140Page 8 #include using namespace std; void main() { char letter; bool gotAVowel = false; do { cout << "Please enter a letter: "; cin >> letter; switch(letter) { case 'a': case 'A': cout << "Apple\n";gotAVowel = true; break; case 'e': case 'E': cout << "Egg\n"; gotAVowel = true; break; case 'i': case 'I': cout << "Iodine\n";gotAVowel = true; break; case 'o': case 'O': cout << "Oval\n";gotAVowel = true; break; case 'u': case 'U': cout << "Upper\n"; gotAVowel = true; break; default: cout << "That is not a vowel. Please try again.\n"; } }while(!gotAVowel); } The break statements ensure that the switch statement is exited once the appropriate case is handled. Without the break statements, all cases might execute. The default case is executed if no other case value matches.

10 Chapter 3CS 140Page 9 Conditional Statements C++ conditional statements provide a concise alternative to the use of relatively simple if-else statements. if (year % 4 == 0) daysInMonth = 29; daysInMonth = 29;else daysInMonth = 28; daysInMonth = 28; daysInMonth = (year % 4 == 0) ? 29 : 28; if (val > 0) cout << “GOOD”; cout << “GOOD”;else cout << “BAD”; cout << “BAD”; (val > 0) ? (cout 0) ? (cout << “GOOD”) : (cout << “BAD”);

11 Chapter 3CS 140Page 10 The for Statement The C++ for statement is designed to facilitate looping when the control of the loop contains three standard features: An initialization action that is performed just before the loop is started the first time. An initialization action that is performed just before the loop is started the first time. A Boolean expression that is checked just before entering the loop at each iteration. A Boolean expression that is checked just before entering the loop at each iteration. An update action that is performed just after each iteration of the loop is completed. An update action that is performed just after each iteration of the loop is completed. for (initAction; booleanExp; updateAction) bodyOfLoop bodyOfLoop

12 Chapter 3CS 140Page 11 #include using namespace std; const double PAY_RATE = 10.15; void main() { double payForWeek = 0.0; int dayOfWeek; double hoursWorked; for(dayOfWeek = 1; dayOfWeek <= 7; dayOfWeek++) { cout << "How many hours did you work on day " << dayOfWeek << "? "; cin >> hoursWorked; (dayOfWeek == 1) ? (payForWeek += 1.5 * hoursWorked * PAY_RATE) : (payForWeek += hoursWorked * PAY_RATE); } cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); cout << endl << endl << "Your Pay For \n" << "The Week Is $" << payForWeek << endl << endl; } dayOfWeek is initialized to 1 when the for -loop is first encountered. This inequality is checked just before the body of the loop is entered (or re-entered). This increment statement is executed right after each execution of the body of the loop. A for -loop Example

13 Chapter 3CS 140Page 12 #include using namespace std; void main() { int iterationNbr, repetitionNbr, indentSpaceNbr; for (iterationNbr = 1; iterationNbr <= 25; iterationNbr++) for (repetitionNbr = 1; repetitionNbr <= 1000; repetitionNbr++) { cout << '\r'; for (indentSpaceNbr = 1; indentSpaceNbr <= iterationNbr; indentSpaceNbr++) cout << ' '; cout << "HAPPY NEW YEAR!"; } cout << endl << endl; return; } Nested for -loops


Download ppt "Chapter 3 More Flow of Control Goals: To analyze the use of Boolean expressions To analyze the use of Boolean expressions To introduce the notion of enumerated."

Similar presentations


Ads by Google