' Visual Basic 2008 9.0 .NET Examples - Iterations - Introduction to Visual Basic : Module Example1 Sub Main() Dim value As Integer ' Do While value < 10 value = value + 1 Console.WriteLine(value) Loop ' Console.ReadLine() End Sub End Module Module Example2 Sub Main() Dim value As Integer ' Do Until value >= 10 value = value + 1 Console.WriteLine(value) Loop ' Console.ReadLine() End Sub End Module Module Example3 Sub Main() Dim value As Integer ' Do value = value + 1 Console.WriteLine(value) Loop While value < 10 ' Console.ReadLine() End Sub End Module Module Example4 Sub Main() Dim value As Integer ' Do value = value + 1 Console.WriteLine(value) Loop Until value >= 10 ' Console.ReadLine() End Sub End Module Module Example5 Sub Main() Console.WriteLine("Highest Value ?") Dim highest As Integer = Console.ReadLine() ' Console.WriteLine("Row :") Dim value As Integer Do value = value + 1 Console.WriteLine(value) Loop Until value >= highest ' Console.ReadLine() End Sub End Module Module Example6 Sub Main() Console.WriteLine("Highest Value ?") Dim highest As Integer = Console.ReadLine() ' Console.WriteLine("Row :") Dim value As Integer Do Until value >= highest value = value + 1 Console.WriteLine(value) Loop ' Console.ReadLine() End Sub End Module Module Example7 Sub Main() Dim value As Integer ' For value = 1 To 10 Console.WriteLine(value) Next ' Console.ReadLine() End Sub End Module Module Example8 Sub Main() Dim value As Integer ' For value = 10 To 15 Console.WriteLine(value) Next ' Console.ReadLine() End Sub End Module Module Example9 Sub Main() Dim value As Integer ' For value = 2 To 10 Step 2 Console.WriteLine(value) Next ' Console.ReadLine() End Sub End Module Module Example10 Sub Main() Dim value As Integer ' For value = 10 To 0 Step -2 Console.WriteLine(value) Next ' Console.ReadLine() End Sub End Module Module Example11 Sub Main() Dim countValue As Integer Dim startValue As Integer = 1 Dim endValue As Integer = 10 Dim stepValue As Integer = 2 ' Console.WriteLine("values during iteration :") For countValue = startValue To endValue Step stepValue Console.WriteLine("count-value : " & countValue) ' countValue = countValue + 1 startValue = startValue + 1 endValue = endValue + 1 stepValue = stepValue + 1 Next ' Console.WriteLine("values after iteration :") Console.WriteLine("count-value : " & countValue) Console.WriteLine("start-value : " & startValue) Console.WriteLine("end-value : " & endValue) Console.WriteLine("step-value : " & stepValue) ' Console.ReadLine() End Sub End Module Module Exercise1Solution Sub Main() Console.WriteLine("Value ?") Dim value As Integer = Console.ReadLine() ' Dim factorial As Integer = value Dim factor As Integer = value - 1 ' For factor = value - 1 To 2 Step -1 factorial = factorial * factor Next ' Console.WriteLine(value & "! = " & factorial) ' Console.ReadLine() End Sub End Module Module Exercise2Solution Sub Main() Dim hours, minutes, seconds As Integer Dim timeLabel As String For hours = 0 To 23 For minutes = 0 To 59 For seconds = 0 To 59 timeLabel = "" If hours < 10 Then timeLabel = "0" End If timeLabel = timeLabel & hours & "h" If minutes < 10 Then timeLabel = timeLabel & "0" End If timeLabel = timeLabel & minutes & "m" If seconds < 10 Then timeLabel = timeLabel & "0" End If timeLabel = timeLabel & seconds & "s" Console.WriteLine(timeLabel) Next Next Next Console.ReadLine() End Sub End Module Module Exercise3Solution Sub Main() Dim totalSeconds, remainingSeconds As Integer Dim days, hours, minutes, seconds As Integer ' Do Console.WriteLine("Seconds ?") totalSeconds = Console.ReadLine() If totalSeconds = 0 Then Console.WriteLine("End.") Else If totalSeconds < 0 Then Console.WriteLine("Error : " & _ "Only positive values are accepted !") Else remainingSeconds = totalSeconds days = remainingSeconds \ 86400 remainingSeconds = remainingSeconds - days * 86400 hours = remainingSeconds \ 3600 remainingSeconds = remainingSeconds - hours * 3600 minutes = remainingSeconds \ 60 seconds = remainingSeconds - minutes * 60 Console.WriteLine("Result :") If days > 0 Then Console.WriteLine(days & " days") End If If hours > 0 Then Console.WriteLine(hours & " hours") End If If minutes > 0 Then Console.WriteLine(minutes & " minutes") End If If seconds > 0 Then Console.WriteLine(seconds & " seconds") End If End If End If Loop Until totalSeconds = 0 ' Console.ReadLine() End Sub End Module Module Exercise4Solution Sub Main() Dim number, sum As Integer ' Do number = Console.ReadLine() If number = 0 Then Console.WriteLine("=") Console.WriteLine(sum) Else sum = sum + number Console.WriteLine("+") End If Loop Until number = 0 ' Console.ReadLine() End Sub End Module Module Exercise5Solution Sub Main() Dim number, result As Integer Dim operatorSymbol As String ' number = Console.ReadLine() ' result = number Do operatorSymbol = Console.ReadLine() If operatorSymbol = "=" Then Console.WriteLine(result) Else number = Console.ReadLine() If operatorSymbol = "+" Then result = result + number Else If operatorSymbol = "-" Then result = result - number End If End If End If Loop Until operatorSymbol = "=" ' Console.ReadLine() End Sub End Module ' Visit www.studyvb.com for more examples. Copyright 2003-2008 De Wolf.