02 - C+ Summary of Last Week Usage of const const PI =...

Info iconThis preview shows pages 1–27. Sign up to view the full content.

View Full Document Right Arrow Icon
C++
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Summary of Last Week Usage of const const PI = 3.14159; const datatype CONSTANTNAME = VALUE ; const double PI = 3.14159; const int PI = 3.14159;
Background image of page 2
Summary of Last Week #include <iostream> using namespace std; int main() { const PI = 3.14; const int PI1 = 3.14; const int &PI2 = 3.14; cout << PI << endl; cout << PI1 << endl; cout << PI2 << endl; return 0; }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Summary of Last Week
Background image of page 4
Summary of Last Week Programming Style
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Chapter 3 Control Statements
Background image of page 6
Objectives To combine conditions using logical operators (&& , || , and ! ) (§3.4) To implement selection control using if ... else statements (§3.5). To implement selection control using nested if statements (§3.6). To implement selection control using switch statements (§3.9). To write expressions using the conditional operator (§3.10). To display formatted output using the stream manipulators (§3.11). To know the rules governing operand evaluation order, operator precedence, and operator associativity (§3.12). To declare an enumerated type for an ordered list of values (§3.14)
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
if if (i > k) if (i > j) if (i > l) cout << "B"; else cout << "C"; else cout << "D"; else cout << "E"; Very very very BAD style !!! Never write things like that !!! i = 3; k = 1; j = 2; l = 5;
Background image of page 8
if (i > k) { if (i > j) { if (i > l) { cout << "B"; } else { cout << "C"; } } else { cout << "D"; } } else { cout << "E"; } GOOD style Use { } even if there is only one statement i = 3; k = 1; j = 2; l = 5;
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
if if (radius >= 0 ); { area = radius * radius * PI; cout << "The area" << " is " <<area; } if (radius >= 0) { }; area = radius * radius * PI; cout <<"The area" << " is " << area; Logic Error
Background image of page 10
if int status = 3; if (status = 0) { cout<<“A!"<<endl; } else if (status = 1) { cout<<“B!"<<endl; } else { cout << "C “; } return (0); Logic error
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
if int status = 3; if (status = 0) { cout<<“A!"<<endl; } if (status = 1) { cout<<“B!"<<endl; } if (status = 3) { cout << "C “<<endl; } return (0);
Background image of page 12
switch switch (ch) { case 'a': cout << ch; case 'b': cout << ch; case 'c': cout << ch; } Suppose ch is 'a':
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
switch (ch) { case 'a': cout << ch; case 'b': cout << ch; case 'c': cout << ch; } ch is 'a': switch
Background image of page 14
Execute this line switch (ch) { case 'a': cout << ch; case 'b': cout << ch; case 'c': cout << ch; } switch
Background image of page 15

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
switch (ch) { case 'a': cout << ch; case case 'c': cout << ch; } Execute this line switch
Background image of page 16
Execute this line switch (ch) { case 'a': cout << ch; case 'b': cout << ch; case 'c': cout << ch; } switch
Background image of page 17

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Execute next statement switch (ch) { case 'a': cout << ch; case 'b': cout << ch; case 'c': cout << ch; } switch
Background image of page 18
switch (ch) { case 'a': cout << ch; break ; case 'b': cout << ch; break ; case 'c': cout << ch; break ; } ch is 'a': switch
Background image of page 19

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
switch (ch) { case 'a': cout << ch; break ; case 'b': cout << ch; break ; case 'c': cout << ch; break ; } Execute this line switch
Background image of page 20
switch (ch) { case 'a': cout << ch; break ; case 'b': cout << ch; break ; case 'c': cout << ch; break ; } Execute this line switch
Background image of page 21

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
switch (ch) { case 'a': cout << ch; break ; case 'b': cout << ch; break ; case 'c': cout << ch; break ; } Execute next statement switch
Background image of page 22
Conditional Expression y = (x > 0) ? 1 : -1; (booleanExp) ? exp1 : exp if (x > 0) { y = 1; } else { y = -1; }
Background image of page 23

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Conditional Expression (x > y) ? cout << "x > y" : cout << "x <= y";
Background image of page 24
Enumerated Types enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY }; Day day; day = MONDAY ; define an enumerated type declare a variable of that type The variable can hold one of the values defined in the enumerated type
Background image of page 25

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Caution 4
Background image of page 26