Examples
|
While |
While |
Do While Loop |
Do Until Loop |
If ElseIf End If |
Select Case |
Using While to Find the sum of integers
' Calculates the sum of the integers from 1 to 10.
Module Calculate
Sub Main()
Dim sum As Integer = 0
Dim number As Integer = 1
While number <= 10
sum += number
number += 1
End While
Console.WriteLine("The sum is: " &
sum)
End Sub
End Module

Using While to Find Powers of 3
' Demonstration of While statement.
Module PowersOfThree
Sub Main()
Dim product As Integer = 3
' statement
multiplies and displays product while product is less than or equal to 100
While product <= 100
Console.Write(product & " ")
product =
product * 3 ' compute next power of 3
End While
Console.WriteLine()
' write blank line
' print result
Console.WriteLine("First power
of 3 " & "larger than 100 is " & product)
End Sub
End Module ' PowersOfThree

Do While ... Loop Statement to Find Powers of 3
' Demonstration of the Do While...Loop statement.
Module DoWhile
Sub Main()
Dim product As Integer = 3
' initialize product
' statement
multiplies and displays the product while it is less than or equal to 100
Do While product <= 100
Console.Write(product & " ")
product =
product * 3
Loop
Console.WriteLine()
' write blank line
' print result
Console.WriteLine("First power of 3 "
& "larger than 100 is " & product)
End Sub
End Module

Do Until...Loop in Finding Powers of 3
'
Demonstration of the Do Until...Loop statement.
Module DoUntil
Sub Main()
Dim product As Integer = 3
' find first
power of 3 larger than 100
Do Until product > 100
Console.Write(product & " ")
product =
product * 3
Loop
Console.WriteLine()
' write blank line
' print result
Console.WriteLine("First power of 3 "
& "larger than 100 is " & product)
End Sub
End Module
Example 5
If Structures
Module Module1
Sub Main()
Dim Grade As Integer
Console.WriteLine("Please enter the student's grade")
Grade = Console.ReadLine()
If Grade >= 90 Then
Console.WriteLine("A")
ElseIf Grade >= 80 Then
Console.WriteLine("B")
ElseIf Grade >= 70 Then
Console.WriteLine("C")
ElseIf Grade >= 60 Then
Console.WriteLine("D")
Else
Console.WriteLine("F")
End If
End Sub
End Module

Example 6
Select Case
Module Module1
Sub Main()
Console.WriteLine("Please enter a grade")
Dim Grade As Integer = Console.ReadLine()
Select Case Grade
Case 90 To 100
Console.WriteLine("A")
Case 80 To 89
Console.WriteLine("B")
Case 70 To 79
Console.WriteLine("C")
Case 60 To 69
Console.WriteLine("D")
Case Else
Console.WriteLine("F")
End Select
End Sub
End Module
