|
|
 |
|
|
|
|
Visual Basic 2008 9.0 .NET Examples and Ebook
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| Suppose we need to bring to the console whether an entered value is "Zero.", "More than zero." or "Less than zero.".
We know that based on a condition of an If-statement we can let our algorithm decide between two options. What to do when the condition is true, and what to do when the condition is false.
But in this case, we have more than two options, we have three options. So we're going to need at least two conditions to make it possible for our algorithm to decide what option to take. |
| Module Example1
Sub Main()
Console.WriteLine("Value ?")
Dim value As Integer = Console.ReadLine()
If value = 0 Then
Console.WriteLine("Zero.")
Else
If value > 0 Then
Console.WriteLine("Above zero.")
Else
Console.WriteLine("Bellow zero.")
End If
End If
Console.ReadLine()
End Sub
End Module Download Broncode |
| Output : Value ?
<i>0</i>
Zero. |
| Output : Value ?
<i>5</i>
Above zero. |
| Output : Value ?
<i>-5</i>
Bellow zero. |
| The above example illustrates how a decision ( a selection ) can be selected.
Suppose we need to print out the multiplication tables of one to nine. |
| Output : 1 x 1 = 1
1 x 2 = 2
...
1 x 9 = 9
2 x 1 = 1
...
9 x 9 = 81 |
| Obviously iterations are needed. Almost identical lines follow each other.
What is being iterated here? Some base value ( starting with one and ending with nine ) is being multiplied with all factors from one to nice.
We can start with an iteration that prints out all numbers from one to nine. |
| Module Example2
Sub Main()
Dim baseValue As Integer = 1
Do While baseValue < 10
Console.WriteLine(baseValue)
baseValue = baseValue + 1
Loop
Console.ReadLine()
End Sub
End Module Download Broncode |
| Output : 1
2
3
4
5
6
7
8
9 |
| In stead of just printing out these numbers, each number should be multiplied with all factors from one to nine.
Lets write an algorithm to print out all multiplications ( with all factors ) of a certain basevalue. |
| Module Example3
Sub Main()
Dim baseValue As Integer = 5
Dim factor As Integer = 1
Dim multiplication As Integer
Do While factor < 10
multiplication = baseValue * factor
Console.WriteLine(baseValue & " x " & factor & " = " & _
multiplication)
factor = factor + 1
Loop
Console.ReadLine()
End Sub
End Module Download Broncode |
| Output : 5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45 |
| Now we only need to integrate both above algorithms. |
| Module Example4
Sub Main()
Dim baseValue As Integer = 1
Dim factor As Integer = 1
Dim multiplication As Integer
Do While baseValue < 10
Do While factor < 10
multiplication = baseValue * factor
Console.WriteLine(baseValue & " x " & factor & " = " & _
multiplication)
factor = factor + 1
Loop
baseValue = baseValue + 1
Loop
Console.ReadLine()
End Sub
End Module Download Broncode |
| Output : 1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
1 x 4 = 4
1 x 5 = 5
1 x 6 = 6
1 x 7 = 7
1 x 8 = 8
1 x 9 = 9 |
| In the above solution the iteration Do While factor < 10 ( which gives a table for a certain basevalue ) is repeated for each basevalue ( Do While baseValue < 10 ).
Notice that the output only contains the table for basevalue 1, and not for all following basevalues.
When we add the line factor = 1 at the end ( or beginning ) of iteration factor = 1 this problem is solved. |
| Module Example5
Sub Main()
Dim baseValue As Integer = 1
Dim factor As Integer = 1
Dim multiplication As Integer
Do While baseValue < 10
Do While factor < 10
multiplication = baseValue * factor
Console.WriteLine(baseValue & " x " & factor & " = " & _
multiplication)
factor = factor + 1
Loop
baseValue = baseValue + 1
factor = 1
Loop
Console.ReadLine()
End Sub
End Module Download Broncode |
| Output : 1 x 1 = 1
1 x 2 = 2
...
1 x 9 = 9
2 x 1 = 1
...
9 x 9 = 81 |
| As the above example illustrates an iteration can be iterated.
Iterations can be iterated, selections can be selected. And of course, iterations be selected or selections can be iterated.
Suppose we need to print out for all numbers from one to ten if they are even or odd. |
| Output : 1 is odd.
2 is even.
3 is odd.
4 is even.
5 is odd.
6 is even.
7 is odd.
8 is even.
9 is odd.
10 is even. |
| A decision have to be taken by our algorithm to selected whether to print out "even" or to print out "odd". This decision then has to iterated for all numbers starting with one up to nine.
It's easy to start with an algorithm that print out if a certain value is odd or even. |
| Module Example6
Sub Main()
Dim value As Integer = 5
If value Mod 2 = 0 Then
Console.WriteLine(value & " is even.")
Else
Console.WriteLine(value & " is odd.")
End If
Console.ReadLine()
End Sub
End Module Download Broncode |
| This decision has to be iterated for all numbers starting with one up to nine. |
| Module Example7
Sub Main()
Dim value As Integer
Do While value < 10
value = value + 1
If value Mod 2 = 0 Then
Console.WriteLine(value & " is an even number")
Else
Console.WriteLine(value & " is an odd number")
End If
Loop
Console.ReadLine()
End Sub
End Module Download Broncode |
| Output : 1 is odd.
2 is even.
3 is odd.
4 is even.
5 is odd.
6 is even.
7 is odd.
8 is even.
9 is odd.
10 is even. |
| As the above example illustrates a selection can be iterated.
By using sequentially defined instruction, iterations and decisions, the most complex algorithms can be formed. |
Exercises
| Task :
Make a program that repeatedly asks for a number. When a value less than zero is entered, the highest entered number is brought on the console. |
| Output : Value ?
<i>3</i>
Value ?
<i>8</i>
Value ?
<i>4</i>
Value ?
<i>-1</i>
Highest Value : 8 |
| Module Exercise1Solution
Sub Main()
Dim value As Integer
Dim highestValue As Integer
Do While value >= 0
Console.WriteLine("Value ?")
value = Console.ReadLine()
If value > highestValue Then
highestValue = value
End If
Loop
Console.WriteLine("Highest Value : " & highestValue)
Console.ReadLine()
End Sub
End Module Download Broncode |
| Make a program that prints out all numbers ( from small to big ) between two values entered by the user. |
| Output : Value ?
<i>-4</i>
Value ?
<i>3</i>
Row :
-3
-2
-1
0
1
2 |
| Output : Value ?
<i>2</i>
Value ?
<i>2</i>
Row : |
| Output : Value ?
<i>2</i>
Value ?
<i>-1</i>
Row :
0
1 |
| Output : Value ?
<i>2</i>
Value ?
<i>3</i>
Row : |
| Module Exercise2Solution
Sub Main()
Dim value1, value2 As Integer
Dim smallestValue, highestValue As Integer
Console.WriteLine("Value ?")
value1 = Console.ReadLine()
Console.WriteLine("Value ?")
value2 = Console.ReadLine()
If value1 > value2 Then
highestValue = value1
smallestValue = value2
Else
highestValue = value2
smallestValue = value1
End If
Console.WriteLine("Row :")
Do While smallestValue + 1 < highestValue
Console.WriteLine(smallestValue + 1)
smallestValue = smallestValue + 1
Loop
Console.ReadLine()
End Sub
End Module Download Broncode |
| Make a program that accepts a positive numeric value ( not zero ). When the value is even, the value is divided by 2 and printed out. When the value is odd, the value is multiplied by 3, added with 1 and printed out. Repeat this until the value is 1. |
| Output : Value ?
<i>10</i>
Row :
10
5
16
8
4
2
1 |
| Module Exercise3Solution
Sub Main()
Console.WriteLine("Value ?")
Dim value As Integer = Console.ReadLine()
Console.WriteLine("Row : ")
Do While value <> 1
Console.WriteLine(value)
If value Mod 2 = 0 Then
value = value / 2
Else
value = value * 3 + 1
End If
Loop
Console.WriteLine(value)
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.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|