Hubbard cs161 lesson 4 a selective execution intro
-
Upload
jillhubbard -
Category
Documents
-
view
171 -
download
2
Transcript of Hubbard cs161 lesson 4 a selective execution intro
Selective Execution
J. Hubbard
2012
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
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!*!*!*!
Welcome the user
Get info from the user
Is the userOver 35?
Print out “You’re OLD”
Thank the user
yes
Flow Chart
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.
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!
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
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;
• OR…..
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.
• 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!
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
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.
• OR…..
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
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
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
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.
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,
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();
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;}
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;}
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;
Now,Let’s see what you can do on your own……..
Remember to start with a ????
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.
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
Challenge 4If you made it this far, you can finish the job, Ropes? Who needs ropes!
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.