Control Statements
Control Statements
VB.NET
Introduction
• Control structures are statements or group of
statements in a programming language which
determine the sequence of execution of other
instructions or statements.
– The 3 main control structures used in programming are:
Selection/branching
Looping/iteration
Counting/sequence
End Sub
End Module
Example 2
Private Sub OK_Click(sender As Object, e As EventArgs) Handles OK.Click
Dim Mark As Integer
Dim Grade As String
Mark = TxtMark.Text
If Mark >= 80 And Mark <= 100 Then
Grade = "A"
Else
Grade = "Out of Range"
End If
MsgBox("You Grade is " & Grade)
End Sub
• NESTED IF/THEN......ELSE/END IF STATEMENT
If( boolean_expression 1)Then
'Executes when the boolean expression 1 is true
If(boolean_expression 2)Then
'Executes when the boolean expression 2 is true
End If
End If
Example:
Module decisions
Sub Main()
'local variable definition
Dim a As Integer = 100
Dim b As Integer = 200
' check the boolean condition
If (a = 100) Then
' if condition is true then check the following
If (b = 200) Then
' if condition is true then print the following
Console.WriteLine("Value of a is 100 and b is 200")
End If
End If
Console.WriteLine("Exact value of a is : {0}", a)
Console.WriteLine("Exact value of b is : {0}", b)
Console.ReadLine()
End Sub
End Module
SELECT....CASE STATEMENT
The Select Case statement executes one of several groups of statements depending on the value of an
expression
Syntax
Select [ Case ] expression
[ Case expressionlist
[ statements ] ]
[ Case Else
[ elsestatements ] ]
End Select
Example
Module Module1
Sub Main()
Dim keyIn As Integer
WriteLine("Enter a number between 1 and 4")
keyIn = Val(ReadLine())
Select Case keyIn
Case 1
WriteLine("You entered 1")
Case 2
WriteLine("You entered 2")
Case 3
WriteLine("You entered 3")
Case 4
WriteLine("You entered 4")
End Select
End Sub
End Module
VB.NET Loops
A loop statement allows us to run a statement or group of
statements many times.
Following are the types of looping statements available in VB.Net
Example
Module Module1
Sub Main()
Dim x As Integer
While (x < 5)
Console.WriteLine(x)
x=x+1
Console.ReadLine()
End While
End Sub
End Module
For Each...Next
For loops enable us to execute a series of expressions multiple numbers of
times
Syntax
For index=start to end [Step step]
[statements]
[Exit For]
[statements]
Next[index]
Example
Module Module1
Sub Main()
For count As Integer = 1 To 10 Step 2
Console.WriteLine(count)
Next
End Sub
End Module
SELECTED EXAMPLES:
A program required to calculate the sum of the first 5
positive integers.
(i) Using the Do while Loop Using the Do..Until loop.