SlideShare a Scribd company logo
1 of 28
Selective
Execution



J. Hubbard
   2012
How program execute
                        Welcome the user
• Find the main
• Start at the top
                        Get info from the
  main () {             User (user’s age)

• Execute each line
  of code from top to       Print out
  bottom until the        The users age

  program ends }

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

Get info from the
       user




   Is the user       yes
    Over 35?
                               Print out
                             “You’re OLD”



    Thank the user
If statements – One Alternative Only
                             This means that either the first
int age;                     statement after the if is executed when
cout << “Welcomen”;         running your program OR it is
                             ignored completely.
cout << “Enter you age: “;
cin >> age;                  If the comparison is true - the if
                             statement is executes
                             If the comparison is false - the if
                             statement is ignored.
if (age > 35)
    cout << endl<<“You’re old n”;

cout <<“Thanks for running
  me”;
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           Flow Chart

                 Get info from the
                        user




                    Is the user       yes
                     Over 35?
                                              Print out
   Print out                                “You’re OLD”
“You’re YOUNG”



                     Thank the user
If- else statements – 2 alternatives
int age;                          Either the first statement is
cout << “Welcomen”;              executed OR the second
cout << “Enter you age: “;        statement is executed.
cin >> age;
                                  BOTH sets of statements are
                                  NEVER used.
if (age > 35)
    cout << endl<<“You’re old n”;
else                                    ONE OR THE
    cout << endl<<“You’re               OTHER!
    young n”;                     If the comparison is true - the
                                   first set is used;
cout <<“Thanks for running me”; If the comparison is false - the
                                   second set is used;
• OR…..
If- else if statements- Multiples
      Alternatives. What’s the difference?
int age;
cout << “Welcomen”;              If the if comparison is true – execute
cout << “Enter you age: “;        its block of code and jump out.
cin >> age;
                                  If it’s false, check the next else-if. If the
                                  comparison is true - execute its block
if (age > 35)                     of code and jump out.
    cout << endl<<“You’re old n”;
else if (age < 35)
    cout << endl<<“You’re young n”;

cout <<“Thanks for running me”;
• 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               Flow Chart
                 Get info from the
                        user



         <35                              >35
                     How old is
                      the user


                          =35

   Print out         Print out              Print out
“You’re YOUNG”   “The jury’s still out”   “You’re OLD”




                   Thank the user
If – else if statements
int age;
cout << “Welcomen”;
cout << “Enter you age: “;                      If the if comparison is true – execute
cin >> age;                                     its block of code and jump out.

if (age > 35)                                   If it’s false, check the next else-if. If
    cout << endl<<“You’re old n”;              the comparison is true - execute its
else if (age < 35)                              block of code and jump out.
    cout << endl<<“You’re young n”;
else if (age == 35)                             If it’s false, check the next else-if. . If
    cout << endl<<“The jury’s still out! n”;   the comparison is true - execute its
                                                block of code and jump out.

cout <<“Thanks for running me”;
• OR…..
If – else if - else statements
int age;
cout << “Welcomen”;
cout << “Enter you age: “;                      If the if comparison is true – execute
cin >> age;                                     its block of code and jump out.

if (age > 35)                                   If it’s false, check the next else-if. If
    cout << endl<<“You’re old n”;              the comparison is true - execute its
else if (age < 35)                              block of code and jump out.
    cout << endl<<“You’re young n”;
else                                            If it’s false, do the else
    cout << endl<<“The jury’s still out! n”;
                                                IF NO CONDITION IS TRUE, THE
                                                ELSE IS EXECUTED. IT’S A
cout <<“Thanks for running me”;                 CATCH ALL
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


                         CS161 Week #3                     16
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
Guided Lab 4
Remember 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             18
Guided Lab 4 - Algorithm
Start with you algorithm!

Step 1: Welcome the user
Step 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 Calculations
        If 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                            19
