Program execution control Statements
If...Then...Else Statements
An If...Then...Else statement is the basic conditional statement. If the expression in the If statement is True, the statements enclosed by the If block are executed. If the expression is False, each of the ElseIf expressions is evaluated.
Syntax :
If Expression [ Then ] StatementTerminator
[ Block ]
[ ElseIfStatement+ ]
[ Block ]
[ ElseStatement ]
[ Block ]
End If StatementTerminator
Example :
Module Module1
Sub Main()
DimiNumber AsInteger
iNumber = System.Console.ReadLine()
IfiNumber < 5 Then
System.Console.WriteLine("Number is Less than Five")
Else
System.Console.WriteLine("Number is Greater than Five")
EndIf
EndSub
EndModule
Select Case
A Select Case statement executes statements based on the value of an expression.
Syntax :
Select [ Case ] Expression StatementTerminator
[ CaseStatement+ ]
....
[ CaseElseStatement ]
End Select StatementTerminator
Example :
Module Module1
Sub Main()
Dim x As Integer = 10
SelectCase x
Case 5
Console.WriteLine("x = 5")
Case 10
Console.WriteLine("x = 10")
Case 20
Console.WriteLine("x = 20 – 10")
Case 30
Console.WriteLine("x = 30")
EndSelect
EndSub
EndModule
While Do While
A While or Do loop statement loops based on a Boolean expression. A While loop statement loops as long as the Boolean expression evaluates to True;
Syntax :
1) While Expression StatementTerminator
[ Block ]
End While StatementTerminator
2) Do [ WhileOrUntil Expression ] StatementTerminator [ Block ]
Loop [ WhileOrUntil Expression ] StatementTerminator
Example :
Module Module1
Sub Main()
Dim x As Integer
x = 3
DoWhile x > 0
Console.WriteLine("Do While x > 0")
x -= 1
Loop
x = 3
While x < 5
Console.WriteLine("While x < 5")
x += 1
EndWhile
Do
Console.WriteLine("While x = 2")
x -= 1
LoopWhile x > 2
EndSub
End Module
For Loop
For/Next loops enable you to evaluate a sequence of statements multiple times.
Syntax :
For [Expression]
[Block]
Next
Example :
Module Module1
Sub Main()
Dim x As Integer
For x = 1 To 10
System.Console.WriteLine("VB Dot Net in Easy Steps")
Next x
EndSub
End Module
For Each Loop
A For Each...Next statement loops based on the elements in an expression. A For Each statement specifies a loop control variable and an enumerator expression.
Syntax :
For Each LoopControlVariable In Expression StatementTerminator
[ Block ]
Next [Expression ] StatementTerminator
Example :
Module Module1
SubMain()
Dim x(9) As Integer
Dim i As Integer="2"> = 0
For i = 0 To 9
x(i) = i + 1
Next
ForEach y AsInteger In x
System.Console.WriteLine(y)
Next
EndSub
End Module