VB.NET Conditional Statements

VB.NET Conditional Statements ,If-statement,If-else Statement,If-statement Equality Operators
Share it:
Conditional Statements
google


Introduction


The software would not mean anything if you, as a programmer, could not decide what code should get executed given a certain condition. Like all other languages, VB.NET has the ubiquitous if-statement and select case statement.


If-statement


The If-statement lets you take a decision based on a given condition. The general syntax for an if-statement is as follows.


If condition Then
            'Conditional statement
        End If

The code below shows a very simple if-statement. It checks whether 1 is equal to 1 and if the result of this comparison is true, the string message is printed.


If 1 = 1 Then
            Console.WriteLine("1 and 1 are equal!")
        End If


If-else Statement  


As if-statement can have an else part that is executed if the condition of the if-statemen is false. Following is the general syntax of this.

If condition1 Then
            'Code
        If condition2 Then
         'Code
        Else
            'Code
        End If



Following is an example of an if-statement with a single condition and an else-part.


 If 3 > 4 Then
            Console.WriteLine("3 is greater than 4!")
        Else
            Console.WriteLine("3 is not greater than 4!")
        End If



The If-statement can be extended to handle multiple conditions that we are interested in. Have a look at the following code.


If 3 > 6 Then
            Console.WriteLine("3 is greater than 6!")
        ElseIf 3 > 5 Then
            Console.WriteLine("3 is greater than 5!")
        ElseIf 3 = 3 Then
            Console.WriteLine("3 is equal to 3!")
        Else
            Console.WriteLine("None of the above conditions was true!")
        End If
Nested If Statement You can nest if-statements for filtering as given an example of below.
 If 5 > 4 Then
            If 5 > 6 Then
                Console.WriteLine("5 is greater than 4 and 6!")
            Else
                Console.WriteLine("5 is greater than 4 but not 6!")
            End If
        End If

If-statement Equality Operators

The following equality operators can be used within if-statements in VB.NET.

= (checks whether two things are equal)
<> (checks whether two things are not equal)
> (checks whether the first thing is bigger than the second one)
< (checks whether the first thing is smaller than the second one)
>= (checks whether the first thing is bigger than or equal to the second one)
<= (checks whether the first thing is smaller than or equal to the second one)




Select Case match
            Case match1
                'Code
            Case match2
                'Code
            Case match3
                'Code
            Case Else 'All other conditions
                'Code
        End Select
] Let's have a look at a practical example.
Dim day As String = "Wednesday"
        
        Select Case day
            Case "Monday"
                Console.WriteLine("It's Monday today")
            Case "Wednesday"
                Console.WriteLine("It's Wednesday today")
            Case "Friday"
                Console.WriteLine("It's Friday today")
            Case Else
                Console.WriteLine("I don't know what day it is today!")
        End Select
The matching case in the above select-statement is Case "Wednesday". You can also put together two or more case-blocks for a single condition as shown below. If the day is Monday or Tuesday, the first case-block will be executed.
Dim day As String = "Wednesday"

        Select Case day
            Case "Monday"
            Case "Tuesday"
                Console.WriteLine("It's either Monday or Tuesday today")
            Case "Wednesday"
                Console.WriteLine("It's Wednesday today")
            Case "Friday"
                Console.WriteLine("It's Friday today")
            Case Else
                Console.WriteLine("I don't know what day it is today!")
        End Select
Share it:

VB.NET

VB.NET Conditional Statements

Post A Comment:

0 comments: