If...Then…Else exercise Answers

 

Given the indicated values, what output would be produced for each of the following?

 

1)

txtHours.Text

txtRate.Text

 

sngHours

sngRate

sngRegPay

sngOvertimePay

Output

40

10

 

40

10

400

0

400

0

45

10

 

45

10

400

75

400

75

30

10

 

30

10

300

*

300

*75

 

 

 
 

 

 

 

 

 

 


*Depending on where variables are declared and where this snippet of code is located in a program the previous value of sngOverTimePay may still be in memory.

 
sngHours =  Val(txtHours.Text)

sngRate = Val(txtRate.Text)

 

If sngHours > 40 then    

      sngRegPay = 40 * sngRate

      sngOvertimePay = (sngHours – 40) * (sngRate * 1.5)

Else

      sngRegPay = sngHours * sngRate

End if

 

Print sngRegPay

Print sngOvertimePay

 

 

2

txtHours.Text

txtRate.Text

 

sngHours

sngRate

sngRegPay

sngOvertimePay

Output

42

10

 

42

10

400

(True path taken)

30

400

30

20

10

 

20

10

200

(false path taken)

0

200

0

 

 

 
 

 

 

 

 

 

 


This time the code explicitly assigns a value to both sngRegPay and sngOverTimePay no matter which path is taken.

 
sngHours =  Val(txtHours.Text)

sngRate = Val(txtRate.Text)

 

If sngHours > 40 then    

      sngRegPay = 40 * sngRate

      sngOvertimePay = (sngHours – 40) * (sngRate * 1.5)

Else

      sngRegPay = sngHours * sngRate

sngOvertimePay = 0

End if

 

Print sngRegPay

Print sngOvertimePay

 

 

3)

    txtBalance

txtDateDue.Text

 

sngBalance

dtmDue

sngNewBalance

output

42

8/15/2007

 

42

8/15/2007

42.00

(True path taken)

42.00

42.00

20

8/15/2006

 

20

8/15/2006

20.30

(False path taken)

20.00

20.30

 

Assuming current date = 9/14/2004

 

 

 

 

 

sngBalance = Val(txtBalance.Text)

dtmDue = Val(txtDateDue.Text)

 

If dtmDue > Date then            Date returns the current date in the system

      sngNewBalance = sngBalance

Else

      sngNewBalance = sngBalance + (sngBalance * .015)

End If

 

Print sngBalance

Print sngNewBalance