There are times when you want the program to respond differently to different events. Say you wanted to write some code that checks to see that the user entered a number. To do this we would use an if statement. The format for an if statement in Visual BASIC is if condition is true then...do something. The condition would use one of these operators:

<less than
>greater than
==equal to
<=less than or equal to
>=greater than or equal to
<>not equal to
<less than

To check to see if what the user entered is numeric, we use the builtin function IsNumeric. To do this we put IsNumeric then parenthases and then what we want to check. IsNumeric returns a Boolean variable, which means that the answer will either be True or False, which we need to know for the if statement. Let's use the program from the last section and check and see if the numbers entered is numeric.

Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim decNum1 As Decimal
Dim decNum2 As Decimal
If IsNumeric(txtNum1.Text) = True Then decNum1 = txtNum1.text
If IsNumeric(txtNum2.Text) = True Then decNum1 = txtNum2.text
lblAnswer.Text &= txtNum1.Text + txtNum2.Text
End Sub

If you want to do more than one line of code then you will need to come to the next line after the Then (hitting enter) and type all the code there. When you are done, you'd put an End If.

Else Statements

Else statements are used when you want want something to happen if the comparison is false. The else statement comes in handy when say you want your program to do something if your name is Jimmy but do something completely different if your name was Sandra. You can never have an else statement by itself, it always needs to accompany an if statement. Else would come after the last line of code to execute if the condition is true, and before the End If. Let's check and see if a number is odd or even. To check if the number entered is an even number, simply mod it by 2. If the result is 0 then the number is even, otherwise it is odd. This code would go inside of a Button, and will work assuming you have the other components named the same thing:

Dim intNum as Integer
intNum = txtNum.Text If (intNum mod 2) = 0 Then
lblMessage.Text = "The number is even"
lblMessage.Text = "The number is odd"
End If

The reason we mod it by 2 is because only even numbers can be divided evenly by 2, so if a number divided by 2 has no remainder, we can conclude that it is even.

Remember the first program in this section, where we had two if statements one for each TextBox, well we can combine them. to do this, we would use one If statement, a condition, a conjunction, and another condition. The conjuctions are:

AndBoth conditions must be true, both are tested, even if condition 1 is false
OrOne conditions must be true, both are tested even if condition 1 is true
AndAlsoBoth conditions must be true, if condition 1 is false, condition 2 won't be tested
OrElseOne condition must be true, if condition 1 is true, condition 2 won't be tested
XorOnly one condition can be true, if both are true it will evaluate to false

So to rewrite the code, we would do:

Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim decNum1 As Decimal
Dim decNum2 As Decimal
If IsNumeric(txtNum1.Text) = True AndAlso IsNumeric(txtnum2.Text) = True Then
lblAnswer.Text = txtNum1.Text + txtnum2.Text
lblAnswer.Text = "Please enter number"
End If
End Sub

Now an else statement can have as many lines of code as you want, you can even put more if statements in there. Say for example you wanted to turn a number grade into a letter grade, to do this we would need a bunch of ElseIf statements.

Dim intGPA As Integer
intGPA = 40
If intGPA >= 90 AndAlso intGPA <= 100 Then
lblMessage.Text = "You'are on the high honor roll"
ElseIf intGPA <= 89 AndAlso intGPA >=80 Then
lblMessage.Text ="You are on the honor roll this semester"
ElseIf intGPA<=79 AndAlso intGPA >=70 Then
lblMessage.Text ="You passed the course"
ElseIf intGPA <= 69 AndAlso intGPA >=0 Then
lblMessage.Text = "You failed the course "
lblMessage.Text = "There's an error with your grade"
End If

Case Select

ElseIf statements can get pretty messy. Just look at the previous code, for some it might be diffictule to decifer. If you have too many choices then you may want to use Select Case. Personally, I use Select Case if an If statement has three or more possible outcomes. To use Select Case, you would have an object's property or variable as the focus and then each case is a condition.

Dim charGPA As Char
charGPA = "A"
Select Case charGrade
Case "A"
lblMessage.Text="You got an A";
Case "B"
lblMessage.Text="You got a B";
Case "C"
lblMessage.Text="You got a C";
Case "D"
lblMessage.Text="You got a D";
Case "F"
lblMessage.Text="You got an F";
Case Else
lblMessage.Text="There's an error with your grade.";
End Select

The Select Case can be used with any variable type. Unlike other languages, the code ends when it hits the next Case statement. After it executes the code, it will go to the End Select and continue with the rest of the program. Using conditions, whether it be an If statement or Select Case is essential to everyday programs.

Next Section: Coming Soon

Link Directory | link directory | GeekySpeaky: Submit Your Site! | Blog Matter

Copyright © 2004-2018 Sean Noble, All rights reserved
Read our Disclosure Policy