Programming, Investing and FI

Visual Basic Tutorial 6 – What IF?!

For the sixth tutorial I want to talk about the IF statement in Visual Basic.

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. Open up the project that we worked on during the exercise 1.

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.

What else can we do?

Exactly, there is another nice feature of the If statement! It’s the Else and ElseIf, 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.

Tutorial 6 MySecondValue is 6

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!

Next article Visual Basic Exercise 2 - If Keyword
Previous article Visual Basic Exercise 1 - Solution

Related posts