200 likes | 356 Views
Lab 1 – Learning the Debugger. EECE 351 Spring 2000 Lecture # 3. Lecture & Lab. Control Structures if , else if , else , switch for & while The Debugger Breakpoints Walking through the code Edit and Continue. Introduction. Before we begin, you need to know the following:
E N D
Lab 1 – Learning the Debugger EECE 351 Spring 2000 Lecture # 3 EECE 351 – Lecture 3
Lecture & Lab • Control Structures • if, else if, else, switch • for & while • The Debugger • Breakpoints • Walking through the code • Edit and Continue EECE 351 – Lecture 3
Introduction • Before we begin, you need to know the following: • false is 0. • true is any other number (positive or negative, as long as it isn’t 0). • Usually code happens one line at a time. • Control structures let you change the flow of the code. • Inside conditionals, use “==” not “=” • 90% of the time, do not put a semicolon after! EECE 351 – Lecture 3
Overview Normal Code for / while if and relatives EECE 351 – Lecture 3
Selective • The if keyword allows your program to decide which option to perform. if (/*test*/) { //True, will only do this if test != 0 } EECE 351 – Lecture 3
Selective (cont.) • You can combine if with else to establish a two-option test. if (/*test*/) { //True, will only do this if test != 0 } else //will only do this code if test == 0 EECE 351 – Lecture 3
Selective (cont.) • else if will help make more elaborate tests. if (/*test*/) { //True, will only do this if test != 0 } else if (/*test2*/) { //Will only do this if test == 0 and test2 != 0 } else //Will only do this code if both tests == 0 EECE 351 – Lecture 3
Selective (cont.) • switch is another selective structure. • It is combined with the case keyword to determine which action to perform. • default is used to specify an action to do when none of the other cases are matched. In an SE world, defaults are MANDATORY! • Use break to keep other sections from running. In an SE world, this is the only time you will use breaks! EECE 351 – Lecture 3
Selective (cont.) • Example: switch(/*SomeNumber*/) { case0: //Only do this if SomeNumber == 0 break; case1: //Only do this if SomeNumber == 1 break; default: //Do this all other times. } EECE 351 – Lecture 3
Repetitive • There are two options for the repetitive control structures. • They both perform the same way, but here are some guidelines: • for – only use when you know the amount of repetitions. • while – use when you need something to happen to stop you. (user hits certain key, end of file reached, etc.) EECE 351 – Lecture 3
for • The for loop has three parts: for(int ct = 0; ct < 10; ct++) { //What you want to repeat goes here } Incremention (each) Initialization Segment (once) Test Segment (each) EECE 351 – Lecture 3
For (cont.) • These parts’ positions are not mandatory : int ct = 0; //Initialization for(; ct < 10; ct++) int ct = 0; //Initialization for(; ct < 10;) { ct++; //Incremention } for(;;) //Infinite Loop BAD! EECE 351 – Lecture 3
While • The other repetition structure comes in two variations, while and do/while. • do/while is commonly used when you want to perform your actions once before testing. do { //Your actions go here } while (/*something is true*/); EECE 351 – Lecture 3
While (cont.) • I, however, have never needed a do/while. Neither will you! • You can get by with just a while. • The structure for a while is like this: while(/*This is true*/) { //What you want to do goes here! } EECE 351 – Lecture 3
What we know • Control Structures • Selective • if,else if, else • Repetitive • while, for EECE 351 – Lecture 3
Where we are going • Lecture – Functions • Prototypes • Definitions • Calls EECE 351 – Lecture 3
Lab 1 – The Debugger • I have a project located at “\\engr_asu\ece351\section 1\Labs\Lab1” • Download the project to a local directory. “C:\Temp\YourName\Lab1” • Open the project “*.dsw” • Examine the code. • Place a breakpoint on the line of code with the first “cout” statement (F9). EECE 351 – Lecture 3
Lab 1 – The Debugger (cont.) • Do you have the red dot shown above? • Hit the Go Button (F5)! • What happens when the app starts? • Use F-10 to step line-by-line. • Watch the middle left window change at each line you step on. • When you get to cin >> nInput; switch back to the app and type a number. EECE 351 – Lecture 3
Lab 1 – The Debugger (cont.) • Did the number you typed appear in the variable window? • Keep walking through the code using F-10. • Place the cursor on line: fCPU = (float) clock() - fStart; • Use the Run-To-Cursor command to take you to that point. Don’t forget –1! • Press F-11. What happened? EECE 351 – Lecture 3
Homework # 1 • Use the control structures in class to create a simple calculator. • Prompt user for 2 numbers. • Tell user the following: • Sum • Difference • Power XY: X = 1st #, Y = 2nd # • Divisor and remainder • Repeat until user enters 5,682 for both numbers. EECE 351 – Lecture 3