Visual Basic 2008 9.0 .NET Examples and Ebook

Introduction to Visual Basic

Vorig Onderwerp

New in Visual Basic 2008 - 9.0

|

Arrays

Volgend Onderwerp

Introduction to Iterations

Vorig Onderwerp

Introduction to Selections

|

Nested Structures

Volgend Onderwerp
Why use Iterations

Why use Iterations

Do While ... Loop Iteration

Do While ... Loop Iteration

Exercises

Exercises



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

Output :

 1
 2
 3

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

Output :

 1
 2
 3

Klik hier om terug naar boven te gaan.  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

Output :

 1
 2
 3

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

Klik hier om terug naar boven te gaan.  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

Solution :


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

Task :


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

Solution :


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.

Updated On : 2008-01-21

Download Broncode

Published On : 2008-06-24

Introduction to Iterations

Vorig Onderwerp

Introduction to Selections

|

Nested Structures

Volgend Onderwerp

Introduction to Visual Basic

Vorig Onderwerp

New in Visual Basic 2008 - 9.0

|

Arrays

Volgend Onderwerp
Nederlands  Nederlands

Add to favorites (IE).