Download presentation
Presentation is loading. Please wait.
Published byMadlyn Thomas Modified over 9 years ago
1
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 01360972001 Chapter 3 Selections
2
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 2 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; }
3
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 3 switch Statement Flow Chart
4
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 4 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.
5
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 5 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.
6
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 6 Trace switch statement switch (ch) { case 'a': cout << ch; case 'b': cout << ch; case 'c': cout << ch; } Suppose ch is 'a': animation
7
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 7 Trace switch statement switch (ch) { case 'a': cout << ch; case 'b': cout << ch; case 'c': cout << ch; } ch is 'a': animation
8
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 8 Trace switch statement switch (ch) { case 'a': cout << ch; case 'b': cout << ch; case 'c': cout << ch; } Execute this line animation
9
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 9 Trace switch statement switch (ch) { case 'a': cout << ch; case 'b': cout << ch; case 'c': cout << ch; } animation Execute this line
10
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 10 Trace switch statement switch (ch) { case 'a': cout << ch; case 'b': cout << ch; case 'c': cout << ch; } animation Execute this line
11
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 11 Trace switch statement switch (ch) { case 'a': cout << ch; case 'b': cout << ch; case 'c': cout << ch; } Next statement; animation Execute next statement
12
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 12 Trace switch statement switch (ch) { case 'a': cout << ch; break; case 'b': cout << ch ; break ; case 'c': cout << ch ; } Suppose ch is 'a': animation
13
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 13 Trace switch statement switch (ch) { case 'a': cout << ch ; break; case 'b': cout << ch ; break ; case 'c': cout << ch; } ch is 'a': animation
14
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 14 Trace switch statement switch (ch) { case 'a': cout << ch ; break; case 'b': cout << ch ; break ; case 'c': cout << ch ; } Execute this line animation
15
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 15 Trace switch statement switch (ch) { case 'a': cout << ch ; break; case 'b': cout << ch ; break ; case 'c': cout << ch ; } Execute this line animation
16
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 16 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
17
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 17 Conditional Operator if (x > 0) y = 1 else y = -1; is equivalent to y = (x > 0) ? 1 : -1; (booleanExpression) ? expression1 : expression2
18
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 18 Conditional Operator, cont. (booleanExpression) ? Expression1 : expression2; The result of this conditional expression is Expression1 if booleanExpression is true otherwise the result is expression2.
19
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 19 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.
20
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 20 Formatting Output #include
21
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 21 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;}
22
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 22 Formatting Output #include using namespace std; int main() { double number=12.34567; 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.
23
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 23 Formatting Output #include using namespace std; int main(){ double monthlyPayment=345.4567; double totalPayment=78676.887234; 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.
24
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 24 Operator Precedence Evaluate 3 + 4 * 4 > 5 * (4 + 3) – 1? 3 + 4 * 4 > 5 * 7 – 1 3 + 16 > 5 * 7 – 1 3 + 16 > 35 – 1 19 > 35 – 1 19 > 34 (1) Inside parentheses first (2) Multiplication (3) Multiplication (4) Addition (5) Subtraction (6) Greater than false
25
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 25 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)
26
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 26 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
27
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 27 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
28
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 28 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
29
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 29 #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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.