Presentation is loading. Please wait.

Presentation is loading. Please wait.

Today in CS161 Lecture #7 Learn about… If and else statements Rewrite our First Program Using if and else statements Create new Graphics Demos Using if.

Similar presentations


Presentation on theme: "Today in CS161 Lecture #7 Learn about… If and else statements Rewrite our First Program Using if and else statements Create new Graphics Demos Using if."— Presentation transcript:

1 Today in CS161 Lecture #7 Learn about… If and else statements Rewrite our First Program Using if and else statements Create new Graphics Demos Using if and else statements CS161 Lecture #71

2 2 Selective Execution Most programs are not as simple as converting inches to mm! We need to select from alternatives... think of the ATM example... this can be done using an if statement an if allows us to select between 2 choices for example, we can select one thing or another, depending on the user

3 CS161 Lecture #7 3 if Statements For example, we can change our inches to mm conversion program, allowing the user to select whether they want to convert from inches to mm, ormm to inches! We will give the user a choice... type ‘m’ to convert to mm type ‘i’ to convert to inches

4 CS161 Lecture #7 4 if Statements have the form... 1) One alternative: if (logical expression) single C++ statement; char selection; cout << “Enter a selection (m or i): “; cin >> selection; if (selection == ‘i’) cout << “You selected to convert to inches!” << endl;

5 CS161 Lecture #7 5 if Statements have the form... 2) Two alternatives: if (logical expression) single C++ statement; else single C++ statement; if (selection == ‘m’) cout mm”; else cout inches”;

6 CS161 Lecture #7 6 if Statements have the form... This means that either the first statement is executed when running your program OR the second statement is executed. BOTH sets of statements are NEVER used. ONE OR THE OTHER! If the comparison is true - the first set is used; If the comparison is false - the second set is used;

7 CS161 Lecture #7 7 if Statements have the form... When an if is encountered, the logical expression is TRUE if it is non zero. In this case, the statement following the expression is executed. Otherwise, if the logical expression evaluates to zero it means it is FALSE. In this case, if there is an else the statement following the else is executed. If there is no else then nothing is done if the logical expression evaluates to zero (FALSE).

8 CS161 Lecture #7 8 if Statements have the form... 3) Two or more alternatives: if (logical expression) single C++ statement; else if (logical expression) single C++ statement; if (selection == ‘m’) cout mm”; else if (selection == ‘i’) cout inches”;

9 CS161 Lecture #7 9 Compound if statements... 4) You might want more than a single statement to be executed given an alternative...so instead of a single statement, you can use a compound statement if (logical expression) { Many C++ statements; } else //optional

10 CS161 Lecture #7 10 Example of if Statements #include using namespace std; int main() { char selection; //the user’s answer…one character float inches, mm; //prompt for input from the user cout << “Enter i to convert to inches” << “ and m to convert to mm: “; cin >> selection; //get the response cin.get();

11 CS161 Lecture #7 11 Example of if Statements if (‘m’ == selection) //notice expression! { cout << “Enter the # inches: “; cin >> inches; cin.get(); mm = 25.4 * inches; //this is multiplication! cout << inches << “in converts to ” << mm << “ millimeters” << endl; }

12 CS161 Lecture #7 12 Example of if Statements else //selection is not an ‘m’ { cout << “Enter the # millimeters: “; cin >> mm; cin.get(); inches = mm / 25.4; cout << mm << “mm converts to ” << inches << “ inches” << endl; } cin.get(); //wait for user input

13 CS161 Lecture #7 13 Or, use the else if sequence… else if (‘i’ == selection) //selection is not an ‘m’ { cout << “Enter the # millimeters: “; cin >> mm; cin.get(); inches = mm / 25.4; //this is division cout << mm << “mm converts to ” << inches << “ inches” << endl; } else cout << “Neither i nor m were selected” << endl;

14 CS161 Lecture #7 14 logical expressions The comparison operators may be: Relational Operators: > for greater than < for less than >= for greater than or equal <= for less than or equal Equality Operators: == for equal to != for not equal to

15 CS161 Lecture #7 15 Using If statements in a program We will now do two examples, one extending the program we wrote last week finding out how many classes you are taking (non- graphical And…another that takes the graphics program and finds out if the user wants to draw circles or rectangles! It is all about choices! Plus, you will notice that now we can error check

16 CS161 Lecture #7 16 Integrating this into a program…

17 CS161 Lecture #7 17 Integrating this into a program… int num_classes = 0; //the number of classes you are taking //prompt and read in the number of classes cout << "How many classes are you taking this term? "; cin >> num_classes; cin.get(); //check to see if the user entered a reasonable number if (0 > num_classes) //a negative value was entered cout << "You can't take fewer than zero classes!!\n\n"; else if (0 == num_classes) //zero classes! cout << "I'm sorry to hear you are not taking classes.\n\n"; else if (5 < num_classes) //more than 5 classes cout << "Wow...you are really taking a lot of classes!\n\n";

18 CS161 Lecture #7 18 Integrating this into a program…

19 CS161 Lecture #7 19 Integrating this into a program…

