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

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

Версия 16:40, 26 мая 2010

Calculated step

public class Test
   public Shared Sub Main
        For x As Single = 1 To 2 Step 1 / 7
            Console.WriteLine(x)
        Next x
   End Sub
End class
1
1.142857
1.285714
1.428572
1.571429
1.714286
1.857143

Calculating compound interest.

Module Tester
   Sub Main()
      Dim amount, principal As Decimal 
      Dim rate As Double               
      Dim year As Integer              
      Dim output As String             
      principal = 1000
      rate = 0.05
      output = "Year" & vbTab & "Amount on deposit" & vbCrLf
      For year = 1 To 10
         amount = principal * (1 + rate) ^ year
         output &= year & vbTab & _
            String.Format("{0:C}", amount) & vbCrLf
      Next
      Console.WriteLine(output)
   End Sub  
End Module
Year    Amount on deposit
1       $1,050.00
2       $1,102.50
3       $1,157.63
4       $1,215.51
5       $1,276.28
6       $1,340.10
7       $1,407.10
8       $1,477.46
9       $1,551.33
10      $1,628.89

Float step

public class Test
   public Shared Sub Main
        For x As Single = 1 To 3 Step 0.5
            Console.WriteLine(x.ToString("0.0"))
        Next x
   End Sub
End class
1.0
1.5
2.0
2.5
3.0

For loop with float point control number

Option Strict On
 Imports System
 Module Module1
    Sub Main( )
       Dim loopCounter As Single
       For loopCounter = 0.5 To 9
          Console.WriteLine("loopCounter: {0}", loopCounter)
       Next
    End Sub "Main
 End Module
loopCounter: 0.5
loopCounter: 1.5
loopCounter: 2.5
loopCounter: 3.5
loopCounter: 4.5
loopCounter: 5.5
loopCounter: 6.5
loopCounter: 7.5
loopCounter: 8.5

For To Next Loop

Option Strict On
 Imports System
 Module Module1
    Sub Main( )
       Dim loopCounter As Integer
       For loopCounter = 0 To 9
          Console.WriteLine("loopCounter: {0}", loopCounter)
       Next
    End Sub
 End Module
loopCounter: 0
loopCounter: 1
loopCounter: 2
loopCounter: 3
loopCounter: 4
loopCounter: 5
loopCounter: 6
loopCounter: 7
loopCounter: 8
loopCounter: 9

Nested For Loop

Option Strict On
 Imports System
 Module Module1
    Sub Main( )
       Dim outer As Integer
       Dim inner As Integer
       For outer = 3 To 6
          For inner = 10 To 12
             Console.WriteLine("{0} * {1} = {2}", _
                 outer, inner, outer * inner)
       Next inner, outer
    End Sub "Main
 End Module
3 * 10 = 30
3 * 11 = 33
3 * 12 = 36
4 * 10 = 40
4 * 11 = 44
4 * 12 = 48
5 * 10 = 50
5 * 11 = 55
5 * 12 = 60
6 * 10 = 60
6 * 11 = 66
6 * 12 = 72

Set For Loop float Step

Option Strict On
 Imports System
 Module Module1
    Sub Main( )
       Dim loopCounter As Single
       For loopCounter = 0.5 To 9 Step 0.5
          Console.WriteLine("loopCounter: {0}", loopCounter)
       Next
    End Sub "Main
 End Module
loopCounter: 0.5
loopCounter: 1
loopCounter: 1.5
loopCounter: 2
loopCounter: 2.5
loopCounter: 3
loopCounter: 3.5
loopCounter: 4
loopCounter: 4.5
loopCounter: 5
loopCounter: 5.5
loopCounter: 6
loopCounter: 6.5
loopCounter: 7
loopCounter: 7.5
loopCounter: 8
loopCounter: 8.5
loopCounter: 9

Using For/Next structure to demonstrate summation.

Imports System.Windows.Forms
Module Tester
    Sub Main()
        Dim sum = 0, number As Integer
        " add even numbers from 2 to 100
        For number = 2 To 100 Step 2
         sum += number
        Next
      Console.WriteLine("The sum is " & sum & "Sum even integers from 2 to 100")
    End Sub 
End Module
The sum is 2550Sum even integers from 2 to 100