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

Similar documents
Today in CS161. Week #3. Learn about. Writing our First Program. See example demo programs. Data types (char, int, float) Input and Output (cin, cout)

Lecture #9 **go to Blackboard ** Review from Shackelford Reading If statements Loops. Using if statements and loops

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Fundamentals of Programming CS-110. Lecture 2

Today in CS161. Lecture #12 Arrays. Learning about arrays. Examples. Graphical. Being able to store more than one item using a variable

LAB 4.1 Relational Operators and the if Statement

Getting started with C++ (Part 2)

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

Introduction to Programming EC-105. Lecture 2

Sample Code: OUTPUT Daily Highs & Lows

Ch 6. Functions. Example: function calls function

C++ For Science and Engineering Lecture 12

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

2 nd Week Lecture Notes

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

CS 117 Programming II, Spring 2018 Dr. Ghriga. Midterm Exam Estimated Time: 2 hours. March 21, DUE DATE: March 28, 2018 at 12:00 PM

Problem Solving: Storyboards for User Interaction

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

VARIABLES & ASSIGNMENTS

WARM UP LESSONS BARE BASICS

CSCE Practice Midterm. Data Types

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

Design and Debug: Essen.al Concepts CS 16: Solving Problems with Computers I Lecture #8

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

Programming. C++ Basics

Week 3: File I/O and Formatting 3.7 Formatting Output

Reviewing all Topics this term

Laboratory 7. Programming Workshop 2 (CSCI 1061U) Faisal Qureshi.

Computer Programming

CHAPTER 3 BASIC INSTRUCTION OF C++

CS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12

while for do while ! set a counter variable to 0 ! increment it inside the loop (each iteration)

Introduction to Programming using C++

1. In C++, reserved words are the same as predefined identifiers. a. True

1) What of the following sets of values for A, B, C, and D would cause the string "one" to be printed?

Programming Language. Functions. Eng. Anis Nazer First Semester

Computer Science II Lecture 1 Introduction and Background

CS2141 Software Development using C/C++ C++ Basics

CSCI 111 First Midterm Exam Spring Solutions 09.05am 09.55am, Wednesday, March 14, 2018

CS 101 Computer Programming and utilization. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay

Study Guide for Test 2

The American University in Cairo Computer Science & Engineering Department CSCE Dr. KHALIL Exam II Spring 2010

CSc Introduc/on to Compu/ng. Lecture 17 Edgardo Molina Fall 2011 City College of New York

Exception with arguments

CS101 PLEDGED SPRING 2001

int x = 5; double y = 3; // Integer division rounds the result down to the nearest whole number. cout << "1a: " << x / 3 << endl; //1

Multiple Choice Questions (20 questions * 5 points per question = 100 points)

REPETITION CONTROL STRUCTURE LOGO

do { statements } while (condition);

Increment and the While. Class 15

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

Review. Relational Operators. The if Statement. CS 151 Review #4

CSCI 102 Fall 2010 Exam #1

BITG 1233: Introduction to C++

CS Semester I. Quiz 1 (version A)

Chapter 3 Objects, Types, and Values

C++ Programming: From Problem Analysis to Program Design, Third Edition

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

Introduction to C++ (Extensions to C)

Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

CSCE Practice Midterm. Data Types

download instant at Introduction to C++

Introduction to Programming

conditional statements

Unit 7. 'while' Loops

LECTURE 02 INTRODUCTION TO C++

University of Michigan EECS 183: Elem. Programming Concepts Fall 2011 Exam 1: Part 1: Form 1. Professors: ML Dorf, Elliot Soloway

CS 31 Discussion 1A, Week 1. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50

CS150 Intro to CS I. Fall Fall 2017 CS150 - Intro to CS I 1

CS31 Discussion 1E Spring 17 : week 05

CPE 112 Spring 2015 Exam III (100 pts) April 8, True or False (12 Points)

Maciej Sobieraj. Lecture 1

Computer Programming : C++

The C++ Language. Arizona State University 1

Ch 8. Searching and Sorting Arrays Part 1. Definitions of Search and Sort

G52CPP C++ Programming Lecture 17

The American University in Cairo Department of Computer Science & Engineeringt CSCI &09 Dr. KHALIL Exam-I Fall 2009

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

CS2141 Software Development using C/C++ Stream I/O

More on Func*ons Command Line Arguments CS 16: Solving Problems with Computers I Lecture #8

CSc Introduc/on to Compu/ng. Lecture 8 Edgardo Molina Fall 2011 City College of New York

Why Is Repetition Needed?

int n = 10; int sum = 10; while (n > 1) { sum = sum + n; n--; } cout << "The sum of the integers 1 to 10 is " << sum << endl;

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7. CS 5301 Spring 2018

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Chapter Four: Loops II

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Lab 2.1: Fixing a C++ program

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

Integer Data Types. Data Type. Data Types. int, short int, long int

Name Section: M/W T/TH Number Definition Matching (8 Points)

CS 103 Lab 6 - Party Like A Char Star

Lab: Supplying Inputs to Programs

Transcription:

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

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 2

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, or mm to inches! We will give the user a choice... type m to convert to mm type i to convert to inches CS161 Lecture #7 3

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 4

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

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 6

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 7

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 << Converting inches -> mm ; else if (selection == i ) cout << Converting mm -> inches ; CS161 Lecture #7 8

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 9

Example of if Statements #include <iostream> 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 10

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 11

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 12

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 13

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 14

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 (nongraphical 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 15

Integrating this into a program CS161 Lecture #7 16

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 17

Integrating this into a program CS161 Lecture #7 18

Integrating this into a program CS161 Lecture #7 19

Integrating this into a program CS161 Lecture #7 20

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 21

/check to see if the user entered a reasonable number Integrating if (0 > num_classes) this //a negative into value a was program 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 22

Running this new version CS161 Lecture #7 23

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 24

Running this new version CS161 Lecture #7 25

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 26

Running this new version CS161 Lecture #7 27

Using Graphics Let s extend the graphics program from last time, asking the user if they want a Rectangle (R) or a Circle (C) //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); CS161 Lecture #7 28

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 29

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 30

Running this new version CS161 Lecture #7 31

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 32

Running this new version CS161 Lecture #7 33