|
|
 |
|
|
|
|
Visual Basic 2008 9.0 .NET Examples and Ebook
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Why use Iterations
| Following example gives us all numbers from one to three. |
| Module Example1
Sub Main()
Console.WriteLine(1)
Console.WriteLine(2)
Console.WriteLine(3)
Console.ReadLine()
End Sub
End Module Download Broncode |
| The above example is very statically defined. Suppose we need all numbers from one to hundred, then 97 instructors need to be added.
A more dynamic solution can be defined when using the iteration technique. |
| Module Example2
Sub Main()
Dim value As Integer
value = value + 1
Console.WriteLine(value)
value = value + 1
Console.WriteLine(value)
value = value + 1
Console.WriteLine(value)
Console.ReadLine()
End Sub
End Module Download Broncode |
Up
Do While ... Loop Iteration
| As you can see in the above solution the same instructions are repeated three times.
In stead of repeating the same instructions, we can code them between a Do While and Loop of an iteration.
Do While is followed by a condition, expression how long the instructions ( between Do While and Loop ) should be repeated. As long as the condition evaluates as true the instructions are repeated. From the moment the condition evaluates as false the iteration is ended, and the instructions following the iteration are executed. |
| Module Example3
Sub Main()
Dim value As Integer
Do While value < 3
value = value + 1
Console.WriteLine(value)
Loop
Console.ReadLine()
End Sub
End Module Download Broncode |
| The algorithm can be coded even more dynamical by adding a start- and endvalue. |
| Module Example4
Sub Main()
Dim startValue As Integer = 10
Dim endValue As Integer = 15
Dim value As Integer = startValue - 1
Do While value < endValue
value = value + 1
Console.WriteLine(value)
Loop
Console.ReadLine()
End Sub
End Module Download Broncode |
| Output : 10
11
12
13
14
15 |
Up
Exercises
| Task :
Make a program that brings all numbers from 20 to 10 on the console. |
| Output : 20
19
18
17
16
15
14
13
12
11
10 |
| Module Exercise1Solution
Sub Main()
Dim value As Integer = 21
Do While value > 10
value = value - 1
Console.WriteLine(value)
Loop
Console.ReadLine()
End Sub
End Module Download Broncode |
| Make a program that brings all powers of 2 smaller ( starting with exponent 1 ) than 1000 on the console. |
| Output : base : 2, exponent : 1, result : 2
base : 2, exponent : 2, result : 4
base : 2, exponent : 3, result : 8
base : 2, exponent : 4, result : 16
base : 2, exponent : 5, result : 32
base : 2, exponent : 6, result : 64
base : 2, exponent : 7, result : 128
base : 2, exponent : 8, result : 256
base : 2, exponent : 9, result : 512 |
| Module Exercise2Solution
Sub Main()
Dim base As Integer = 2
Dim exponent As Integer = 1
Dim result As Integer = base ^ exponent
Do While result < 1000
Console.WriteLine("base : " & base & _
", exponent : " & exponent & _
", result : " & result)
exponent = exponent + 1
result = base ^ exponent
Loop
Console.ReadLine()
End Sub
End Module Download Broncode |
This version ( published on 2008-06-24 ) is printed from http://www.studyvb.com, visit the website for more recent information.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|