|
|
 |
|
|
|
|
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.
Two nested Ifs can be used to achieve this. |
| 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("Positive value.")
Else
Console.WriteLine("Negative value.")
End If
End If
Console.ReadLine()
End Sub
End Module Download Broncode |
| Output : Value ?
<i>0</i>
Zero. |
| Output : Value ?
<i>5</i>
Positive value. |
| Output : Value ?
<i>-5</i>
Negative value. |
| We also could place 3 selections ( Ifs ) in a sequential order. |
| Module Example2
Sub Main()
Console.WriteLine("Value ?")
Dim value As Integer = Console.ReadLine()
If value = 0 Then Console.WriteLine("Zero.")
If value > 0 Then Console.WriteLine("Positive value.")
If value < 0 Then Console.WriteLine("Negative value.")
Console.ReadLine()
End Sub
End Module Download Broncode |
| Output : Value ?
<i>0</i>
Zero. |
| Output : Value ?
<i>5</i>
Positive value. |
| Output : Value ?
<i>-5</i>
Negative value. |
| As you can see a complete If statement can be written on 1 coding line. This is only possible when only 1 instruction needs to be executed when the condition is correct.
Although resulting in the same output, the above example is less efficient than the first example. This first example will in worst case scenario ( when the value is not equal to zero ) evaluate 2 conditions ( value = 0 which results in False and value > 0 ), in best case scenario only 1 condition needs to be evaluated ( value = 0 ). The above example will in every scenario ( best, worst or something in between ) evaluate all 3 conditions.
Another variation can be build using ElseIf. |
| Module Example3
Sub Main()
Console.WriteLine("Value ?")
Dim value As Integer = Console.ReadLine()
If value = 0 Then
Console.WriteLine("Zero.")
ElseIf value > 0 Then
Console.WriteLine("Positive value.")
Else
Console.WriteLine("Negative value.")
End If
Console.ReadLine()
End Sub
End Module Download Broncode |
| Output : Value ?
<i>0</i>
Zero. |
| Output : Value ?
<i>5</i>
Positive value. |
| Output : Value ?
<i>-5</i>
Negative value. |
| Again this results in the same output.
Performance is equal to the first example. In best case scenario only 1 condition is evaluated, in worst case 2 conditions are evaluated.
The ElseIf part is optional, but can be used more than once.
The instruction in the Else will only be execution when all the above conditions fail.
The syntax of an If statement looks like this : |
| If <condition> Then
...
[ ElseIf <other-condition> Then
... ] *
[ Else
... ]
End If
|
| Although the first and the third examples are equally efficient, one could prefer the first construction. Suppose we also need to print out the double of the entered value when the value is not equal to zero. We could achieve this by adding one line of code to the Else part of the first selection. |
| Module Example4
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("Positive value.")
Else
Console.WriteLine("Negative value.")
End If
Console.WriteLine("Double : " & (value * 2))
End If
Console.ReadLine()
End Sub
End Module Download Broncode |
| Output : Value ?
<i>0</i>
Zero. |
| Output : Value ?
<i>5</i>
Positive value.
Double : 10 |
| Output : Value ?
<i>-5</i>
Negative value.
Double : -10 |
| To achieve the same result when adapting the third example, we need to add 2 identical lines of code, 1 to the If and 1 to the Else part of the second selection. The more you can avoid writing identical line of code, the better. |
Select Case ... End Select
| Suppose we need to bring some information ( "One.", "Two.", "Three" of "Not one, two or three." ) about an entered value to the console. We have 4 each other excluding option, so 3 nested selections would be sufficient.
To avoid nesting lot of selections, one could use a Select Case ... End Select. This often leads up to more elegant code. |
| Module Example5
Sub Main()
Console.WriteLine("Value ?")
Dim value As Integer = Console.ReadLine()
Select Case value
Case 1
Console.WriteLine("One.")
Case 2
Console.WriteLine("Two.")
Case 3
Console.WriteLine("Three.")
Case Else
Console.WriteLine("Not one, two or three.")
End Select
Console.ReadLine()
End Sub
End Module Download Broncode |
| Output : Value ?
<i>1</i>
One. |
| Output : Value ?
<i>2</i>
Two. |
| Output : Value ?
<i>3</i>
Three. |
| Output : Value ?
<i>4</i>
Not one, two or three. |
| The keywords Select Case are followed by an expression that will be evaluated. Between Select Case and End Select all relevant cases ( and the corresponding instructions ) are defined.
In this situation we have 4 cases, case value equals 1, case value equals 2, case value equals 3 and the case where value doesn't equals 1 or 2 or 3.
Only the instructions of the first case - that's valid - will be executed.
The optional 'Case Else' contains the instructions that will be executed only when all the above case aren't valid.
In best case scenario ( value is equal to 1 ) only 1 condition will be evaluated, in worst case scenario ( value is not equal to 1, 2 or 3 ) 3 conditions will be evaluated. So looking at the number of conditions that will be evaluated, there is no performance overhead for using a Select Case ... End Select.
Suppose we need to bring the information "One.", "Two, three or four.", _ "Value from 5 to 10." or "More than 10." to the console. |
| Module Example6
Sub Main()
Console.WriteLine("Value ?")
Dim value As Integer = Console.ReadLine()
Select Case value
Case 1
Console.WriteLine("One.")
Case 2, 3, 4
Console.WriteLine("Two, three or four.")
Case 5 To 10
Console.WriteLine("Value from 5 to 10.")
Case Is > 10
Console.WriteLine("More than 10.")
End Select
Console.ReadLine()
End Sub
End Module Download Broncode |
| Output : Value ?
<i>1</i>
One. |
| Output : Value ?
<i>3</i>
Two, three or four. |
| Output : Value ?
<i>8</i>
Value from 5 to 10. |
| Output : Value ?
<i>12</i>
More than 10. |
| A number of possible values can be defined using commas to separate the values ( for instance Case 2, 3, 4 ).
A range of possible values can be defined using To ( for instance Case 5 To 10 ). This is only useful when evaluating a numeric expression.
Also more general conditions ( about the expression ) can be formulated. Case is here fore followed by Is and a comparison operator ( for instance Case Is > 10 defines the case when value is more than 10 ).
Suppose we need to bring the information "More than 10.", "More than 100." or "More than 1000." to the console. |
| Module Example7
Sub Main()
Console.WriteLine("Value ?")
Dim value As Integer = Console.ReadLine()
Select Case value
Case Is > 10
Console.WriteLine("More than 10.")
Case Is > 100
Console.WriteLine("More than 100.")
Case Is > 1000
Console.WriteLine("More than 1000.")
End Select
Console.ReadLine()
End Sub
End Module Download Broncode |
| Output : Value ?
<i>11</i>
More than 10. |
| Output : Value ?
<i>101</i>
More than 10. |
| Output : Value ?
<i>1001</i>
More than 10. |
| The program functions correctly when we enter 11, "More than 10." is the result. But when we enter 101, we get the same result. This is correct, 101 is more than 10, but probably not really the result we were hoping on.
The above Select Case ... End Select defines overlapping cases. When a value is more than 1000, it is also more than 100 and more than 10.
Be aware of the sequence in which you define overlapping cases. The above example is probably more useful when we reorder the different cases. |
| Module Example8
Sub Main()
Console.WriteLine("Value ?")
Dim value As Integer = Console.ReadLine()
Select Case value
Case Is > 1000
Console.WriteLine("More than 1000.")
Case Is > 100
Console.WriteLine("More than 100.")
Case Is > 10
Console.WriteLine("More than 10.")
End Select
Console.ReadLine()
End Sub
End Module Download Broncode |
| Output : Value ?
<i>11</i>
More than 10. |
| Output : Value ?
<i>101</i>
More than 100. |
| Output : Value ?
<i>1001</i>
More than 1000. |
Up
Exercise
| Task :
Make a program to give information about a - by the user - entered value. The options are "Less than -100.", "Less than -50.", "Number from -50 to -1.", "Zero.", "One or eleven.", "Number from 1 to 50.", "More than 50." and "More than 100.". Try to be as precise as possible, 101 for example is rather "More than 100." than "More than 50.". |
| Output : Value ?
<i>11</i>
One or eleven. |
| Output : Value ?
<i>101</i>
More than 100. |
| Output : Value ?
<i>-101</i>
Less than -100. |
| Output : Value ?
<i>51</i>
More than 50. |
| Output : Value ?
<i>0</i>
Zero. |
| Module ExerciseSolution
Sub Main()
Console.WriteLine("Value ?")
Dim value As Integer = Console.ReadLine()
Dim info As String
Select Case value
Case Is > 100
info = "More than 100."
Case Is > 50
info = "More than 50."
Case 1, 11
info = "One or eleven."
Case 2 To 50
info = "Number from 1 to 50."
Case 0
info = "Zero."
Case -50 To -1
info = "Number from -50 to -1."
Case Is < -100
info = "Less than -100."
Case Is < -50
info = "Less than -50."
End Select
Console.WriteLine(info)
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.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|