Chapter 4 Homework Solutions

download Chapter 4 Homework Solutions

of 4

Transcript of Chapter 4 Homework Solutions

  • 7/31/2019 Chapter 4 Homework Solutions

    1/4

    Chapter 4 Homework Solutions:

    Chapter 4-Decisions and Conditions

    ANSWERS TO REVIEW QUESTIONS

    1. What is the general format of the statement used to code decisions in an application?If (condition) Then

    statement(s)

    [ElseIf (condition) Then

    statement(s)]

    [Else

    statement(s)]

    End If

    2. What is a condition?

    The test in an If statement is based on a condition. To form conditions, six relational

    operators, >, =, and , =, and

  • 7/31/2019 Chapter 4 Homework Solutions

    2/4

    variables and constants, object properties, and arithmetic expressions. However, it is

    important to note that comparisons must be made on like types; that is, strings can be

    compared only to other strings, and numeric values can be compared only to other numeric

    values, whether a variable, constant, property, or arithmetic expression.

    8. Explain a Boolean variable test for True or False. Give an example.

    Visual Basic evaluates the condition in an If statement. If the condition is a Booleanvariable name, it holds the values True or False.

    For example:

    If blnSuccessfulOperation = True Then

    is equivalent to

    If blnSuccessfulOperation ThenBoolean variables hold the value zero when False, and negative one when True.

    9. Give an example of a situation where nested Ifs would be appropriate.

    An example where nested Ifs would be appropriate would be a situation where multiple

    conditions must be tested and input validation required. The following code would be

    extremely awkward if block ifs were used instead.

    intHours = Val(txtHours.Text)

    intPayRate = Val(txtPayRate.Text)

    If intHours > 0 And intHours 0 Then

    cPay = intHours * intPayRate

    Else

    MsgBox Please Input the Rate of Pay

    End If

    Else

    MsgBox Please enter the number of non-overtime hours worked this week.EndIf

    10. When would a message box be used?

    A message box would be used whenever a message must be displayed to the user.

    Validating input data is an appropriate time to use a message box. If we reject bad data, we

    need to let the user know why the desired action was not achieved.

  • 7/31/2019 Chapter 4 Homework Solutions

    3/4

  • 7/31/2019 Chapter 4 Homework Solutions

    4/4