28
Selectiv e Executio n J. Hubbard 2012

Hubbard cs161 lesson 4 a selective execution intro

Embed Size (px)

Citation preview

Page 1: Hubbard cs161 lesson 4 a selective execution intro

Selective Execution

J. Hubbard

2012

Page 2: Hubbard cs161 lesson 4 a selective execution intro

How program execute

• Find the main

• Start at the top main () {

• Execute each line of code from top to bottom until the program ends }

Welcome the user

Get info from the User (user’s age)

Print out The users age

Thank the user

Page 3: Hubbard cs161 lesson 4 a selective execution intro

Let the program decide which instructions are executed

• What is we want to look at the user’s age and make a decision in the program based on it?– For example: If they’re over 35, maybe we

want to tell the user they’re old!– How rude!*!*!*!

Page 4: Hubbard cs161 lesson 4 a selective execution intro

Welcome the user

Get info from the user

Is the userOver 35?

Print out “You’re OLD”

Thank the user

yes

Flow Chart

Page 5: Hubbard cs161 lesson 4 a selective execution intro

If statements – One Alternative Only

int age;cout << “Welcome\n”;cout << “Enter you age: “;cin >> age;

if (age > 35)cout << endl<<“You’re old \n”;

cout <<“Thanks for running me”;

This means that either the first statement after the if is executed when running your program OR it is ignored completely.

If the comparison is true - the if statement is executesIf the comparison is false - the if statement is ignored.

Page 6: Hubbard cs161 lesson 4 a selective execution intro

Let the program decide which instructions are executed

• Let’s do something a little different– If they’re over 35, let’s tell the user they’re old!– If they’re under 35, let’s tell the user they’re

young!

Page 7: Hubbard cs161 lesson 4 a selective execution intro

Welcome the user

Get info from the user

Is the userOver 35?

Print out “You’re OLD”

Thank the user

yes

Print out “You’re YOUNG”

Flow Chart

Page 8: Hubbard cs161 lesson 4 a selective execution intro

If- else statements – 2 alternatives

int age;cout << “Welcome\n”;cout << “Enter you age: “;cin >> age;

if (age > 35)cout << endl<<“You’re old \n”;

elsecout << endl<<“You’re young \n”;

cout <<“Thanks for running me”;

Either the first statement is executed 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;

Page 9: Hubbard cs161 lesson 4 a selective execution intro

• OR…..

Page 10: Hubbard cs161 lesson 4 a selective execution intro

If- else if statements- Multiples Alternatives. What’s the difference?

int age;cout << “Welcome\n”;cout << “Enter you age: “;cin >> age;

if (age > 35)cout << endl<<“You’re old \n”;

else if (age < 35)cout << endl<<“You’re young \n”;

cout <<“Thanks for running me”;

If the if comparison is true – execute its block of code and jump out.

If it’s false, check the next else-if. If the comparison is true - execute its block of code and jump out.

Page 11: Hubbard cs161 lesson 4 a selective execution intro

• Wait….

• What if the user IS 35?

• Let’s tell them that we’re not sure yet if they’re young or old. But in a year, we’ll let them know!

Page 12: Hubbard cs161 lesson 4 a selective execution intro

Welcome the user

Get info from the user

How old is the user

Print out “You’re OLD”

Thank the user

>35

Print out “You’re YOUNG”

<35

Print out “The jury’s still out”

=35

Flow Chart

Page 13: Hubbard cs161 lesson 4 a selective execution intro

If – else if statementsint age;cout << “Welcome\n”;cout << “Enter you age: “;cin >> age;

if (age > 35)cout << endl<<“You’re old \n”;

else if (age < 35) cout << endl<<“You’re young \n”;

else if (age == 35)cout << endl<<“The jury’s still out! \n”;

cout <<“Thanks for running me”;

If the if comparison is true – execute its block of code and jump out.

If it’s false, check the next else-if. If the comparison is true - execute its block of code and jump out.

If it’s false, check the next else-if. . If the comparison is true - execute its block of code and jump out.

Page 14: Hubbard cs161 lesson 4 a selective execution intro

• OR…..

Page 15: Hubbard cs161 lesson 4 a selective execution intro

If – else if - else statementsint age;cout << “Welcome\n”;cout << “Enter you age: “;cin >> age;

if (age > 35)cout << endl<<“You’re old \n”;

else if (age < 35) cout << endl<<“You’re young \n”;

elsecout << endl<<“The jury’s still out! \n”;

cout <<“Thanks for running me”;

If the if comparison is true – execute its block of code and jump out.

If it’s false, check the next else-if. If the comparison is true - execute its block of code and jump out.

If it’s false, do the else

IF NO CONDITION IS TRUE, THE ELSE IS EXECUTED. IT’S A CATCH ALL

Page 16: Hubbard cs161 lesson 4 a selective execution intro

CS161 Week #3 16

Compound if statements...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

Page 17: Hubbard cs161 lesson 4 a selective execution intro

Relational operators

> greater than 5 > 4 is TRUE

< less than 4 < 5 is TRUE

>= greater than or equal 4 >= 4 is TRUE

<= less than or equal 3 <= 4 is TRUE

== equal to 5 == 5 is TRUE

!= not equal to 5 != 4 is TRUE

Page 18: Hubbard cs161 lesson 4 a selective execution intro

CS161 Week #3 18

Guided Lab 4Remember our guided lab that converted

inches to millimeters? Let’s make it better!

Ask The user if they want to convert inches to millimeters or millimeters to inches. Based on their answer, we’ll do the corresponding conversion for them.

Page 19: Hubbard cs161 lesson 4 a selective execution intro

CS161 Week #3 19

Guided Lab 4 - AlgorithmStart with you algorithm!

Step 1: Welcome the userStep 2: Get info from user

Do they want to convert from inches to mm or mm to inches?How many inches or millimeters do they want to convert

Step 3: Do CalculationsIf they want to convert to mm, then mm = inches*25.4;If the want to convert to inches, then (look it up)

Step 4: Output the result___ inches converts to ___ mm OR___ mm converts to ___ inches

Step 5: Thank the user for running your program,

Page 20: Hubbard cs161 lesson 4 a selective execution intro

CS161 Week #3 20

Guided Lab 4#include <iostream>using namespace std;//headerint main() { //declare variables char selection; //the user’s answer…one character float inches, mm;

//Step 1: 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();

Page 21: Hubbard cs161 lesson 4 a selective execution intro

CS161 Week #3 21

Example of if Statements

// look at what’s inside the selection variable// if if’s m, run code to convert inches to mmif (‘m’ == selection) //notice expression!{

//Get inches from the user cout << “Enter the # inches: “; cin >> inches; cin.get();

//Do the conversion mm = 25.4 * inches; //this is multiplication!

//Print out the results cout << inches << “in converts to ” << mm << “ millimeters” << endl;}

Page 22: Hubbard cs161 lesson 4 a selective execution intro

CS161 Week #3 22

Example of if Statementselse //selection is not an ‘m’. Convert to inches{

//Get mm from the user cout << “Enter the # millimeters: “; cin >> mm; cin.get();

//Do the conversion inches = mm / 25.4;

//Print out the results cout << mm << “mm converts to ” << inches << “ inches” << endl;}

Page 23: Hubbard cs161 lesson 4 a selective execution intro

CS161 Week #3 23

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;

Page 24: Hubbard cs161 lesson 4 a selective execution intro

Now,Let’s see what you can do on your own……..

Remember to start with a ????

Page 25: Hubbard cs161 lesson 4 a selective execution intro

Assignment 4• You’ve been hired by a cell phone company to write a program

that calculates customers’ monthly bill. Here is the information you are given.

• The plan costs $60 each month for 200 free minutes, 250 texts and 2GB of data. Additional minutes costs 40 cents per minute. Additional texts cost 20 cents per text. Additional data costs $10 for each GB over (rounded up).

• Write a program that will ask the customer how many minutes they have used, how many texts they have sent/received, and how much data they’ve used that month and print out their monthly bill.

Page 26: Hubbard cs161 lesson 4 a selective execution intro

Assignment 4

• For example, if a customer – uses their cell phone for 250 minutes, – sent/received 300 texts messages,– used 4GB of data – they will be charged $60(plan cost) + 50 (additional minutes)*.40 (cents per

minute) + 50 (additional text)*.20 (cents per text) + 2 (additional GB)*10 (dollars per GB) = $110.00 that month.

 • Your program output should look like:

– Plan Fee: 60.00– Additional Minutes Fee: $20.00– Additional Texting Fee: $10.00– Additional Data Fee: $20.00– Total Monthly Cost: $110.00

Page 27: Hubbard cs161 lesson 4 a selective execution intro

Challenge 4If you made it this far, you can finish the job, Ropes? Who needs ropes!

Page 28: Hubbard cs161 lesson 4 a selective execution intro

Challenge 4• Adding on to assignment 4: your customers can choose their plan type too.

They can sign up for either the standard plan or the premium plan.

• Like assignment 4, the standard plan costs $60 each month for 200 free minutes, 250 texts and 2GB of data. Additional minutes costs 40 cents per minute. Additional texts cost 20 cents per text. Additional data costs $10 for each GB over (rounded up).

• The premium plan costs $100 each month for 300 free minutes, 300 texts and 3GB of data. Additional minutes costs 30 cents per minute. Additional texts cost 15 cents per text. Additional data costs $10 for each GB over (rounded up).  

 

• Write a program that will ask the customer their plan type in addition to all the other input from assignment 4.

• Your program will output the plan type chosen, the additional fees (for the chosen plan type) and the customers monthly bill.