20 CS161 Lecture #7 20 Integrating this into a program…

21 CS161 Lecture #7 21 Using If statements in a program Let’s extend this now to only echo the number of classes that you are taking, if it is a reasonable number Otherwise, after the error message we will tell the user to re-run the program once they have a better idea of how many classes they are taking

22 CS161 Lecture #7 22 Integrating this into a program… /check to see if the user entered a reasonable number if (0 > num_classes) //a negative value was entered cout << "You can't take fewer than zero classes!!\n\n"; else if (0 == num_classes) //zero classes! cout << "I'm sorry to hear you are not taking classes.\n\n"; else if (5 < num_classes) //more than 5 classes cout << "Wow...you are really taking a lot of classes!\n\n"; else { //echo what we got back to the user, if it is valid cout << endl <<endl; cout << "You are taking " << num_classes << " classes" <<endl; } //tell the user to re-use the program if it was an invalid value if (num_classes <= 0) cout << "Re-run the program once you start taking classes\n\n"; else if (num_classes > 5) cout << "Consider reevaluating the classes you are taking\n\n";

23 CS161 Lecture #7 23 Running this new version…

24 CS161 Lecture #7 24 Another approach...for next time //check to see if the user entered a reasonable number //If the number is less than or equal to zero OR greater than 5 if (0 >= num_classes || 5 < num_classes) //out of range! { cout << "The value you entered is out of range.\n\n"; cout << "Re-run the program once you figure it out!\n\n"; } else { //echo what we got back to the user cout << endl <<endl; cout << "You are taking " << num_classes << " classes" <<endl; }

25 CS161 Lecture #7 25 Running this new version…

26 CS161 Lecture #7 26 Another approach...for next time //check to see if the user entered a reasonable number if (0 >= num_classes || 5 < num_classes) //out of range! { if (0 > num_classes) cout << "You can't take fewer than zero classes!!\n\n"; else if (0 == num_classes) //zero classes! cout << "I'm sorry to hear you are not taking classes.\n\n"; else if (5 < num_classes) //more than 5 classes cout << "Wow...you are really taking a lot of classes!\n\n"; cout << "Re-run the program once you figure it out!\n\n"; } else { //echo what we got back to the user cout << endl <<endl; cout << "You are taking " << num_classes << " classes" <<endl; }

27 CS161 Lecture #7 27 Running this new version…

28 CS161 Lecture #7 28 Using Graphics… //Here is where I am going to put my variables int window_size; int color; int circle_radius; char selection; // what does the user want to do? int width, height; //rectangle width and height cout << "How big of a window do you want (pick a number less than 1200): "; cin >> window_size; cin.get(); initwindow(window_size, window_size); cout << "What color do you want...enter in a number 1-15 (15 is white) "; cin >> color; cin.get(); setcolor(color); setfillstyle(SOLID_FILL,color); Let’s extend the graphics program from last time, asking the user if they want a Rectangle (R) or a Circle (C)

29 CS161 Lecture #7 29 Using Graphics… //Find out if they want to draw a circle or a rectangle cout << "Do you want to draw a CIRCLE or a RECTANGLE? C or R: "; cin >> selection; cin.get(); if ('C' == selection) //Circle { cout << "How big do you want the circle? "; cin >> circle_radius; cin.get(); fillellipse(window_size/2,window_size/2,circle_radius,circle_radius); } else if ('R' == selection) //Rectangle { cout << "How wide do you want the rectangle? "; cin >> width; cin.get(); cout << "How high should the rectangle be? "; cin >> height; cin.get(); // next page……

30 CS161 Lecture #7 30 Using Graphics… //figure out how to draw the filled rectangle (a bar) int startx = (window_size-width)/2; //center the rectangle int starty = (window_size-height)/2; bar(startx,starty,startx+width,starty+height); //"bars" are filled } else { //The user did not enter a C or an R cout <<"Sorry you couldn't decide!" <<endl <<endl; settextstyle(0,0,6); //6 is BIG outtextxy(0,window_size/2,"TOO BAD!"); }

31 CS161 Lecture #7 31 Running this new version…

32 CS161 Lecture #7 32 Using Graphics…using the OR //Find out if they want to draw a circle or a rectangle cout << "Do you want to draw a CIRCLE or a RECTANGLE? C or R: "; cin >> selection; cin.get(); if ('C' == selection || ‘c’ == selection) //Circle (upper or lower case) { cout << "How big do you want the circle? "; cin >> circle_radius; cin.get(); fillellipse(window_size/2,window_size/2,circle_radius,circle_radius); } else if ('R' == selection|| ‘r’ == selection) //Rectangle { cout << "How wide do you want the rectangle? "; cin >> width; cin.get(); cout << "How high should the rectangle be? "; cin >> height; cin.get(); // ……etc. … no other changes

33 CS161 Lecture #7 33 Running this new version…


Download ppt "Today in CS161 Lecture #7 Learn about… If and else statements Rewrite our First Program Using if and else statements Create new Graphics Demos Using if."

Similar presentations


Ads by Google