Tuesday, October 13, 2015

Visual Basic Programming Exercise Chapter 4 Making Decisions


Visual Basic Programming Chapter 4: Making Decisions



4.4.

The If...Then....ElseIf Statement


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 through 64, and adds 1 to the variable  seniors if age is 65 or older. 

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


OR


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 the values of the bool variable  isPremiumCustomerand the int variable nbooksPurchased.


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 if 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.


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.
 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

No comments:

Post a Comment