Sie sind auf Seite 1von 24

Starting Out with Programming Logic and Design

Lab 4: Decisions and Boolean Logic


This lab accompanies Chapter 4 of Starting Out with Programming Logic & Design. Name !dam

Lab 4.1 Logical Operators and Dual Alternative Decisions


Critical "e#iew The logical !ND operator and the logical O" operator allow $ou to connect multiple %oolean e&pressions to create a compound e&pression. The logical NOT operator re#erses the truth of a %oolean e&pression. 'hen using the !ND operator( both conditions must be true in order for the statements within an if to process. 'hen using the O" operator( either condition must be true in order for the statements within an if to process. ! dual alternati#e decision structure will e&ecute one group of statements if its %oolean e&pression is true( or another group if its %oolean e&pression is false. The general structure of an if then else statement is
If condition Then Statement Statement Etc. Else Statement Statement Etc. End If

)elp *ideo Double clic+ the file to #iew #ideo

This lab re,uires $ou to thin+ about possible true and false conditions using if statements. Step 1: Consider the following #alues set to #ariables. m$!ge - ./ $our!ge - 10 m$Number - 01 $ourNumber - 11 #oting!ge - 10

Starting Out with Programming Logic and Design Step 2: %ased on the #alues to the #ariables in Step 1( what is the e&pected output2 )int The output will be either what is printed to the screen( or nothing. 3"eference Logical Operators( page 1414. The condition
If myAge == 31 AND yourAge < myAge Then Display !y age is 31 and your age is less than that" End If If myAge <= 3# AND myAge $= 3% Then Display !y age is &et'een 3% and 3#5 End If If yourAge == (otingAge )* yourAge $ (otingAge Then Display +ou can (ote" End If If myNum&er == ,3 )* yourNum&er == ,3 Then Display )ne of our num&ers is ,3" End If

!pected Output Nothing

6$ age is between ./ and .7 8ou can #ote

Nothing

Step ": %ased on the #alues to the #ariables in Step 1( what is the e&pected output2 3"eference Dual !lternati#e Decision Structures( page 1/74. The condition
If myAge == 31 AND yourAge < myAge Then Display !y age is 31 and your age is less than that" Else Display )ur ages do not -ualify" End If If myAge <= 3# AND myAge $= 3% Then Display !y age is &et'een 3% and 3#" Else Display !y age is not 'ithin that range" End If If yourAge == (otingAge )* yourAge $ (otingAge Then Display +ou can (ote" Else Display +ou cannot (ote" End If If myNum&er == ,3 )* yourNum&er == ,3 Then Display )ne of our num&ers is ,3" Else Display ,3 is not our num&ers" End If

!pected Output Our ages do not ,ualif$

6$ age is between ./ and .7

8ou can #ote

0. is not our numbers

Starting Out with Programming Logic and Design

Lab 4.2 #seudocode: Dual Alternative Decisions


Critical "e#iew ! dual alternati#e decision structure will e&ecute one group of statements if its %oolean e&pression is true( or another group if its %oolean e&pression is false. The general structure of an if then else statement is If condition Then Statement Statement Etc. Else Statement Statement Etc. End If 6odule "e#iew "ecall the difference between a reference #ariable and a #alue #ariable. "eference #ariables are used in the following lab when the #alue of the #ariable is modified in the module. 8ou9ll notice some parameter lists include the +e$word "ef before the #ariable that is going to change within the module. )elp *ideo Double clic+ the file to #iew #ideo

This lab re,uires $ou to thin+ about the steps that ta+e place in a program b$ writing pseudocode. "ecall the retail compan$ program from Lab ../. The compan$ now wants to modif$ their bonus portion to include different le#els and t$pes and eliminate the da$ off program. The new program is as follows
A retail company assigns a .#/// store &onus if monthly sales are more than .1//0///1 other'ise a .#// store &onus is a'arded. Additionally0 they are doing a'ay 'ith the pre(ious day off program and no' using a percent of sales increase to determine if employees get indi(idual &onuses. If sales increased &y at least 23 then all employees get a .#/ &onus. If they do not0 then indi(idual &onuses are /.

Step 1: To accommodate the changes to the program( create the additional #ariables needed. Create a #ariable named storeAmount to hold the store bonus amount. Create a #ariable named empAmount to hold the indi#idual bonus amount. Create a #ariable named salesIncrease to hold the percent of increase.

Starting Out with Programming Logic and Design


44Declare local (aria&les Declare *eal monthlySales Declare *eal storeAmount Declare *eal empAmount Declare *eal salesIncrease

Step 2: The first module in the program is getSales34. Since this is still re,uired( lea#e this module as is. This module should be written as follows
44!)D56E 1 44this module ta7es in the re-uired user input !odule getSales8*eal *ef monthlySales9 Display Enter the total sales for the month." Input monthlySales End !odule

Step ": The second module in the program was is%onus34. Since there are two t$pes of bonuses now( rename this module and the module call to store%onus34. 'rite an if then else statement within this module that will set the bonus amount to either 7::: or 7::. !lso( pass the #ariable store!mount to the module as a reference. Complete the missing lines. 3"eference Dual !lternati#e Decision Structures( page 1/74.
44!)D56E % 44this module 'ill determine 'hat the &onus le(els are !odule store&:onuses8*eal monthlySales0 *eal *ef storeAmount9 If monthlySales $=1///// Then Set storeAmount = #/// Else Set storeAmount = #// End If End !odule

Step 4: 'rite a module that will as+ the user to enter the percent of sales increase in decimal format. This module will ha#e to accept sales;ncrease as a reference. Complete the missing lines.
44!)D56E 3 44this module ta7es in percent of increase in decimal 44format such as ./% for % percent. !odule getIncrease8*eal *ef salesIncrease9 Display Enter the percent of sale increase in decimal format." Input salesIncrease End !odule

Step $: 'rite a module that will determine indi#idual bonuses. ;f the sales increase percent was 4< or more( then all emplo$ees get a =7: bonus. ;f the sales increase was

Starting Out with Programming Logic and Design

not reached( then the bonus amount should be set to >ero. This module should be called emp%onus and accept sales;ncrease as a normal #ariable and emp!mount as a reference.
44!)D56E 2 44this module 'ill determine 'hat the &onus le(els are !odule emp:onus8*eal salesIncrease0 *eal *ef empAmount 9 If salesIncrease $= 23 Then Set empAmount = #/ Else Set empAmount = / End If End !odule

Step %: 'rite a module that will print the store bonus and the emplo$ee bonus amount. Name this module print%onus34 and pass the two necessar$ #ariables.
44!)D56E # 44this module 'ill display store and employee &onus info. !odule print:onus898*eal storeAmount0 *eal empAmount9 Display The store &onus is ."0 storeAmount Display The employee &onus is ."0 empAmount End !odule

Step &: The final step in completing the pseudocode is to call all the modules with the proper arguments. Complete the missing lines.
!odule main 89 44Declare local (aria&les Declare *eal monthlySales Declare *eal storeAmount Declare *eal empAmount Declare *eal salesIncrease 44;unction calls <all getSales8monthlySales9 <all getIncrease8salesIncrease9 <all store:onus8monthlySales0 storeAmount9 <all emp:onus8salesIncrease0 empAmount 9 <all print emp:onus8storeAmount0 empAmount9 End !odule

Starting Out with Programming Logic and Design

Lab 4." #seudocode: 'ested Decision Structures


Critical "e#iew To test more than one condition( a decision structure can be nested inside another decision structure. This structure can become #er$ comple&( and often an if@then@else@if statement is used instead. The general structure of the if@then@else@if statement is
If condition=1 Then Statement Statement Etc. Else If condition=% Then Statement Statement Etc. Insert as many Else If clauses as necessary Else Statement Statement Etc. End If

! case structure lets the #alue of a #ariable or an e&pression determine which path of e&ecution the program will ta+e. This is often used as an alternati#e to a nested if else decision. )elp *ideo Double clic+ the file to #iew #ideo

The compan$ now wants to add additional le#els to their store and emplo$ee bonuses. The new le#els are as follows
Store &onuses> If store sales If store sales If store sales If store sales are are are are .,/0/// or more0 store &onus is .3/// .?/0/// or more0 store &onus is .2/// .1//0/// or more0 store &onus is .#/// .11/0/// or more0 store &onus is .@///

Employee &onuses> If percent of increase is 33 or more0 employee &onus is .2/ If percent of increase is 23 or more0 employee &onus is .#/ If percent of increase is #3 or more0 employee &onus is .A#

Step 1: 6odif$ the store%onus module to write a nested if else statement to set the new bonus le#els. Complete the missing lines. 3"eference The if@then@else Statement( page 14:4.
44!)D56E % 44this module 'ill determine 'hat the &onus le(els are

Starting Out with Programming Logic and Design


!odule store:onus 8*eal monthlySales0 *eal *ef storeAmount9 If monthlySales $= 11//// Then Set storeAmount = @/// Else If monthlySales $= 1///// Then Set storeAmount = #/// Else if monthlySales $= ?//// Then Set storeAmount = 2/// Else if monthlySales $= ,//// Then Set storeAmount = 3/// Else Set storeAmount = / End If End !odule

Step 2: 6odif$ the emp%onus module to write a nested if else statement to set the new bonus le#els. Complete the missing lines. 3"eference The if@then@else Statement( page 14:4.
44!)D56E 2 44this module 'ill determine 'hat the &onus le(els are !odule emp:onus 8*eal salesIncrease0 *eal *ef empAmount9 If salesIncrease $= ./# Then Set empAmount = A# Else If salesIncrease $= ./2 Then Set empAmount = #/ Else if salesIncrease $= ./3 Then Set empAmount = 2/ Else Set empAmount = / End If End !odule

Step ": 6odif$ 6odule 7 b$ adding an if statement that will print a message if both the store bonus and the emplo$ee bonus are the highest amounts possible. 3"eference Logical Operators( page 1414.
44!)D56E # 44this module 'ill display store and employee &onus info. !odule print:onus8*eal storeAmount0 *eal empAmount9 Display The store &onus is ."0 storeAmount Display The employee &onus is ."0 empAmount If store!mount -- @/// AND empAmount == A# Then Display <ongratsB +ou ha(e reached the highest &onus amounts possi&leB" End If End !odule

Starting Out with Programming Logic and Design

Lab 4.4 (lo)charts


Critical "e#iew ! dual alternati#e decision structure has two possible paths of e&ecution A one path is ta+en if a condition is true( and the other path is ta+en if the condition is false. ! diamond with a true and false #alue is used in flowcharting a dual alternati#e decision structure.

Nested if@then@else flowcharts loo+ as follows

)elp *ideo Double clic+ the file to #iew #ideo

Starting Out with Programming Logic and Design

This lab re,uires $ou to con#ert $our pseudocode in Lab 4.. to a flowchart. Cse an application such as "aptor or *isio. Step 1: Start "aptor and sa#e $our document as Lab 4-4. The .rap file e&tension will be added automaticall$. Start b$ adding a Comment bo& that declares $our #ariables. Step 2: The ne&t step in $our flowchart should be to call $our methods. 6ain should loo+ as follows. %e sure to clic+ $es to add new tabs for each module.

Step ": Clic+ on the getSales tab and add the necessar$ code to enter the monthl$ sales. 8our getSales method might loo+ li+e the following

Starting Out with Programming Logic and Design Step 4: Clic+ on the get;ncrease tab and add the necessar$ code to enter the percent of increase in sales. Since percentages can be entered differentl$( $ou should specif$ the method to the user. Dither method is fine( as long as $ou specif$. One method is as follows

1:

)ere is a different method whereas the number entered is di#ided b$ 1::

Step $: Clic+ on the store%onus tab and add the nested if@then@else statement from Lab 4..( Step 1. Do not forget the final else of setting store!mount to :. The start of $our module should loo+ as follows( and $ou should ha#e a total of four decisions

Starting Out with Programming Logic and Design Step %: Clic+ on the emp%onus tab and add the nested if@then@else statement from Lab 4..( Step /. Do not forget the final else of setting emp!mount to :. The start of $our module should loo+ as follows( and $ou should ha#e a total of three decisions

11

Step &: Clic+ the print%onus tab and add the necessar$ code from Lab 4..( Step .. The module should loo+ as follows

Step *: 'hen $our program is complete( test the following monthl$ sales and sales increases to ensure that the output matches the following. ;f $our output is different( then re#iew $our code.

Starting Out with Programming Logic and Design

1/

+nput ,alues monthl$Sales - 1/:7:: sales;ncrease - 7 monthl$Sales - B.4:: sales;ncrease - 7 monthl$Sales - 17::: sales;ncrease - 1.7 monthl$Sales - 0/::: sales;ncrease - ..? monthl$Sales - 1/7::: sales;ncrease - 4.7

!pected Output The store bonus amount is =?::: The emplo$ee bonus amount is =17 CongratsE 8ou ha#e reached the highest bonus amounts possibleE The store bonus amount is =4::: The emplo$ee bonus amount is =17 The store bonus amount is =: The emplo$ee bonus amount is =: The store bonus amount is =.::: The emplo$ee bonus amount is =4: The store bonus amount is =?::: The emplo$ee bonus amount is =7:

Step -: The final step is to insert $our finished flowchart in the space below. ;nside "aptor( select File and the Print to Clipboard from the menu. ;nside 'ord in the space below( select Ddit and Paste. 8ou will ha#e to do this for each module $ou created.

Starting Out with Programming Logic and Design

1.

Starting Out with Programming Logic and Design

14

Starting Out with Programming Logic and Design

17

Lab 4.$ #.thon /ode


Critical "e#iew ;n code we write a dual alternati#e decision structure as an if@else statement. )ere is the general format of the if@else statement if condition> statement statement etc. else> statement statement etc. )ere is the general format of the if@elifCelse statement if condition_1> statement statement etc. elif condition_2> statement statement etc. Insert as many elif clauses as necessary else> statement statement etc. The logical operators and0 or0 and not are used in P$thon to connect %oolean e&pressions. )elp *ideo Double clic+ the file to #iew #ideo

Step 1: Start the ;DLD Dn#ironment for P$thon. Prior to entering code( sa#e $our file b$ clic+ing on File and then Sa#e. Select $our location and sa#e this file as Lab4-5.py. %e sure to include the .p$ e&tension. Step 2: Document the first few lines of $our program to include $our name( the date( and a brief description of what the program does.

Starting Out with Programming Logic and Design Step ": Start $our program with the following code
D6a& 2C# DThe main function def main89> monthlySales = getSales89 Dcall to get sales DThis function gets the monthly sales def getSales89> monthlySales = input8EEnter the monthly sales .E9 monthlySales = float8monthlySales9 return monthlySales DThis function gets the percent of increase in sales def getIncrease89> DThis function determines the storeAmount &onus def store:onus89> DThis function determines the empAmount &onus def emp:onus89> DThis function prints the &onus information def print:onus89> Dcalls main main89

1?

Step 4: Cnder the get;ncrease function( add the necessar$ code to allow the user to input sales increase. 8our code might loo+ as follows
DThis function gets the percent of increase in sales def getIncrease89> salesIncrease = input8EEnter percent of sales increase. ;or eFample 23 should &e entered as 2> E9 salesIncrease = float8salesIncrease9 salesIncrease = salesIncrease 4 1// return salesIncrease

Step $: Cnder the call to getSales34( add a function call to get;ncrease34.


salesIncrease = getIncrease89 Dcall to get sales increase

Step %: Cnder the store%onus function( add the necessar$ code so that the program will determine what the proper store!mount #ariable should ha#e. This function might loo+ as follows
DThis function determines the storeAmount &onus def store:onus8monthlySales9>

Starting Out with Programming Logic and Design


if monthlySales $=11////> storeAmount = @/// elif monthlySales $=1/////> storeAmount = #/// elif monthlySales $=?////> storeAmount = 2/// elif monthlySales $=,////> storeAmount = 3/// else> storeAmount = / return storeAmount

11

Step &: Cnder the call to get;ncrease34( add a function call to store%onus34.
Dcall to get the store &onus storeAmount = store:onus8monthlySales9

Step *: "epeat the similar process in step ? and 1 for writing the emp%onus34 function and ma+ing a call to it. "ecall that this function uses sales;ncrease to determine emp!mount. Step -: Code the print%onus34 function to print the store!mount and emp!mount. This function might loo+ as follows
DThis function prints the &onus information def print:onus8storeAmount0 empAmount9> print EThe store &onus amount is .E0 storeAmount print EThe employee &onus amount is .E0 empAmount if storeAmount == @/// and empAmount == A#> print E<ongratsB +ou ha(e reached the highest &onus amounts possi&leBE

Step 10: Cnder the call to emp%onus34( add a function call to print%onus. This call might loo+ as follows
print:onus8storeAmount0 empAmount9 Dcall to print amounts

Step 11: Clic+ "un and "un 6odule to see how $our program processes. Test the following #alues to #erif$ the e&pected output. +nput ,alues monthl$Sales - 1/:7:: sales;ncrease - 7 monthl$Sales - B.4:: sales;ncrease - 7 monthl$Sales - 17::: !pected Output The store bonus amount is = ?::: The emplo$ee bonus amount is = 17 CongratsE 8ou ha#e reached the highest bonus amounts possibleE The store bonus amount is =4::: The emplo$ee bonus amount is =17 The store bonus amount is =:

Starting Out with Programming Logic and Design sales;ncrease - 1.7 monthl$Sales - 0/::: sales;ncrease - ..? monthl$Sales - 1/7::: sales;ncrease - 4.7 The emplo$ee bonus amount is =: The store bonus amount is =.::: The emplo$ee bonus amount is =4: The store bonus amount is =?::: The emplo$ee bonus amount is =7:

10

Step 12: D&ecute $our program so that it wor+s and paste the final code below

GLab 4@7 GThe main function def main34 monthl$Sales - getSales 34 Gcall to get sales GThis function gets the monthl$ sales def getSales34 monthl$Sales - input 3HDnter the monthl$ sales =H4 monthl$Sales - float 3monthl$Sales4 return monthl$Sales sales;ncrease - get;ncrease34 Gcall to get sales increase GThis function gets the percent of increase in sales def get;ncrease34 sales;ncrease - input 3HDnter percent of sales increase. For e&ample 4< should be enterned as 4 H4 sales;ncrease - float 3sales;ncrease4 sales;ncrease - sales;ncrease I 1:: return sales;ncrease Gcall to get the store bonus store!mount - store%onus3monthl$Sales4 GThis function determines the store!mount bonus def store%onus3monthl$Sales4 if monthl$Sales J-11:::: store!mount - ?::: elif monthl$Sales J-1::::: store!mount - 7::: elif monthl$Sales J-B:::: store!mount - 4::: elif monthl$Sales J-0:::: store!mount - .::: else store!mount - :

Starting Out with Programming Logic and Design return store!mount Gcall to get the emplo$ee bouns sales;ncrease - emp%onus 3monthl$Sales4 GThis function determines the emp!mount bonus def emp%ouns34 if sales;ncrease J- 7 emp!mount - 17 elif sales;ncrease J- 4 emp!mount - 7: elif sales;ncrease J- . emp!mount - 4: else emp!mount - : return emp!mount print bonus 3store!mount( emp!mount4 Gcalls to print amounts GThis function prints the bonus information def print%onus 3store!mount( emp!mount4 print HThe store bonus amount is =H( store!mount print HThe emplo$ee bonus is =H( emp!mount if store!mount -- ?::: and emp!mount -- 17 print HCongratsE 8ou ha#e reached the highest bouns amounts possibleEH

1B

Gcalls main main 34

Starting Out with Programming Logic and Design

/:

Lab 4.% #rogra11ing /hallenge 1 Tip2 Ta!2 and Total


'rite the Pseudocode( Flowchart( and P$thon code for the following programming problem.
)elp *ideo Double clic+ the file to #iew #ideo

"ecall the Tip( Ta&( and Total program from Lab /.?. 6odif$ $our program to include new re,uirements.
Grite a program that 'ill calculate a HHH3 tip and a @3 taF on a meal price. The user 'ill enter the meal price and the program 'ill calculate tip0 taF0 and the total. The total is the meal price plus the tip plus the taF. +our program 'ill then display the (alues of tip0 taF0 and total. The restaurant no' 'ants to change the program so that the tip percent is &ased on the meal price. The ne' amounts are as follo's> Meal Price Range ./1 to #.?? @ to 1%.// 1%./1 to 1A.// 1A./1 to %#.// %#./1 and more Tip Percent 1/3 133 1@3 1?3 %%3

The #seudocode 6odule 6ain34 IIDeclare #ariables Declare real mealPrice Declare real tip Declare real ta& Declare real total Function Calls34 Call inputKmeal3mealprice4 Call calcKtip3mealprice( tip4 Call calcKta&3mealprice( ta&4 Call calcKtotal3mealprice( ta&( tip( total4 Call printKinfo3mealprice( ta&( tip( total4 Dnd module IIthis function will input mealprice

Starting Out with Programming Logic and Design 6odule inputKmeal3"eal "ef mealprice4 Displa$ 3LDnter the meal price =54 ;nput mealprice Dnd module IIThis function will calculate the tip 6odule calcKtip3"eal mealprice( "eal "ef tip4 ;f mealprice J- .:1 !ND mealprice M- 7.BB Then Set tip - mealprice N .1: Dlse if mealprice J- ? !ND mealprice M-1/ Then Set tip - mealprice N .1. Dlse if mealprice J-1/.:1 !ND mealprice M-11 Then Set tip - mealprice N .1? Dlse if mealprice J- 11.:1 !ND mealprice M- /7 Then Set tip - mealprice N .1B Dlse Tip - mealprice N .// Dnd module IIThis function will calculate the ta& at ?< 6odule calcKta&3"eal mealprice( "eal "ef ta&4 Set ta& - mealprice N .:? Dnd module IIThis function will calculate tip( ta&( and total cost 6odule calcKtotal 3"eal mealprice( "eal "ef total( "eal tip( "eal ta&4 Set total - mealprice O tip O ta& Dnd module IIThis function will print the tip( ta&( and total cost 6odule printKinfo3"eal mealprice( "eal total( "eal tip( "eal ta&4 Displa$ LThe mealprice is =5 P mealpruce Displa$ LThe tip is =5( tip Displa$ LThe ta& is =5 ( ta& Displa$ LThe total is =5 ( total Dnd module

/1

The (lo)chart

Starting Out with Programming Logic and Design

//

Starting Out with Programming Logic and Design

/.

The #.thon /ode

GThe main function def main34 print Q'elcome to tht tip and ta& calculator programQ print Gprints a blan+ line meaplprice - inputKmeal34 tip - calcKtip3mealprice4 ta& - calcKta& 3mealprice4 total - calcKtotal3mealprice( tip( ta&4 printKinfo3mealprice( tip( ta&( total4 GThis function will input mealprice def inputKmeal34 mealprice - input3HDnter the meal price =H4 mealprice - float3mealprice4 return mealprice GThis function will calculate the tip at /:<

Starting Out with Programming Logic and Design def calcKtip3mealprice4 ;f mealprice J- .:1 !ND mealprice M- 7.BB tip - mealprice N .1: elif mealprice J- ? !ND mealprice M- 1/ tip - mealprice N .1. elif mealprice J- 1/.:1 !ND mealprice M- 11 tip - mealprice N .1? elif mealprice J- 11.:1 !ND mealprice M- /7 tip - mealprice N.1B else tip - mealprice N .// GThis function will calculate the ta& at ?< def calcKta&3mealprice4 ta& - mealprice N .:? return ta& GThis funtion will calculate the tip( ta&( and total cost def calcKtotal3mealprice( tip( ta&4 total - mealprice O tip O ta& return total GThis function will print the tip( ta&( and total cost def printKinfo3mealprice( tip( ta&( total4 print QThe mealprice is =Q ( mealprice print QThe tip is =Q ( tip print QThe ta& is =Q ( ta& print QThe total is =Q ( total Gcalls main main34

/4

Das könnte Ihnen auch gefallen