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.

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

Week 4 Selections This week shows how to use selection statements for more flexible programs. It also describes the various integral types that are available.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
Computer Science 1620 Programming & Problem Solving.
CS 1400 Jan 2007 Chapter 4, sections Relational operators Less than< Greater than> Less than or equal= Equal== Not equal!=
The If/Else Statement, Boolean Flags, and Menus Page 180
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
Today in CS161 Lecture #4 Solving Problems with Computers Walk through the Tic Tac Toe Algorithm Getting ready for Creating Programs Turn the Inches to.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
CS161 Topic #21 CS161 Introduction to Computer Science Topic #2.
Today in CS161 Lecture #9**go to Blackboard ** Practicing… Review from Shackelford Reading If statements Loops Begin writing Programs Using if statements.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Chapter 4 Selection Structures: Making Decisions.
1 CS 1430: Programming in C++. 2 Literal Values Literal values of int Literal values of float
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
CONTROLLING PROGRAM FLOW
Fundamental Programming: Fundamental Programming Introduction to C++
1 CS161 Introduction to Computer Science Topic #3.
# ACS 168 Structured Programming Using the Computer Chapter 2 Spring 2002 Prepared by Shirley White.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Computer Science 1620 boolean. Types so far: Integer char, short, int, long Floating Point float, double, long double String sequence of chars.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Today in CS161 Lecture #8 Learn about… Logicals: And, Or, Not Repetition: loops! Rewrite our First Program Using loops Graphics Let’s make the graphics.
Lecture 7: Making Decisions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.
Input Validation 10/09/13. Input Validation with if Statements You, the C++ programmer, doing Quality Assurance (by hand!) C++ for Everyone by Cay Horstmann.
1 CS161 Introduction to Computer Science Topic #8.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
Today in CS161 Lecture #5 Learn about… Data types (char, int, float) Input and Output (cin, cout) Writing our First Program Write the Inches to MM Program.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
A First Book of C++ Chapter 4 Selection.
The Ohio State University
Basic concepts of C++ Presented by Prof. Satyajit De
CNG 140 C Programming (Lecture set 3)
Chapter 3 Control Statements
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
CS161 Introduction to Computer Science
Chapter 2 Assignment and Interactive Input
EGR 2261 Unit 4 Control Structures I: Selection
The Selection Structure
Engineering Problem Solving with C++, Etter/Ingber
Control Structures – Selection
Compound Assignment Operators in C++
Today in CS161 Week #3 Learn about… Writing our First Program
Today in CS161 Week #4 Using if statements Learn about Repetition
Today in CS161 Week #5 **weekly visit D2L** Practicing…
Today in CS161 Week #4 Learn about… Rewrite our First Program
Selection Statements.
Summary Two basic concepts: variables and assignments Basic types:
Let’s Write a Graphics Program
Chapter 4: Control Structures I (Selection)
Chapter 3: Selection Structures: Making Decisions
Life is Full of Alternatives
Today in CS161 Week #5 **go to Blackboard ** Practicing…
CS150 Introduction to Computer Science 1
Life is Full of Alternatives
CHAPTER 5: Control Flow Tools (if statement)
Chapter 3: Selection Structures: Making Decisions
Life is Full of Alternatives
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
Programming Fundamental
Presentation transcript:

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 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

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

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;

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”;

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;

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).

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”;

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

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();

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; }

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

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;

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

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

CS161 Lecture #7 16 Integrating this into a program…

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";

CS161 Lecture #7 18 Integrating this into a program…

CS161 Lecture #7 19 Integrating this into a program…

CS161 Lecture #7 20 Integrating this into a program…

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

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";

CS161 Lecture #7 23 Running this new version…

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; }

CS161 Lecture #7 25 Running this new version…

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; }

CS161 Lecture #7 27 Running this new version…

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)

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……

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!"); }

CS161 Lecture #7 31 Running this new version…

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

CS161 Lecture #7 33 Running this new version…