lecture4slides

lecture4slides - Todays Topics: 1. 2. 3. 4. 5. Project #2...

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

View Full Document Right Arrow Icon
Today’s Topics: 1. Project #2 Questions 2. PROPER SYNTAX ON TESTS! 3. Programming Challenge 4. The switch statement 5. Loops-continued: break and continue 6. 7. More on strings! A new web-site! http://66.27.190.186/ Leave comments or give comments the class! It’s ANONYMOUS!
Background image of page 1

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

View Full DocumentRight Arrow Icon
Programming Challenge int main(void) { int a; for (a=1;a<=2;a++) { cout << a << "\n"; int b; for (b = a; b < 3; b++) cout << "* " << b << "\n"; } cout << a << "\n"; while (a--) { cout << a << "\n"; } cout << a << "\n"; }
Background image of page 2
The switch Statement Usage: switch ( expression ) { case CONST1 : do-this; and-this; and-this; break; case CONST2: do-something; break; case CONST3: do-whatever; break; default: do-default-case; break; } The expression (e.g. 5*x+17) is evaluated once . If the expression is equal to any CONST value, execute that code until a break statement is reached. If the expression doesn’t match any CONST, run the default code. expression and CONST values must be chars ints or longs. Not doubles, strings.
Background image of page 3

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

View Full DocumentRight Arrow Icon
What if you wanted to do this? if (variable == 1) { cout << “you’re #1”; cout << “woohoo!\n”; } else if (variable == 2) cout << “you’re a deuce\n”; else if (variable == 3) cout << “third, not last\n”; else cout << “you lose\n”; Use the switch statement: switch(variable) { case 1: cout << “you’re #1”; cout << “woohoo!\n”; break; case 2: cout << “you’re a deuce\n”; break; case 3: cout << “third, not last\n”; break; default: cout << “you lose\n”; break; }
Background image of page 4
int main(void) { int num, x = 5; cout<<"How many friends do you have? "; cin >> num; switch( num ) // indentation! { case 0: cout << "Poor guy.\n"; cout << "Try harder!\n"; break; case 1: case 2: cout << "You don't "; case 3: cout << "have many friends!\n"; break; case 4: cout << "you must be busy!\n"; break; default: cout << "you're lying!\n"; break; } } Questions: 1. What happens if we move the default case to the top? 2.
Background image of page 5

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

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 20

lecture4slides - Todays Topics: 1. 2. 3. 4. 5. Project #2...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online