Combine While and If to search in an array
Imports System
Public Class WhileExample
Shared Sub Main()
Dim iCounter As Integer = 0
Dim arrList(9) As String
Dim iMatch As Integer = -1
Dim sMatch As String
sMatch = "A"
arrList(0) = "A"
arrList(1) = "B"
arrList(2) = "C"
arrList(3) = "D"
arrList(4) = "E"
arrList(5) = "G"
arrList(6) = "H"
arrList(7) = "I"
arrList(8) = "J"
arrList(9) = "K"
While iCounter <= 9 AND iMatch = -1
If arrList(iCounter) Like sMatch Then
iMatch = iCounter
Else
iCounter = iCounter + 1
End If
End While
If iMatch <> -1 Then
System.Console.WriteLine("Matched " & iMatch)
End If
End Sub
End Class
Matched 0
Draw square of * by using nested while loop
Module Tester
Sub Main()
Dim side As Integer
Dim row As Integer = 1
Dim column As Integer
side = 12
If side <= 20 Then
While row <= side
column = 1
While column <= side
Console.Write("* ")
column += 1
End While
Console.WriteLine()
row += 1
End While
Else
Console.WriteLine("Side too large")
End If
End Sub
End Module
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
Using counter-controlled repetition
Module modAverage
Sub Main()
Dim total As Integer
Dim gradeCounter As Integer
Dim grade As Integer
Dim average As Double
total = 0
gradeCounter = 1
While gradeCounter <= 10
grade = 12
total += grade
gradeCounter += 1
End While
average = total / 10
Console.WriteLine("Class average is {0}", average)
End Sub
End Module
Class average is 12
While structure
Module Tester
Sub Main()
Dim product As Integer = 2
While product <= 1000
Console.Write("{0} ", product)
product = product * 2
End While
Console.WriteLine("Smallest power of 2 " & _
"greater than 1000 is {0}", product)
End Sub " Main
End Module
2 4 8 16 32 64 128 256 512 Smallest power of 2 greater than 1000 is 1024