Interesting fact on Visual Basic before we begin: this programming language is aimed at simplifying the way we write code in a way that someone else who doesn’t know what you code does can read it like a normal sentence. What I mean by that is that Visual Basic will use less symbols like { or } or ; or -> or && and so on. Another nice feature of this language which makes it easier to learn.
If Keyword Usage
Let’s jump right into the code, what is the goal of an if statement? We want the computer to select between one output or the other depending on one or multiple factors. Create a new project and you can copy and paste the following. Remember to rename your starting module to Program.vb.
Option Explicit On Option Strict On Module Program Sub Main() 'Declaration Dim MyVariable As String Dim MySecondVariable As Integer 'Initialization MyVariable = "Test" MySecondVariable = 6 Console.WriteLine(MyVariable) Console.ReadKey() End Sub End Module
We are going to modify it so the computer can choose between one output or the other depending on the value of MySecondVariable.
Here is the template of an If statement in Visual Basic
If Some condition Then What to do if that condition is true End If
Now lets apply this template to our code, what is our goal here? If the value of MySecondVariable is equal or higher than 6, then display the message MySecondVariable is higher or equal to 6! in the console. Please read the previous sentence twice and then read the applied code below, you will see that it reads fairly similar (referencing what I told you earlier).
Option Explicit On Option Strict On Module Program Sub Main() 'Declaration Dim MyVariable As String Dim MySecondVariable As Integer 'Initialization MyVariable = "Test" MySecondVariable = 6 'Condition check if MySecondVariable is higher or equal to 6 If MySecondVariable >= 6 Then Console.WriteLine("MySecondVariable is higher or equal to 6!") End If Console.WriteLine(MyVariable) Console.ReadKey() End Sub End Module
We can start by reviewing the first line of the if statement, IF is the keyword and the condition which is MySecondVariable >= 6 (there are many mathematical operators in Visual Basic, again, Microsoft has a really good Webpage on them). Finally, Then which announces the start of what we want to do if the condition passes. In this case, we want to write a line in the console. The last line is for telling Visual Basic that this is the end of the If statement: End If.
What Else Can We Do?
Exactly, there is another nice feature of the If statement! It’s the Else If and the Else, we can say: If a condition then something happens Else If some other condition Else a final condition End If.
Here is what it looks like in code.
Option Explicit On Option Strict On Module Program Sub Main() 'Declaration Dim MyVariable As String Dim MySecondVariable As Integer 'Initialization MyVariable = "Test" MySecondVariable = 6 'MySecondVariable condition check If MySecondVariable > 6 Then Console.WriteLine("MySecondVariable is higher than 6!") ElseIf MySecondVariable < 6 Then Console.WriteLine("MySecondVariable is strictly lower than 6!") Else Console.WriteLine("MySecondVariable is 6!") End If Console.WriteLine(MyVariable) Console.ReadKey() End Sub End Module
First condition here: If MySecondVariable is greater than 6 then write MySecondVariable is higher than 6!.
Else condition: If MySecondVariable is lower than 6 then write MySecondVariable is strictly lower than 6!.
Finally, if all else fails, write MySecondVariable is 6!.
End of If statement.
Now you should run the code and play with the value of MySecondVariable, change it to 8, then 5 and back to 6 to see the different results.
You now have the basic understanding of the If statement, what I suggest is that you try to combine multiple conditions by joining them with the And or the Or keywords. I will give you a hint:
If MySecondVariable > 6 Or MySecondVariable < 6 Then Console.WriteLine("MySecondVariable is not 6!") Else Console.WriteLine("MySecondVariable is 6 then!") End If
Remember that the If statement is really important in most languages, if you don’t understand please feel free to ask a question!
No Comments Yet!
You can be first to comment this post!