![]() |
Introduction to Conditional Statements |
|
Checking Whether a Condition is True/False |
|
Introduction |
|
In some programming assignments, you must find out whether a given situation bears a valid value. This is done by checking a condition. To support this, the Visual Basic language provides a series of words that can be combined to perform this checking. Checking a condition usually produces a True or a False result. Once the condition has been checked, you can use the result (as True or False) to take action. Because there are different ways to check a condition, there are also different types of keywords to check different things. To use them, you must be aware of what each does or cannot do so you would select the right one. |
|
|
Module BethesdaCarRental
Public Function Main() As Integer
Dim EmployeeName As String
Dim StrHourlySalary As String, StrWeeklyHours As String
Dim HourlySalaryFormatter As String
Dim WeeklyHoursFormatter As String
Dim Payroll As String
HourlySalaryFormatter = "0.00"
WeeklyHoursFormatter = "0.00"
EmployeeName = InputBox("Enter Employee Name:", _
"Bethesda Car Rental", _
"John Doe")
StrHourlySalary = InputBox("Enter Employee Hourly Salary:", _
"Bethesda Car Rental", _
"0.00")
HourlySalaryFormatter = FormatCurrency(CDbl(StrHourlySalary))
StrWeeklyHours = InputBox("Enter Employee Weekly Hours:", _
"Bethesda Car Rental", _
"0.00")
WeeklyHoursFormatter = FormatNumber(CDbl(StrWeeklyHours))
Payroll = "======================" & vbCrLf & _
"=//= BETHESDA CAR RENTAL =//=" & vbCrLf & _
"==-=-= Employee Payroll =-=-==" & vbCrLf & _
"-------------------------------------------" & vbCrLf & _
"Employee Name:" & vbTab & EmployeeName & vbCrLf & _
"Hourly Salary:" & vbTab & _
HourlySalaryFormatter & vbCrLf & _
"Weekly Hours:" & vbTab & _
WeeklyHoursFormatter & vbCrLf & _
"======================"
MsgBox(Payroll, MsgBoxStyle.Information Or MsgBoxStyle.OkOnly, _
"Bethesda Car Rental")
Return 0
End Function
End Module
|
|
|
|
|
|
The If...Then statement examines the truthfulness of an expression. Structurally, its formula is: If ConditionToCheck Then Statement Therefore, the program examines a condition, in this case ConditionToCheck. This ConditionToCheck can be a simple expression or a combination of expressions. If the ConditionToCheck is true, then the program will execute the Statement. There are two ways you can use the If...Then statement. If the conditional formula is short enough, you can write it on one line, like this: If ConditionToCheck Then Statement Here is an example: |
To assist you with checking a condition and its alternative, the Visual Basic language provides a function named IIf. Its syntax is: Public Function IIf( _ ByVal Expression As Boolean, _ ByVal TruePart As Object, _ ByVal FalsePart As Object _ ) As Object This function operates like an If...Then...Else condition. It takes three required arguments and returns a result of type Object. This returned value will hold the result of the function. The condition to check is passed as the first argument:
As mentioned already, you can retrieved the value of the right argument and assign it to the result of the function. The expression we saw early can be written as follows: Module Exercise
Public Function Main() As Integer
Dim MemberAge As Int16
Dim MemberCategory As String
MemberAge = 16
MemberCategory = IIf(MemberAge <= 18, "Teen", "Adult")
MsgBox("Membership: " & MemberCategory)
Return 0
End Function
End Module
This would produce the same result we saw earlier.
We saw how to call a function named Choose where only one value is being considered. The reality is that if there is an alternate value, the function produces a null result. Consider the same program we used earlier but with a different value: Module Exercise
Public Function Main() As Integer
Dim Status As UShort, EmploymentStatus As String
Status = 2
EmploymentStatus = Choose(Status, "Full Time")
MsgBox(EmploymentStatus)
Return 0
End Function
End Module
This would produce:
Notice that the function returns nothing (an empty string). To use this function as an alternative to the If...Then...Else operation, you can pass two values for the second argument. The second argument is actually passed as a list of values. Each value has a specific position as its index. To use the function in an If...Then...Else implementation, pass two values for the second argument. Here is an example: Choose(Status, "Full Time", "Part Time") The second argument to the function, which is the first value of the Choose argument, has an index of 1. The third argument to the function, which is the second value of the Choose argument, has an index of 2. When the Choose() function is called, if the first argument has a value of 1, then the second argument is validated. If the first argument has a value of 2, then the third argument is validated. As mentioned already, you can retrieve the returned value of the function and use it however you want. Here is an example: Module Exercise
Public Function Main() As Integer
Dim Status As UShort, EmploymentStatus As String
Status = 2
EmploymentStatus = Choose(Status, "Full Time", "Part Time")
MsgBox("Employment Status: " & EmploymentStatus)
Return 0
End Function
End Module
This would produce:
We saw earlier how to call the Switch function in an If...Then condition. Once again, the problem is that if you call it with a value that is not checked by the first argument, the function produces a null value (an empty string). To apply this function to an If...Then...Else scenario, you can call it using the following formula: Switch(Condition1ToCheck, Statement1, Condition2ToCheck, Statement2) In the Condition1ToCheck placeholder, pass a Boolean expression that can be evaluated to True or False. If that condition is true, the second argument would be executed. To provide an alternative to the first condition, pass another condition as Condition2ToCheck. If the Condition2ToCheck is true, then Statement2 would be executed. Once gain, remember that you can get the value returned by the Switch function and use it. Here is an example: Module Exercise
Private Enum EmploymentStatus
FullTime
PartTime
Contractor
Seasonal
Unknown
End Enum
Public Function Main() As Integer
Dim Status As EmploymentStatus
Dim Result As String
Status = EmploymentStatus.PartTime
Result = "Unknown"
Result = Microsoft.VisualBasic.Switch( _
Status = EmploymentStatus.FullTime, "Full Time", _
Status = EmploymentStatus.PartTime, "Part Time")
MsgBox("Employment Status: " & Result)
Return 0
End Function
End Module
This would produce:
|
|
|
||
| Previous | Copyright © 2008-2010 FunctionX | Next |
|
|
||