Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved switch Statements switch (status) { case 0: compute taxes for single filers; break; case 1: compute taxes for married file jointly; break; case 2: compute taxes for married file separately; break; case 3: compute taxes for head of household; break; default: cout<< "Errors: invalid status"<<endl; }
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved switch Statement Flow Chart
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved switch Statement Rules switch (switch-expression) { case value1: statement(s)1; break; case value2: statement(s)2; break; … case valueN: statement(s)N; break; default: statement(s)-for-default; } The switch-expression must yield a value of char, byte, short, or int type and must always be enclosed in parentheses. The value1,..., and valueN must have the same data type as the value of the switch-expression. The resulting statements in the case statement are executed when the value in the case statement matches the value of the switch- expression. Note that value1,..., and valueN are constant expressions, meaning that they cannot contain variables in the expression, such as 1 + x.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved switch Statement Rules The keyword break is optional, but it should be used at the end of each case in order to terminate the remainder of the switch statement. If the break statement is not present, the next case statement will be executed. switch (switch-expression) { case value1: statement(s)1; break; case value2: statement(s)2; break; … case valueN: statement(s)N; break; default: statement(s)-for-default; } The default case, which is optional, can be used to perform actions when none of the specified cases matches the switch-expression.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Trace switch statement switch (ch) { case 'a': cout << ch; case 'b': cout << ch; case 'c': cout << ch; } Suppose ch is 'a': animation
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Trace switch statement switch (ch) { case 'a': cout << ch; case 'b': cout << ch; case 'c': cout << ch; } ch is 'a': animation
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Trace switch statement switch (ch) { case 'a': cout << ch; case 'b': cout << ch; case 'c': cout << ch; } Execute this line animation
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Trace switch statement switch (ch) { case 'a': cout << ch; case 'b': cout << ch; case 'c': cout << ch; } animation Execute this line
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Trace switch statement switch (ch) { case 'a': cout << ch; case 'b': cout << ch; case 'c': cout << ch; } animation Execute this line
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Trace switch statement switch (ch) { case 'a': cout << ch; case 'b': cout << ch; case 'c': cout << ch; } Next statement; animation Execute next statement
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Trace switch statement switch (ch) { case 'a': cout << ch; break; case 'b': cout << ch ; break ; case 'c': cout << ch ; } Suppose ch is 'a': animation
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Trace switch statement switch (ch) { case 'a': cout << ch ; break; case 'b': cout << ch ; break ; case 'c': cout << ch; } ch is 'a': animation
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Trace switch statement switch (ch) { case 'a': cout << ch ; break; case 'b': cout << ch ; break ; case 'c': cout << ch ; } Execute this line animation
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Trace switch statement switch (ch) { case 'a': cout << ch ; break; case 'b': cout << ch ; break ; case 'c': cout << ch ; } Execute this line animation
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Trace switch statement switch (ch) { case 'a': cout << ch ; break; case 'b': cout << ch ; break ; case 'c': cout << ch ; } Next statement; Execute next statement animation
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Conditional Operator if (x > 0) y = 1 else y = -1; is equivalent to y = (x > 0) ? 1 : -1; (booleanExpression) ? expression1 : expression2
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Conditional Operator, cont. (booleanExpression) ? Expression1 : expression2; The result of this conditional expression is Expression1 if booleanExpression is true otherwise the result is expression2.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Conditional Operator, cont. cout << ((num % 2 == 0) ? "num is even" : "num is odd"); max=(num1>num2) ? num1 : num2; This displays the message “num is even” if num is even, and otherwise displays “num is odd”. The larger number between variable num1 and num2 is assigned to max.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Formatting Output #include
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Formatting Output #include using namespace std; int main() { cout<<setw(8)<<"C++"<<setw(6)<<101<<endl; cout<<setw(8)<<"Java"<<setw(6)<<101<<endl; cout<<setw(8)<<"HTML"<<setw(6)<<101<<endl; system("PAUSE"); return 0;}
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Formatting Output #include using namespace std; int main() { double number= ; cout<<setw(10)<<setprecision(5)<<number; cout<<setw(10)<<setprecision(4)<<number; cout<<setw(10)<<setprecision(3)<<number; cout<<setw(10)<<setprecision(8)<<number<<endl; system("PAUSE"); return 0; } setprecision(n) : n is total number of digits before and after decimal point.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Formatting Output #include using namespace std; int main(){ double monthlyPayment= ; double totalPayment= ; cout<<fixed<<setprecision(2); cout<<setw(10)<<monthlyPayment<<endl; cout<<setw(10)<<totalPayment<<endl; system("PAUSE"); return 0;} When setprecision is used after fixed, the setprecision specifies the number of digits after the decimal point.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Operator Precedence Evaluate * 4 > 5 * (4 + 3) – 1? * 4 > 5 * 7 – > 5 * 7 – > 35 – 1 19 > 35 – 1 19 > 34 (1) Inside parentheses first (2) Multiplication (3) Multiplication (4) Addition (5) Subtraction (6) Greater than false
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Operator Precedence F var++, var-- +, - (Unary plus and minus), ++var, --var F (type) Casting F ! (Not) *, /, % (Multiplication, division, and remainder) +, - (Binary addition and subtraction) , >= (Comparison) ==, !=; (Equality) F && (Conditional AND) Short-circuit AND F || (Conditional OR) Short-circuit OR =, +=, -=, *=, /=, %= (Assignment operator)
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Enumerated Types C++ enables you to declare your own type, known as enumerated type. enum Day {MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY}; Once a type is defined, you can declare a variable of that type: Day day; The variable day can hold one of the values defined in the enumerated type. For example, the following statement assigns enumerated value MONDAY to variable day: day = MONDAY; Companion Website
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Enumerated Types As with any other type, you can declare and initialize a variable in one statement: Day day = MONDAY; Furthermore, C++ allows you to declare an enumerated type and variable in one statement. For example, enum Day {MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY} day = MONDAY; Companion Website
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Enumerated Types enum Day {MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY} day = MONDAY; Enumerated values are stored as integers in memory. By default, the values correspond to 0,1,2,…, in the order of their appearance in the list. You can assign an enumerated value with any integer value. enum Color{RED=20, GREEN=30,BLUE=40}; enum City{PARIS, LONDON, ISTANBUL=30, CAIRO}; PARIS will be assigned 0, LONDON 1, ISTANBUL 30 and CAIRO 31. Companion Website
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved #include using namespace std; int main() { enum Day {MONDAY = 1, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY} day; cout << "Enter a day (1 for Monday, 2 for Tuesday, etc): "; int dayNumber; cin >> dayNumber; switch (dayNumber) { case MONDAY: cout << "Play soccer" << endl; break; case TUESDAY: cout << "Piano lesson" << endl; break; case WEDNESDAY: cout << "Math team" << endl; break; default: cout << "Go home" << endl; } system("PAUSE"); return 0; } TestEnumeratedType.cpp