Conditional Statements-IF..Else..End if

If statement will evaluate the condition. If it is true, it will execute the statement(s) that follows it. Otherwise, it will execute the statement(s) in else block.
if  condition then

                 Statement1

                 Statement2
end if       

If you use the above general form, The statements are executed when the condition is true. There is no statement to execute when the condition is false.

The below syntax will exceute the false block when the condition is not true.
 
if condition then
 
                 Statement1

                 statement2
                       
else
                 Statement3

                 statement4
end if       
       
Example:
Module Module1
 
    Sub Main()
        Dim x As Integer
        x = 10
        If (x Mod 2) = 0 Then
            Console.WriteLine("x is an even number")
        Else
            Console.WriteLine("x is an odd number")
        End If
        Console.ReadLine()
    End Sub
 
End Module

If have multiple conditions to check, you can use the below form of the If stament:

If condition1 then
 
                 Statement1

                 statement2

Else If condition2 then
 
                 Statement13

                 statement24


                       
Else
                 Statement15

                 statement6
End If       

Example:

Module Module1

Sub Main()
    Dim x As Integer
    x = Integer.Parse(Console.ReadLine())
    If x > 0 Then
         Console.WriteLine("It is positive.")

    Elseif x<0
        Console.WriteLine("It is negative.")
    Else
        Console.WriteLine("It is zeo.")
    End If
    Console.Read()
End Sub

End Module




HTML Comment Box is loading comments...



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.