VB.Net Tutorial/Statements/While — различия между версиями

Материал из VB Эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 15:53, 26 мая 2010

Combine While and If to search in an array

<source lang="vbnet">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</source>

Matched 0

Draw square of * by using nested while loop

<source lang="vbnet">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</source>

* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *

Using counter-controlled repetition

<source lang="vbnet">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</source>

Class average is 12

While structure

<source lang="vbnet">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</source>

2  4  8  16  32  64  128  256  512  Smallest power of 2 greater than 1000 is 1024