csi 201 introduction to
play

CSI 201 - Introduction to expressions will be discussed Complex - PowerPoint PPT Presentation

Introduction A few more details about boolean CSI 201 - Introduction to expressions will be discussed Complex if-else statements (which were Computer Science already covered) If statements embedded inside if statements (see ex6.cpp


  1. Introduction � A few more details about boolean CSI 201 - Introduction to expressions will be discussed � Complex if-else statements (which were Computer Science already covered) � If statements embedded inside if statements (see ex6.cpp from Chapter 2 lecture notes) � The switch statement Chapter 7 � The for statement More Flow of Control � The C++ library Random Number Generator Brian R. King (not covered in the book) Instructor 3/24/2006 2 CSI 201 -- Chapter 07 More boolean expressions Order of evaluation Boolean expression have an order of evaluation just like arithmetic expressions � � We covered most of section 7.1 already: do. We can create an updated list of precedence rules combining the logical and � � C++ type bool comparison operators with the arithmetic operators. The precedence rules are in the following order, with the most important listed � � C++ keywords true and false first : +, -, ++, --, ! Unary Operators � � How to declare and use boolean variables *, /, % � Arithmetic Operators +, - � � How to return a boolean value from a function <, >, <=, >= � Comparison Operators ==, != � � How boolean expressions convert to values of && � Logical Operators || � type int . Therefore, many complex boolean expressions that are a combination of both � boolean and arithmetic operators can still be evaluated: Example: � if (x + 3 > 10 || y / 10 < 1) // Notice .. no parenthesis required � Let's look more at how C++ evaluates complex � cout << "OK"; boolean expressions… Parenthesis can override the general order of evaluations. � 3/24/2006 CSI 201 -- Chapter 07 3 3/24/2006 CSI 201 -- Chapter 07 4

  2. Short-circuit evaluation How can we use this to our advantage? Boolean expressions joined together by logical operators are evaluated from left to right. � Consider a boolean expression that uses a logical OR operator ( || ) � double pounds, price; int x = 4, y = 0; cout << "Enter the price of meat: "; if (x > 0 || y < 0) cout << "ALPHA"; cin >> price; else cout << "BETA"; cout << "Enter the number of pounds: "; What is the output of the above code? Why? � cin >> pounds; if ((pounds != 0) && ((price / pounds) > 10.0)) Do both expressions (x > 0) and (y < 0) need to be evaluated in order to determine � the value of the entire expression (x > 0 || y < 0) if we know (x > 0) is true? cout >> "WOW, that's expensive meat!" < endl; Why? � � This prevents a possible divide by zero run- C++ takes advantage of this to reduce the running time of your program by eliminating � unnecessary computation. time error from occurring, thereby preventing The logical AND operator (&&) also has a shortcut evaluation for a false evaluation. � Example: your program from crashing. 3/24/2006 5 3/24/2006 6 CSI 201 -- Chapter 07 CSI 201 -- Chapter 07 Examples Boolean expression examples Determine the value, either true or false , A division by zero in your program will produce a run-time error, and � � will cause your program to crash. of the following expressions, or does it Does the following code produce a division by zero error? � produce an error?: int j; j = -1; int count = 0, limit = 10; if ((j != -1) && (1/(j+1) > 10)) int x, y; cout << i << endl; 1. ((count == 0) && (limit < 20)) 2. (count == 0 && limit < 20) 3. !(count == 12) What about this code? � 4. (count == 1) && (x < y) int j; 5. ((limit/count) > 7) && (count != 0) j = 1; if ((j > 0) && (1/(j-1) > 10)) 6. (count != 0) && ((limit/count) > 7) cout << i << endl; Answers given in class… 3/24/2006 CSI 201 -- Chapter 07 7 3/24/2006 CSI 201 -- Chapter 07 8

  3. Recap of if and if-else statements Nesting if and if-else statements We discussed if-else statements in Chapter 2, and also discussed embedded if statements within You can have an if statement as one of your statements inside of � � if statements. To recap… an if . For example: if statement: � if ( boolean_expression ) if ( boolean_expression ) if ( another_boolean_expression ) statement ; // if with a single statement statement ; OR if ( boolean_expression ) � This is logically equivalent to: { if ( boolean_expression && another_boolean_expression ) statement_1 ; // if with a block statement statement ; statement_2 ; … statement_n ; � Or, you can have another if-else statement as your statement } attached to the else : if-else statement: � if ( boolean_expression_a ) if ( boolean_expression ) statement_a ; statement_a ; // if statement (single) else else statement_b ; // else statement if ( boolean_expression_b ) statement_b ; Again, you could have used block statements in place of each statement attached to the if or the else or both. else statement_c ; 3/24/2006 9 3/24/2006 10 CSI 201 -- Chapter 07 CSI 201 -- Chapter 07 Nested if and if-else continued… Be careful with nested if-else There is no limit to the hierarchy of if and if-else statements which � Suppose you have: � you can have nested. double fuel_gauge = 0.8; You can implement several branches of execution by joining if-else � � What is the output of the following: statements together: if (x <= 1) if (fuel_gauge < 0.75) cout << "x is less than or equal to 1"; if (fuel_gauge < 0.25) else if (x == 2) cout << "Low fuel… less than a quarter left!" << endl; cout << "x is 2"; else else if (x >= 3 && x <= 5) cout << "Fuel over 3/4 full! Keep going!" << endl; cout << "x is between 3 and 5."; � Clearly not what the programmer intended. else To correct the logic error, use braces: cout << "x is greater than 5." if (fuel_gauge < 0.75) General rule of thumb is to understand that the compiler always � pairs an else with the nearest previous if that is not already paired { if (fuel_gauge < 0.25) with some other else . cout << "Low fuel… less than a quarter left!" << endl; Be safe! Use braces to create block statements to connect with � } specific if statements to eliminate any guessing on your part. else cout << "Fuel over 3/4 full! Keep going!" << endl; 3/24/2006 CSI 201 -- Chapter 07 11 3/24/2006 CSI 201 -- Chapter 07 12

  4. ex1.cpp The switch statement double tax(int net_income) The switch statement provides a more formalized mechanism to � { implement a multiway if-else statement. double five_percent_tax, ten_percent_tax; However, a switch statement is restricted to comparing an expression � to constants . if (net_income <= 15000) return 0; The general syntax is: � switch ( controlling_expr ) else if ((net_income > 15000) && (net_income <= 25000)) //return 5% of amount over $15,000 { return (0.05*(net_income - 15000)); case Constant_1 : else //net_income > $25,000 Statement_sequence_1; { break ; case Constant_2: //five_percent_tax = 5% of income from $15,000 to $25,000. five_percent_tax = 0.05*10000; Statement_sequence_2; break ; //ten_percent_tax = 10% of income over $25,000. … ten_percent_tax = 0.10*(net_income - 25000); case Constant_n : return (five_percent_tax + ten_percent_tax); Statement_sequence_n ; break ; } default : } Default_statement_sequence ; } 3/24/2006 13 3/24/2006 14 CSI 201 -- Chapter 07 CSI 201 -- Chapter 07 ex2.cpp -- switch statement example The switch statement explained int main( ) Your "controlling expression" must evaluate to an integral type . This � { includes one of the following: char grade; cout << "Enter your midterm grade and press return: "; bool � cin >> grade; an enum constant (not covered), switch (grade) � { short, int, long � case 'A': char . � cout << "Excellent. " << "You need not take the final.\n"; The controlling expression is evaluated first. � break; Its value will be compared with each constant in a case statement. case 'B': � cout << "Very good. "; If your program finds a match, it executes the statements following that cout << "Still have to take final.\n"; � break; case , up to the first break statement encountered, or to the end of the case 'C': switch statement, whichever comes first. cout << "Passing.\n"; break; If your program does NOT find a match, then your program jumps to the � case 'D': statements following the default label in the switch statement. case 'F': cout << "Not good. " If your program does not include the default label, and there is no � << "Go study.\n"; match found in the case statements presented, then your program break; default: simply jumps out of the switch statement to the code following the cout << "That is not a possible grade.\n"; closing bracket of the switch statement. } cout << "End of program.\n"; return 0; } 3/24/2006 CSI 201 -- Chapter 07 15 3/24/2006 CSI 201 -- Chapter 07 16

Recommend


More recommend