Tuesday, February 18, 2014

Section 4.2: If Blocks - ElseIf Clauses:




40121:  Write an if/else statement that adds 1 to the variable minors if the variable age is less than 18, adds 1 to the variable adults if age is 18 through 64 and adds 1 to the variable seniors if age is 65 or older.

If (age < 18) Then
minors += 1
ElseIf (age < 65) Then
adults += 1
Else
seniors += 1
End If

40367:  Write an if/else statement that adds 1 to the variable minors if the variable age is less than 18, adds 1 to the variable adults if age is 18 thorugh 64, and adds 1 to the variable seniors if age is 65 or older.

If (age < 18) Then
minors += 1
ElseIf (age < 65) Then
adults += 1
Else
seniors += 1
End If

40368:  Write an if/else statement that compares the double variable pH with 7.0 and makes the following assignments to the bool variables neutral, base, and acid:  false, false, true if pH is less than 7.  false, true, false if pH is greater than 7. true, false, false if pH is equal to 7.

If (pH < 7) Then
neutral = false
base = false
acid = true
ElseIf (pH > 7) Then
neutral = false
base = true
acid = false
ElseIf (pH = 7) Then
neutral = true
base = false
acid = false
End If

41064:  Online Book Merchants offers premium customers 1 free book with every purchase of 5 or more books and offers 2 free books with every purchase of 8 or more books.  It offers regular customers 1 free book with every purchase of 7 or more books, and offers 2 free books with every purchase of 12 or more books.  Write a statement that assigns freeBooks the appropriate value based on teh values of the bool variable isPremiumCustomer and the int variable nbooksPurchased.

If isPremiumCustomer Then
if (nbooksPurchased > 4) And (nbooksPurchased < 8)
freebooks = 1
elseif (nbooksPurchased >= 8)
freebooks = 2
elseif (nbooksPurchased < 5)
freebooks = 0
End If
Else
if (nbooksPurchased > 6) And (nbooksPurchased < 12)
freebooks = 1
elseif (nbooksPurchased >= 12)
freebooks = 2
elseif (nbooksPurchased < 7)
freebooks = 0
End If
End If




41062:  Write a statement that increments (adds 1 to) one and only one of these five variables:  reverseDrivers parkedDrivers slowDrivers safeDrivers speeders.  The variable speed determines which of the five is incremented as follows:  The statement increments reverseDrivers is speed is less than 0, increments parkedDrivers if speed is less than 1, increments slowDrivers if speed is less than 40, increments safeDrivers if speed is less than or equal to 65, and otherwise increments speeders.

If (speed > 65) Then
speeders += 1
Elseif (speed >= 40)
safeDrivers += 1
Elseif (speed >= 1)
slowDrivers += 1
Elseif (speed >= 0)
parkedDrivers += 1
Elseif (speed < 0)
reverseDrivers += 1
End If

40367:  Write an if/else statement that adds 1 to the variable minors if the variable age is elss than 18, adds 1 to the variable adults if age is 18 through 64, and adds 1 to the variable seniors if age is 65 or older.

If (age < 18) Then
minors += 1
ElseIf (age < 65) Then
adults += 1
Else
seniors += 1
End If

41062:  Write a statement that increments (adds 1 to) one and only one of these five variables:  reverseDrivers parkedDrivers slowDrivers safeDrivers speeders.  The variable speed determines which of the five is incremented as follows:  The statement increments reverseDrivers is speed is less than 0, increments parkedDrivers if speed is less than 1, increments slowDrivers if speed is less than 40, increments safeDrivers if speed is less than or equal to 65, and otherwise increments speeders.

If (speed > 65) Then
speeders += 1
Elseif (speed >= 40)
safeDrivers += 1
Elseif (speed >= 1)
slowDrivers += 1
Elseif (speed >= 0)
parkedDrivers += 1
Elseif (speed < 0)
reverseDrivers += 1
End If




41063:  Write a statement that compares the values of score1 and score2 and takes the following actions.  When score1 exceeds score2, the message "player1 wins" is printed to standard out.  When score2 exceeds score1, the message "player2  wins" is printed to standard out.  IN each case, the variables player1Wins, player1Losses, player2Wins and player2Losses are incremented when appropriate.  Finally, in the event of a tie, the message "tie" is printed and the variable tieCount is incremented.

If (score1 > score2) Then
System.Console.Writeline("player1 wins")
player1Wins += 1
player2Losses += 1
ElseIf (score1 < score2)
System.Console.Writeline("player2 wins")
player2Wins += 1
player1Losses += 1
ElseIf (score1 = score2)
System.Console.Writeline("tie")
tieCount += 1
End If

1 comment: