Chapter 4 Homework Solutions:
What is the general format of
the statement used to code decisions in an application?
If (condition) Then
[ElseIf (condition) Then
What is a condition?
The test in an If statement is based on a condition. To form conditions, six relational operators, >, <, =, <>, >=, and <=, are used to compare values. The result of the comparison is either True or False.
Explain the purpose of relational operators and logical operators.
Conditions are formed using the six relational operators, >, <, =, <>, >=, and <=. The relational operators are used to compare values. Compound conditions test more than one condition. Compound conditions are created using the logical operators; And, Or, and Not.
Differentiate between a comparison performed on numeric data and a
comparison performed on string data.
An algebraic comparison is made when numeric values are involved in a test; that is, the sign of the number is taken into account. String variables can be compared to other string variables or string literals enclosed in quotation marks. The comparison begins with the leftmost character and proceeds one character at a time from left to right. As soon as a character in one string is not equal to the corresponding character in the second string, the comparison is terminated, and the string with the lower ranking character is judged less than the other.
How does Visual Basic compare the Text property of a text box?
When you compare the Text property of a text box with another value the Text property behaves like a variant. Visual Basic compares one text box to another as strings and compares a text box to a numeric variable or constant with a numeric compare. You can force a numeric comparison on a Text property by using the Val function.
Why would it be useful to include the Ucase
function in a comparison?
When comparing strings, the case of the characters is important. An uppercase “Y” does not compare equal to a lowercase “y”. Since the user may type a name or word in either uppercase or lowercase or a combination of the two, we must check all possibilities. The easiest way is to use the string function Ucase or Lcase, which returns the uppercase or lowercase equivalency of a string, respectively.
Name the types of information that can be used in a comparison.
The conditions to be tested can be formed with numeric variables and constants, string 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.
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 Boolean variable name, it holds the values True or False.
If blnSuccessfulOperation = True Then
is equivalent to
If blnSuccessfulOperation Then…
Boolean variables hold the value zero when False, and negative one when True.
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 <= 40 Then
If intPayRate > 0 Then
cPay = intHours * intPayRate
MsgBox “Please Input the Rate of Pay”
MsgBox “Please enter the number of non-overtime hours worked this week.”
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.
Give an example of three message box constants.
OK button vbOKOnly
Critical Message icon vbCritical
Warning Query icon vbQuestion
Warning Message icon vbExclamation
Information Message icon vbInformation
Define the term validation. When
is it appropriate to do validation?
Verifying that appropriate values have been entered for a text box is called validation. Validation may include making sure that data is numeric, checking for specific values, or making sure that required items are entered. Validation should be done to check the values entered into text boxes before beginning any calculations.
Define the term checking a range?
Checking a range means checking the reasonableness of a value. For example, you could display a message box to the user if they input their age as 200 instead of 20.
Explain the difference between Step Into and Step Over.
The stepping commands, Step Into or Step Over, are used at design time. The program will begin running and immediately transfer to break time. With each of these commands the project executes a single line at a time and displays the Code window with the current statement highlighted. Most likely you will use the Step Into command more than Step Over. When you choose Step Into (from the menu, the toolbar button, or F8), the next line of code is executed and the program pauses again in break time. If the line of code is a call to another procedure, the first line of code of the other procedure will be displayed. The Step Over command also executes one line of code at a time. The difference between Step Over and Step Into occurs when your code has calls to other procedures. Step Over will not display the code in the called procedure, but only displays the lines of code in the current procedure being analyzed. You can choose Step Over from the menu, from the toolbar button, or press Shift + F8.
What steps are necessary to view the current contents of a variable
during program execution?
The steps for viewing the contents of a variable during run time are
1. Break the execution (using the Run menu, the toolbar button, or a breakpoint).
2. Click on the View Code button in the Project Explorer window or select Code from the View menu.
3. Point to the variable or expression you wish to view.
The current contents of the expression will pop up in a label.