Guided Lab 4
#include <iostream>
using namespace std;
//header
int 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              20
Example of if Statements
// look at what’s inside the selection variable
// if if’s m, run code to convert inches to mm
if (‘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                   21
Example of if Statements
else //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             22
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 Week #3              23
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 4
              If 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.

More Related Content

Featured

Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Featured (20)

Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 

Hubbard cs161 lesson 4 a selective execution intro

  • 2. How program execute Welcome the user • Find the main • Start at the top Get info from the main () { User (user’s age) • Execute each line of code from top to Print out bottom until the The users age program ends } Thank the user
  • 3. 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!*!*!*!
  • 4. Welcome the user Flow Chart Get info from the user Is the user yes Over 35? Print out “You’re OLD” Thank the user
  • 5. If statements – One Alternative Only This means that either the first int age; statement after the if is executed when cout << “Welcomen”; running your program OR it is ignored completely. cout << “Enter you age: “; cin >> age; If the comparison is true - the if statement is executes If the comparison is false - the if statement is ignored. if (age > 35) cout << endl<<“You’re old n”; cout <<“Thanks for running me”;
  • 6. 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!
  • 7. Welcome the user Flow Chart Get info from the user Is the user yes Over 35? Print out Print out “You’re OLD” “You’re YOUNG” Thank the user
  • 8. If- else statements – 2 alternatives int age; Either the first statement is cout << “Welcomen”; executed OR the second cout << “Enter you age: “; statement is executed. cin >> age; BOTH sets of statements are NEVER used. if (age > 35) cout << endl<<“You’re old n”; else ONE OR THE cout << endl<<“You’re OTHER! young n”; If the comparison is true - the first set is used; cout <<“Thanks for running me”; If the comparison is false - the second set is used;
  • 10. If- else if statements- Multiples Alternatives. What’s the difference? int age; cout << “Welcomen”; If the if comparison is true – execute cout << “Enter you age: “; its block of code and jump out. cin >> age; If it’s false, check the next else-if. If the comparison is true - execute its block if (age > 35) of code and jump out. cout << endl<<“You’re old n”; else if (age < 35) cout << endl<<“You’re young n”; cout <<“Thanks for running me”;
  • 11. • 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!
  • 12. Welcome the user Flow Chart Get info from the user <35 >35 How old is the user =35 Print out Print out Print out “You’re YOUNG” “The jury’s still out” “You’re OLD” Thank the user
  • 13. If – else if statements int age; cout << “Welcomen”; cout << “Enter you age: “; If the if comparison is true – execute cin >> age; its block of code and jump out. if (age > 35) If it’s false, check the next else-if. If cout << endl<<“You’re old n”; the comparison is true - execute its else if (age < 35) block of code and jump out. cout << endl<<“You’re young n”; else if (age == 35) If it’s false, check the next else-if. . If cout << endl<<“The jury’s still out! n”; the comparison is true - execute its block of code and jump out. cout <<“Thanks for running me”;
  • 15. If – else if - else statements int age; cout << “Welcomen”; cout << “Enter you age: “; If the if comparison is true – execute cin >> age; its block of code and jump out. if (age > 35) If it’s false, check the next else-if. If cout << endl<<“You’re old n”; the comparison is true - execute its else if (age < 35) block of code and jump out. cout << endl<<“You’re young n”; else If it’s false, do the else cout << endl<<“The jury’s still out! n”; IF NO CONDITION IS TRUE, THE ELSE IS EXECUTED. IT’S A cout <<“Thanks for running me”; CATCH ALL
  • 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 CS161 Week #3 16
  • 17. 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
  • 18. Guided Lab 4 Remember 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 18
  • 19. Guided Lab 4 - Algorithm Start with you algorithm! Step 1: Welcome the user Step 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 Calculations If 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 19
  • 20. Guided Lab 4 #include <iostream> using namespace std; //header int 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 20
  • 21. Example of if Statements // look at what’s inside the selection variable // if if’s m, run code to convert inches to mm if (‘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 21
  • 22. Example of if Statements else //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 22
  • 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; CS161 Week #3 23
  • 24. Now, Let’s see what you can do on your own…….. Remember to start with a ????
  • 25. 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.
  • 26. 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
  • 27. Challenge 4 If you made it this far, you can finish the job, Ropes? Who needs ropes!
  • 28. 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.