The If Statementt
Using an Example in our Program
|
In the previous chapter we programmed the Simple Calculator Form with this code:
Dim dNumber1 As Decimal
Dim dNumber2 As Decimal
Dim dNumberResult As Decimal
dNumber1 = CDec(TextBox1.Text)
dNumber2 = CDec(TextBox2.Text)
If RadioButton1.Checked = True Then dNumberResult = dNumber1 + dNumber2
If RadioButton2.Checked = True Then dNumberResult = dNumber1 – dNumber2
If RadioButton3.Checked = True Then dNumberResult = dNumber1 * dNumber2
If RadioButton4.Checked = True Then dNumberResult = dNumber1 / dNumber2
TextBox3.Text = CStr(dNumberResult)
In this chapter we are going to continue to explain what we did. This little piece of code declared three variables and assigned them values using some If…Then logic statements and operators along the way. In the last chapter we looked at variables, and using conversion functions. In this installment were going out take a quick look at the If Statement and other logic that allow us to make decisions in code and direct the program flow.
The If statement is the basis of all computing. The ability to have different outcomes based on conditions is what makes programs possible. We will look at the If statement and it’s fancier version, the Select Case statement.
The If Statement
In our code we use the if statement:
If RadioButton1.Checked = True Then dNumberResult = dNumber1 + dNumber2
Basically, we are telling the compiler that if the radio button (+) is checked then to add the two numbers.
The simplest if statement is simply:
If [conditions] then [result]
However, there are more variations.
If [result] is more than one statement, then you will need to use this format:
If [conditions] then
[statements to execute]
End if
And, finally, the if statement can be extended with the Else, and/or the Else If clauses:
If [conditions] then
[statements to execute]
Else [statements]
Else If [conditions] Then
[statements]
End if
Example:
Dim dNumber1 As Decimal
Dim bEven As Boolean
If (dNumber1 Mod 2 = 0) Then
bEven = True
Else
bEven = False
End If
In this example, we are evaluating whether or not the number is even or odd by using the modulus operator (MOD) to see if there is a remainder after we divide by two. If there is not one we set the Boolean variable bEven to true, else it is that the false
Select Case
The Select Case structure allows for more possibilities than the simpler if statements above. I think that’s easily seen in an example:
Select Case dNumber1
Case 1
TextBox3.Text = “You entered a 1”
Case 2
TextBox3.Text = “You entered a 2”
Case 3
TextBox3.Text = “You entered a 3”
End Select
In this example we are evaluating the value of dNumber1. Because dNumber1 can have many values this structure works much better. Rather than writing a different If or Else If statements for every possible value of dNumber1, this structure allows us to write statements for many different possible values of the expression to be of value added.
Using Goto
Next, we are going to look at the go to statement as a means of a redirection in code.
In our example, it is a problem if you try to divide by zero. One way to avoid this would be to exit the procedure before trying to divide by zero as in the following example. We name a place to “go to” by adding a colon (:) after the name. Then in the code we write:
Goto Place
Here is an example:
Dim Result As DialogResult
Select Case dNumber2
Case 0
Quit_Procedure
Case Else
If RadioButton4.Checked = True Then dNumberResult = dNumber1 / dNumber2
End Select
Quit_Procedure:
Result = MessageBox.Show(“You can’t divide by Zero”, “Math Error”, MessageBoxButtons.OK)
Exit Sub
Using Exit
Lastly we are going to look quickly at the exit statement. The exit statement is used in a sub procedure or a function to simply exit that part of the code without proceeding further. For example:
Dim Result As DialogResult
Select Case dNumber2
Case 0
Result = MessageBox.Show(“You can’t divide by Zero”, “Math Error”, MessageBoxButtons.OK)
Exit Sub
Case Else
If RadioButton4.Checked = True Then dNumberResult = dNumber1 / dNumber2
End Select
This would be another way to avoid dividing by zero in our program.
Using an Example in our Program
The last thing were going to do is add an if statement to our program to make sure that we don’t try to divide by zero.
Here is what it looks like. We are going to change this:
If RadioButton4.Checked = True Then dNumberResult = dNumber1 / dNumber2
to this:
If RadioButton4.Checked = True Then
If dNumber2 = 0 Then
MessageBox.Show(“You are attempting to divide by zero. Try another number.”, “Error”, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Exit SubElse
dNumberResult = dNumber1 / dNumber2End If
End If
Save all files, build the program, and run it. Try dividing by zero. You should get the error message you entered.
(c) copyright Mark W Smith, All rights reserved.