Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS114-009 Class 05 Topics  Selection: switch statement Announcements  Read pages 74-83, 110-116, 118-138.

Similar presentations


Presentation on theme: "CS114-009 Class 05 Topics  Selection: switch statement Announcements  Read pages 74-83, 110-116, 118-138."— Presentation transcript:

1 CS114-009 Class 05 Topics  Selection: switch statement Announcements  Read pages 74-83, 110-116, 118-138

2 Quick note: Comments In C++ to add comments to your program for readability:  If comment the rest of a line use // int a; //this variable hold the age  Or use /* and */ to surround comment (this is good if comment > 1 line long) /* This program was written by me and it is due on Monday */

3 Selection statements Two alternatives decision True clause False clause

4 Nested IF’s decision True clause False clause decision True clause False clause decision True clause False clause decision True clause False clause And so on… Multiple alternatives

5 Example int num; cin>>num; if (num<10){ cout<<"SMALL"<<endl; if (num%2==0){ cout<<"YEA"<<endl; if (num>5){ cout<<“W00T!"<<endl; if (num==8) cout<<num/3<<endl; } else cout<<"DUH HUH"<<endl; } else cout<<"BOO"<<endl; } else cout<<num<<endl; Note that not all decisions have an else. Trace this code with an input of 6. What is the output? No else

6 Class Exercise Copy and paste the following C++ file into a new project:  On our class web page under “Sample Programs” get “Sept. 4.cpp” Complete the program so that it will handle the numbers 1 thru 4 the same way that it handles 5 thru 10.  Delete the line cout<<"finish the code for num = 1 to 4\n"; and insert the appropriate code in its place.

7 Quick Note: The else if statement Rather than using a series of nested if-else clauses you can use an else if statement. if (x == 0) … else if (x == 1) … else if (x == 2) … else … //use as a catch-all Example: printing out the month corresponding to a number 1-12.  User enters a 1, January is printed out.

8 Example of else if int month; cin>>month; if(month==1) cout<<"Jan."<<endl; else if (month == 2) cout<<"Feb."<<endl;. else if (month == 12) cout<<"Dec."<<endl; else cout<<"What month?"<<endl;

9 Switch decision Case 1 Case 2 Case 3 Case 4 Case 5 Case 6 Multiple alternatives

10 Switch statement Format: switch (variable) { case 1: statement(s) break; case 2: statement(s) break; … default: statement(s) break; // default case is optional, // but it’s smart! } Variable must be int or char break: jumps to end of the switch statement Example: int month; cin >> month; switch (month) { case 1: cout << "Jan" << endl; break; case 2: cout << "Feb” << endl; break; case 3: cout << “Mar" << endl; break; … }

11 Selection - FYI These are equivalent: If (x==5) cout<<"yes\n"; else cout<<"no\n"; switch (x){ case 5: cout<<"yes\n"; break; default: cout<<"no\n"; }

12 Break What happens if you don’t have the break statement? int month; cin >> month; switch (month) { case 1: cout << "Jan" << endl; break; case 2: cout << "Feb” << endl; case 3: cout << “Mar" << endl; break; … }

13 Class Exercise Write a C++ program that reads in a single integer telling your final position in a contest. Use a switch statement to generate the following messages:  if you finished first: “Gold medal”  if you finished second: “Silver medal”  if you finished third: “Bronze medal”  if you finished below third (the default case): “Nice try”

14 Increment( ++ ) and Decrement( -- ) Four ways to add one to a variable: a = a + 1; a++; ++a; a += 1; Other useful operators: += *= -= /= %= Important: a++ and ++a differ: int y, z=5; y = z++; // inc after use cout << y << endl: cout << z << endl; // prints 5 and 6 y = ++z; // inc before use cout << y << endl; cout << z << endl; // prints 7 and 7

15 Using a debugger Things go wrong  Not everyone will get their program to run perfectly the first time.  The compiler will catch “syntax” errors, but it will not catch “logic” errors. Syntax error  double int time; Logic error  time = min * 37 + sec; Need a process to resolve logic errors  Study the code  Extra print statements  Debugger  Change random lines

16 Using the debugger Three handy tools  Breakpoints Stop at a certain point Look around Do things make sense?  Step through code Walk through it and watch what happens Note: “Step over” I/O calls!  Watch on variable Track changes to a variable Breakpoint:  Right click on the line: time = min * 60 + sec;  Select “Breakpoint” -> “Insert Breakpoint” Red dot appears by line  Select “Debug/Start Debugging” or hit the F5 key Program should run and prompt you for two inputs Halts (yellow arrow by line where breakpoint was inserted) Examine the bottom left window (make sure “locals” is selected) F11 (or “Step Into”) to execute the line of code  Re-examine values in “locals” for updates to variables F5 (or “Continue”) continues execution

17 Use the debugger to answer: Questions:  What is the value of an integer variable right after it is declared?  What is the value of a real (double) variable right after it is declared?  What is the result of the following code? int a, b, c; b = 5; c = a + b; cout << c << endl; More questions:  What if you assign a real value to an integer? int a; a = 3.25; cout << a << endl;  What if you assign an integer variable to a real value? double a; a = 312; cout << a << endl;

18 End of Class 05  Read pages 74-83, 110-116, 118-138


Download ppt "CS114-009 Class 05 Topics  Selection: switch statement Announcements  Read pages 74-83, 110-116, 118-138."

Similar presentations


Ads by Google