Free computer Tutorials

home
Stay at Home and Learn

Conditional Operators

 
Computer Tutorials List

 

 

 

 

 

VB NET - Operators

 

The Conditional Operators allow you to refine what you are testing for. Instead of saying "If X is equal to Y", you can specify whether it's greater than, less than, and a whole lot more. Examine the list of Operators:

Operator Meaning  
>

This symbol means Is Greater Than and is used like this:

If number > 10 Then
MsgBox "The Number was Greater Than 10"
End If

 

<

This symbol means Is Less Than and is used like this:

If number <10 Then
MsgBox "The Number was Less Than 10"
End If

 

>=

These symbols mean Is Greater Than or Equal to, and are used like this:

If number >= 10 Then
MsgBox "The Number was 10 or Greater"
End If

 

<=

These symbols mean Is Less Than or Equal to, and are used like this:

If number <= 10 Then
MsgBox "The Number was 10 or Less"
End If

 

And

You can combine the logical operators with the word And. Like this:

If number > 5 And number < 15 Then
MsgBox "Greater than 5 And Less than 15"
End If

 

Or

You can combine the logical operators with the word Or. Like this:

If number > 5 Or number < 15 Then
MsgBox "Greater than 5 Or Less than 15"
End If

 

<>

These symbols mean Is Not Equal to, and are used like this:

If number1 <> number2 Then
MsgBox "number1 is not equal to number2"
End If

 

A word about And and Or. Notice the format with And and Or. The variable is repeated twice

If VariableName = 7 Or VariableName = 10 Then MsgBox "7 or 10 spotted"

If you just put something like this

If VariableName > 7 Or < 10 Then MsgBox "7 or 10 spotted"

then Visual Basic will give you an error message. What you have to say is

If [test the variable for this value] And [test the variable for that value] Then

Your If statement can contain an Else part, too. The format is this:

If [conditional logic here] Then
Some Code here
Else
Some Other Code here
End If

But don't worry if all that hasn't sunk in - you'll get used to the Conditional Operators as you go along. In the next part, there's two little programmes for you to write. They will test the skills you have acquired so far.

 

Click here to move on to the Section Three Exercises -->

Click here to jump to Loops in Visual Basic .NET-->

<--Back to the .NET Contents Page

View all our Home Study Computer Courses