If....Else statement in vb.net

Conditional Statements

If....Else statement

If conditional expression is one of the most useful control structures which allows us to execute a expression if a condition is true and execute a different expression if it is False. The syntax looks like this:

If condition Then
[statements]
Else If condition Then
[statements]
-
-
Else
[statements]
End If

Understanding the Syntax

If the condition is true, the statements following the
Then keyword will be executed, else, the statements following the ElseIf will be checked and if true, will be executed, else, the statements in the else part will be executed.

Example

Imports System.Console
Module Module1

Sub Main()
Dim i As Integer
WriteLine("Enter an integer, 1 or 2 or 3")
i = Val(ReadLine())
'ReadLine() method is used to read from console

If i = 1 Then
WriteLine("One")
ElseIf i = 2 Then
WriteLine("Two")
ElseIf i = 3 Then
WriteLine("Three")
Else
WriteLine("Number not 1,2,3")
End If

End Sub

End Module

No comments: