4.Statements
4.1.If
4.1.1.If with And
|
Imports System
Public Class Test
Shared Sub Main()
Dim dtCurrent As System.DateTime
Dim iHour As Integer
dtCurrent = dtCurrent.Now()
iHour = dtCurrent.Hour
If (iHour < 12) Then
Console.Writeline("Good Morning!")
ElseIf (iHour >= 12) And (iHour < 18) Then
Console.WriteLine("Good Afternoon!")
Else
Console.WriteLine("Good Evening!")
End If
End Sub
End Class
|
Good Afternoon!
4.1.2.Use if statement to compare Integer
|
Option Strict On
Imports System
Module Module1
Sub Main( )
Dim valueOne As Integer = 10
Dim valueTwo As Integer = 20
Dim valueThree As Integer = 30
If valueOne > valueTwo Then
Console.WriteLine("ValueOne: {0} larger than ValueTwo: {1}", valueOne, valueTwo)
End If
If valueThree > valueTwo Then
Console.WriteLine("ValueThree: {0} larger than ValueTwo: {1}",valueThree, valueTwo)
End If
If valueTwo > 15 Then
Console.WriteLine("Yes it is")
End If
End Sub 'Main
End Module
|
ValueThree: 30 larger than ValueTwo: 20
Yes it is
4.1.3.Nested if statement
|
Option Strict On
Imports System
Module Module1
Sub Main( )
Dim temp As Integer = 32
If temp <= 32 Then
If temp = 32 Then
Console.WriteLine("temp = 32")
Else
Console.WriteLine("Temp: {0}", temp)
End If
End If
End Sub
End Module
|
temp = 32
4.1.4.Use ElseIf
|
Option Strict On
Imports System
Module Module1
Sub Main( )
Dim temp As Integer = -32
If temp > 32 Then
ElseIf temp = 32 Then
Console.WriteLine("= 32")
ElseIf temp > 0 Then
ElseIf temp = 0 Then
Console.WriteLine(" = 0")
Else
Console.WriteLine("Else")
End If
End Sub 'Main
End Module
|
Else
4.1.5.Else If (Not ELSEIF)
|
Imports System
Public Class Test
Shared Sub Main()
Dim dtCurrent As System.DateTime
Dim iHour As Integer
dtCurrent = dtCurrent.Now()
iHour = dtCurrent.Hour
If (iHour < 12) Then
Console.Writeline("Good Morning!")
Else If (iHour >= 12) And (iHour < 18) Then
Console.WriteLine("Good Afternoon!")
Else
Console.WriteLine("Good Evening!")
End If
End Sub
End Class
|
Very Good.
4.1.6.Use If statement in a For statement
|
public class Test
public Shared Sub Main
Dim intCounter As Integer
Dim strString As String = "some silly string"
For intCounter = 0 To strString.Length - 1
If strString(intCounter) = "s" Then
strString = strString.Remove(intCounter, 1)
strString = strString.Insert(intCounter, "S")
End If
Next intCounter
Console.WriteLine(strString)
End Sub
End class
|
Some Silly String
4.1.7.ElseIf
|
public class Test
public Shared Sub Main
Dim intScore As Integer
Dim strResult As String
intScore = 78
If intScore < 50 Then
strResult = "Failed."
ElseIf intScore < 75 Then
strResult = "Pass."
ElseIf intScore < 90 Then
strResult = "Very Good."
Else
strResult = "Excellent."
End If
Console.WriteLine(strResult)
End Sub
End class
|
Very Good.
4.1.8.Single line vs Multi-line If statement
|
public class Test
public Shared Sub Main
Dim intScore As Integer
intScore = 23
'Single line If statement.
If intScore < 50 Then Console.WriteLine("Failed.") : Console.WriteLine("Please try again.")
'Multi-line If statement.
If intScore < 50 Then
Console.WriteLine("Failed.")
Console.WriteLine("Please try again.")
End If
End Sub
End class
|
Failed.
Please try again.
Failed.
Please try again.
|
4.1.9.If statement Short Circuiting
|
|
Public Class ShortCircuiting
Shared Sub Main()
If Test("Left") AND Test("Right") Then
'do something
End If
End Sub
Shared Function Test(sInput As String) As Boolean
System.Console.WriteLine(sInput)
Test = FALSE
End Function
End Class
|
|
|
Left
Right
|
4.1.10.If Short Circuiting with ANDALSO
|
Public Class ShortCircuiting
Shared Sub Main()
If Test("Left") ANDALSO Test("Right") Then
'do something
End If
End Sub
Shared Function Test(sInput As String) As Boolean
System.Console.WriteLine(sInput)
Test = FALSE
End Function
End Class
|
Left
4.2.IIf
4.2.1.IIf
|
public class Test
public Shared Sub Main
Dim intScore As Integer
intScore = 23
Console.WriteLine( IIf((intScore < 50), 1, 2) )
End Sub
End class
|
1
4.3.Select
4.3.1.Using Select Case structure
|
Module Tester
Sub Main()
Dim grade As Integer = 89
Dim aCount As Integer = 0
Dim bCount As Integer = 0
Dim cCount As Integer = 0
Dim dCount As Integer = 0
Dim fCount As Integer = 0
Select Case grade
Case 100
Console.WriteLine("Perfect Score!" & vbCrLf & "Letter grade: A" & vbCrLf)
aCount += 1
Case 90 To 99
Console.WriteLine("Letter Grade: A" & vbCrLf)
aCount += 1
Case 80 To 89
Console.WriteLine("Letter Grade: B" & vbCrLf)
bCount += 1
Case 70 To 79
Console.WriteLine("Letter Grade: C" & vbCrLf)
cCount += 1
Case 60 To 69
Console.WriteLine("Letter Grade: D" & vbCrLf)
dCount += 1
Case 0, 10 To 59
Console.WriteLine("Letter Grade: F" & vbCrLf)
fCount += 1
Case Else
Console.WriteLine("Invalid Input. " & _
"Please enter a valid grade." & vbCrLf)
End Select
End Sub
End Module
|
Letter Grade: B
4.3.2.Select Case statement with Integer and exact value
|
Option Strict On
Imports System
Module Module1
Sub Main( )
Dim targetInteger As Integer = 15
Select Case targetInteger
Case 5
Console.WriteLine("5")
Case 10
Console.WriteLine("10")
Case 15
Console.WriteLine("15!")
Case Else
Console.WriteLine("Value not found")
End Select
End Sub 'Main
End Module
|
15!
4.3.3.Select Case statement with range Integer value
|
Option Strict On
Imports System
Module Module1
Sub Main( )
Dim targetInteger As Integer = 7
Select Case targetInteger
Case Is < 10
Console.WriteLine("Less than 10")
Case 10 To 14
Console.WriteLine("10-14")
Case 15
Console.WriteLine("15!")
Case Else
Console.WriteLine("Value not found")
End Select
End Sub 'Main
End Module
|
Less than 10
4.3.4.Select Case statement with String range value
|
Option Strict On
Imports System
Module Module1
Sub Main( )
Dim target As String = "M"
Select Case target
Case "A" To "L"
Console.WriteLine("A To L executed")
Case "L" To "Z"
Console.WriteLine("L To Z executed")
Case Else
Console.WriteLine("Else executed")
End Select
End Sub 'Main
End Module
|
L To Z executed
4.3.5.Select Now.DayOfWeek
|
public class Test
public Shared Sub Main
Dim strMessage As String
Select Case Now.DayOfWeek
Case DayOfWeek.Monday
strMessage = "Have a nice week."
Case DayOfWeek.Friday
strMessage = "Have a nice weekend."
Case Else
strMessage = "Welcome back!"
End Select
Console.WriteLine(strMessage)
End Sub
End class
|
Have a nice weekend.
4.3.6.Use both range and fix value in Select statement
|
Module Module1
Sub Main()
Dim intInput As Integer = 12
Select Case intInput
Case 1
Console.WriteLine("Thank you for the 1.")
Case 2 To 5
Console.WriteLine("Your value was 2, 3, 4, or 5")
Case Is > 5
Console.WriteLine("That was greater than 5.")
Case Else
Console.WriteLine("Sorry, I can't deal with that.")
End Select
End Sub
End Module
|
That was greater than 5.
4.3.7.Specify several values in one Case statement
|
public class Test
public Shared Sub Main
Select Case "Bird"
Case "Bird"
Console.WriteLine("This animal has 2 legs.")
Case "Horse", "Dog", "Cat"
Console.WriteLine("This animal has 4 legs.")
Case "Snake"
Console.WriteLine("This animal has no legs.")
Case "Centipede"
Console.WriteLine("This animal has 100 legs.")
Case Else
Console.WriteLine("You did not select from the list!")
End Select
End Sub
End Class
|
This animal has 2 legs.
4.4.Switch
4.4.1.Microsoft.VisualBasic.Switch
|
Module Module1
Sub Main()
Dim intValue As Integer = -10
Console.WriteLine("Absolute value: " & _
Microsoft.VisualBasic.Switch(intValue < 0, -1 * intValue, intValue >= 0, intValue))
End Sub
End Module
|
Absolute value: 10
4.5.With statement
4.5.1.Use With statement with Form
object
|
Imports System.Drawing
Imports System.Windows.Forms
Public Class WithForm : Inherits Form
Public Shared Sub Main()
Dim frm as New Form
With frm
.BackColor = Color.Gray
.ForeColor = Color.Red
.Text = "The With Statement"
.Enabled = True
.Topmost = True
.MinimizeBox = False
.ShowDialog()
End With
End Sub
End Class
|
4.5.2.Insert statement into With
statement
|
Imports System.Drawing
Imports System.Windows.Forms
Public Class WithForm : Inherits Form
Public Shared Sub Main()
Dim frm as New Form
With frm
.BackColor = Color.Gray
.ForeColor = Color.Red
.Text = "The With Statement"
Dim fnt As Font = .Font
MsgBox(fnt.Name)
.Enabled = True
.Topmost = True
.MinimizeBox = False
.ShowDialog()
End With
End Sub
End Class
|
4.5.3.Nested With statement
|
Imports System.Drawing
Imports System.Windows.Forms
Public Class Form1 : Inherits Form
Public Shared Sub Main()
Dim frm As New Form1()
Application.Run(frm)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With Me
.BackColor = Color.Gray
.ForeColor = Color.Red
.Text = "The With Statement"
.Enabled = True
.TopMost = True
.MinimizeBox = False
For Each ctrl As Control In .Controls
With ctrl
.BackColor = SystemColors.Window
.AutoSize = False
.Cursor = Cursors.Hand
End With
Next
End With
End Sub
End Class
|
4.5.4.Use With statement with
OpenFileDialog
|
Imports System.Windows.Forms
public class OpenFileDialogWith
public Shared Sub Main
Dim OpenFileDialog1 As OpenFileDialog = New System.Windows.Forms.OpenFileDialog
With OpenFileDialog1
.FileName = "C:\My Documents\Doc1.txt"
.DefaultExt = ".txt"
.Filter = "Text Files|*.TXT"
.Filter = "Bitmaps|*.BMP|GIF Images|*.GIF|" & _
"JPG Images|*.JPG|All Images|*.BMP;*.GIF;*.JPG"
.FilterIndex = 2
.InitialDirectory = "C:\My Documents"
.ShowReadOnly = True
.ReadOnlyChecked = True
End With
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Console.WriteLine(OpenFileDialog1.FileName)
End If
End Sub
End class
|
4.6.For
4.6.1.For To Next Loop
|
Option Strict On
Imports System
Module Module1
Sub Main( )
Dim loopCounter As Integer
For loopCounter = 0 To 9
Console.WriteLine("loopCounter: {0}", loopCounter)
Next
End Sub
End Module
|
loopCounter: 0
loopCounter: 1
loopCounter: 2
loopCounter: 3
loopCounter: 4
loopCounter: 5
loopCounter: 6
loopCounter: 7
loopCounter: 8
loopCounter: 9
4.6.2.Using For/Next structure to demonstrate summation.
|
Imports System.Windows.Forms
Module Tester
Sub Main()
Dim sum = 0, number As Integer
' add even numbers from 2 to 100
For number = 2 To 100 Step 2
sum += number
Next
Console.WriteLine("The sum is " & sum & "Sum even integers from 2 to 100")
End Sub
End Module
|
The sum is 2550Sum even integers from 2 to 100
4.6.3.Calculating compound interest.
|
Module Tester
Sub Main()
Dim amount, principal As Decimal
Dim rate As Double
Dim year As Integer
Dim output As String
principal = 1000
rate = 0.05
output = "Year" & vbTab & "Amount on deposit" & vbCrLf
For year = 1 To 10
amount = principal * (1 + rate) ^ year
output &= year & vbTab & _
String.Format("{0:C}", amount) & vbCrLf
Next
Console.WriteLine(output)
End Sub
End Module
|
Year Amount on deposit
1 $1,050.00
2 $1,102.50
3 $1,157.63
4 $1,215.51
5 $1,276.28
6 $1,340.10
7 $1,407.10
8 $1,477.46
9 $1,551.33
10 $1,628.89
4.6.4.For loop with float point control number
|
Option Strict On
Imports System
Module Module1
Sub Main( )
Dim loopCounter As Single
For loopCounter = 0.5 To 9
Console.WriteLine("loopCounter: {0}", loopCounter)
Next
End Sub 'Main
End Module
|
loopCounter: 0.5
loopCounter: 1.5
loopCounter: 2.5
loopCounter: 3.5
loopCounter: 4.5
loopCounter: 5.5
loopCounter: 6.5
loopCounter: 7.5
loopCounter: 8.5
4.6.5.Set For Loop float Step
|
Option Strict On
Imports System
Module Module1
Sub Main( )
Dim loopCounter As Single
For loopCounter = 0.5 To 9 Step 0.5
Console.WriteLine("loopCounter: {0}", loopCounter)
Next
End Sub 'Main
End Module
|
loopCounter: 0.5
loopCounter: 1
loopCounter: 1.5
loopCounter: 2
loopCounter: 2.5
loopCounter: 3
loopCounter: 3.5
loopCounter: 4
loopCounter: 4.5
loopCounter: 5
loopCounter: 5.5
loopCounter: 6
loopCounter: 6.5
loopCounter: 7
loopCounter: 7.5
loopCounter: 8
loopCounter: 8.5
loopCounter: 9
4.6.6.Nested For Loop
|
Option Strict On
Imports System
Module Module1
Sub Main( )
Dim outer As Integer
Dim inner As Integer
For outer = 3 To 6
For inner = 10 To 12
Console.WriteLine("{0} * {1} = {2}", _
outer, inner, outer * inner)
Next inner, outer
End Sub 'Main
End Module
|
3 * 10 = 30
3 * 11 = 33
3 * 12 = 36
4 * 10 = 40
4 * 11 = 44
4 * 12 = 48
5 * 10 = 50
5 * 11 = 55
5 * 12 = 60
6 * 10 = 60
6 * 11 = 66
6 * 12 = 72
4.6.7.Float step
|
public class Test
public Shared Sub Main
For x As Single = 1 To 3 Step 0.5
Console.WriteLine(x.ToString("0.0"))
Next x
End Sub
End class
|
1.0
1.5
2.0
2.5
3.0
4.6.8.Calculated step
|
public class Test
public Shared Sub Main
For x As Single = 1 To 2 Step 1 / 7
Console.WriteLine(x)
Next x
End Sub
End class
|
1
1.142857
1.285714
1.428572
1.571429
1.714286
1.857143
4.6.9.Exit For 1
|
public class Test
public Shared Sub Main
Dim j As Integer
For i As Integer = 1 To 3
j = 0
Do While j < 3
j += 1
For k As Integer = 1 To 3
Dim test1 As Boolean = k = 2
If test1 Then Exit For ' Exits the For K loop.
Dim test2 As Boolean = i = j
If test2 Then Exit Do ' Exits the Do.
Console.WriteLine(i & ", " & j & ", " & k)
Next k
Loop
Next i
End Sub
End class
|
2, 1, 1
3, 1, 1
3, 2, 1
4.7.For Each
4.7.1.Use For Each to loop through
Array
|
Option Strict On
Imports System
Public Class Employee
Private empID As Integer
Public Sub New(ByVal empID As Integer)
Me.empID = empID
End Sub
Public Overrides Function ToString( ) As String
Return empID.ToString( )
End Function
End Class
Class Tester
Shared Sub Main( )
Dim intArray( ) As Integer
Dim empArray( ) As Employee
intArray = New Integer(5) {}
empArray = New Employee(3) {}
Dim i As Integer
For i = 0 To empArray.Length - 1
empArray(i) = New Employee(i + 5)
Next i
Console.WriteLine("The Integer array...")
Dim intValue As Integer
For Each intValue In intArray
Console.WriteLine(intValue.ToString( ))
Next
Console.WriteLine("The employee array...")
Dim e As Employee
For Each e In empArray
Console.WriteLine(e)
Next
End Sub 'Run
End Class
|
The Integer array...
0
0
0
0
0
0
The employee array...
5
6
7
8
4.7.2.For Each with Collection
|
Imports System.Collections
public class Test
public Shared Sub Main
Dim m_employees As New Collection
m_employees.Add(New employee("A", "F", "F"))
m_employees.Add(New employee("B", "E", "H"))
m_employees.Add(New employee("C", "D", "I"))
For Each emp1 As employee In m_employees
If emp1.FirstName = "A" Then Continue For
Console.WriteLine(emp1.Title & " " & emp1.FirstName & " " & emp1.LastName)
Next emp1
End Sub
End class
Public Class Employee
Public FirstName As String
Public LastName As String
Public Title As String
Public Sub New(ByVal first_name As String, ByVal last_name As String, ByVal new_title As String)
FirstName = first_name
LastName = last_name
Title = new_title
End Sub
End Class
|
H B E
I C D
4.8.While
4.8.1.While structure
|
Module Tester
Sub Main()
Dim product As Integer = 2
While product <= 1000
Console.Write("{0} ", product)
product = product * 2
End While
Console.WriteLine("Smallest power of 2 " & _
"greater than 1000 is {0}", product)
End Sub ' Main
End Module
|
2 4 8 16 32 64 128 256 512 Smallest power of 2 greater than 1000 is 1024
4.8.2.Using counter-controlled repetition
|
Module modAverage
Sub Main()
Dim total As Integer
Dim gradeCounter As Integer
Dim grade As Integer
Dim average As Double
total = 0
gradeCounter = 1
While gradeCounter <= 10
grade = 12
total += grade
gradeCounter += 1
End While
average = total / 10
Console.WriteLine("Class average is {0}", average)
End Sub
End Module
|
Class average is 12
4.8.3.Draw square of * by using nested while loop
|
Module Tester
Sub Main()
Dim side As Integer
Dim row As Integer = 1
Dim column As Integer
side = 12
If side <= 20 Then
While row <= side
column = 1
While column <= side
Console.Write("* ")
column += 1
End While
Console.WriteLine()
row += 1
End While
Else
Console.WriteLine("Side too large")
End If
End Sub
End Module
|
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
4.8.4.Combine While and If to search in an array
|
Imports System
Public Class WhileExample
Shared Sub Main()
Dim iCounter As Integer = 0
Dim arrList(9) As String
Dim iMatch As Integer = -1
Dim sMatch As String
sMatch = "A"
arrList(0) = "A"
arrList(1) = "B"
arrList(2) = "C"
arrList(3) = "D"
arrList(4) = "E"
arrList(5) = "G"
arrList(6) = "H"
arrList(7) = "I"
arrList(8) = "J"
arrList(9) = "K"
While iCounter <= 9 AND iMatch = -1
If arrList(iCounter) Like sMatch Then
iMatch = iCounter
Else
iCounter = iCounter + 1
End If
End While
If iMatch <> -1 Then
System.Console.WriteLine("Matched " & iMatch)
End If
End Sub
End Class
|
4.9.Do Loop Until
4.9.1.Using Do/Loop Until repetition
structure
|
Module Tester
Sub Main()
Dim counter As Integer = 1
Do
Console.Write(counter & " ")
counter += 1
Loop Until counter > 5
End Sub
End Module
|
1 2 3 4 5 "
4.10.Do Loop While
4.10.1.Demonstrating the Do/Loop While
repetition structure.
|
Module Tester
Sub Main()
Dim counter As Integer = 1
Do
Console.Write(counter & " ")
counter += 1
Loop While counter <= 5
End Sub
End Module
|
1 2 3 4 5
4.11.Do Until
4.11.1.Do Until Loop
|
Option Strict On
Imports System
Module Module1
Sub Main( )
Dim counterVariable As Integer = 0
Do Until counterVariable = 10
Console.WriteLine("counterVariable: {0}", counterVariable)
counterVariable = counterVariable + 1
Loop
End Sub
End Module
|
counterVariable: 0
counterVariable: 1
counterVariable: 2
counterVariable: 3
counterVariable: 4
counterVariable: 5
counterVariable: 6
counterVariable: 7
counterVariable: 8
counterVariable: 9
4.11.2.Do Until/Loop (2)
|
Module Tester
Sub Main()
Dim product As Integer = 2
Do Until product > 1000
Console.Write("{0} ", product)
product = product * 2
Loop
Console.WriteLine("Smallest power of 2 " & _
"greater than 1000 is {0}", product)
End Sub
End Module
|
2 4 8 16 32 64 128 256 512 Smallest power of 2 greater than 1000 is 1024
4.12.Do While
4.12.1.Do While Loop
|
Option Strict On
Imports System
Module Module1
Sub Main( )
Dim counterVariable As Integer = 0
Do While counterVariable < 10
Console.WriteLine("counterVariable: {0}", counterVariable)
counterVariable = counterVariable + 1
Loop
End Sub
End Module
|
counterVariable: 0
counterVariable: 1
counterVariable: 2
counterVariable: 3
counterVariable: 4
counterVariable: 5
counterVariable: 6
counterVariable: 7
counterVariable: 8
counterVariable: 9
4.12.2.Do While/Loop: multiplies and displays product while product
is less than or equal to 1000
|
Module Tester
Sub Main()
Dim product As Integer = 2
Do While product <= 1000
Console.Write("{0} ", product)
product = product * 2
Loop
Console.WriteLine("Smallest power of 2 " & _
"greater than 1000 is {0}", product)
End Sub
End Module
|
2 4 8 16 32 64 128 256 512 Smallest power of 2 greater than 1000 is 1024
4.12.3.Exit Do While
|
public class Test
public Shared Sub Main
Dim j As Integer
For i As Integer = 1 To 3
j = 0
Do While j < 3
j += 1
For k As Integer = 1 To 3
Dim test1 As Boolean = k = 2
If test1 Then Exit For ' Exits the For K loop.
Dim test2 As Boolean = i = j
If test2 Then Exit Do ' Exits the Do.
Console.WriteLine(i & ", " & j & ", " & k)
Next k
Loop
Next i
End Sub
End class
|
2, 1, 1
3, 1, 1
3, 2, 1
4.14.Continue
4.14.1.Continue Do
|
public class Test
public Shared Sub Main
Dim j As Integer
For i As Integer = 1 To 3
j = 0
Do While j < 3
j += 1
For k As Integer = 1 To 3
Dim test1 As Boolean = k = 2
If test1 Then Continue For ' Continues the For K loop.
Dim test2 As Boolean = i = j
If test2 Then Continue Do ' Continues the Do.
Console.WriteLine(i & ", " & j & ", " & k)
Next k
Loop
Next i
End Sub
End class
|
1, 2, 1
1, 2, 3
1, 3, 1
1, 3, 3
2, 1, 1
2, 1, 3
2, 3, 1
2, 3, 3
3, 1, 1
3, 1, 3
3, 2, 1
3, 2, 3
4.14.2.Continue For
|
Option Strict On
Public Module ContinueStatement
Public Sub Main()
For ctr As Integer = 1 to 100
If ctr Mod 2 = 1 Then
Console.Write("{0} is odd", ctr)
Else
Console.WriteLine("{0} is even.", ctr)
Continue For
End If
If ctr Mod 3 = 0 Then
Console.Write(" and it is divisible by 3")
End If
If ctr Mod 5 = 0 Then
Console.Write(" and it is divisible by 5")
End If
If ctr Mod 7 = 0 Then
Console.Write(" and it is divisible by 7")
End If
Console.WriteLine()
Next
End Sub
End Module
|
1 is odd
2 is even.
3 is odd and it is divisible by 3
4 is even.
5 is odd and it is divisible by 5
6 is even.
7 is odd and it is divisible by 7
8 is even.
9 is odd and it is divisible by 3
10 is even.
11 is odd
12 is even.
13 is odd
14 is even.
15 is odd and it is divisible by 3 and it is divisible by 5
16 is even.
17 is odd
18 is even.
19 is odd
20 is even.
21 is odd and it is divisible by 3 and it is divisible by 7
22 is even.
23 is odd
24 is even.
25 is odd and it is divisible by 5
26 is even.
27 is odd and it is divisible by 3
28 is even.
29 is odd
30 is even.
31 is odd
32 is even.
33 is odd and it is divisible by 3
34 is even.
35 is odd and it is divisible by 5 and it is divisible by 7
36 is even.
37 is odd
38 is even.
39 is odd and it is divisible by 3
40 is even.
41 is odd
42 is even.
43 is odd
44 is even.
45 is odd and it is divisible by 3 and it is divisible by 5
46 is even.
47 is odd
48 is even.
49 is odd and it is divisible by 7
50 is even.
51 is odd and it is divisible by 3
52 is even.
53 is odd
54 is even.
55 is odd and it is divisible by 5
56 is even.
57 is odd and it is divisible by 3
58 is even.
59 is odd
60 is even.
61 is odd
62 is even.
63 is odd and it is divisible by 3 and it is divisible by 7
64 is even.
65 is odd and it is divisible by 5
66 is even.
67 is odd
68 is even.
69 is odd and it is divisible by 3
70 is even.
71 is odd
72 is even.
73 is odd
74 is even.
75 is odd and it is divisible by 3 and it is divisible by 5
76 is even.
77 is odd and it is divisible by 7
78 is even.
79 is odd
80 is even.
81 is odd and it is divisible by 3
82 is even.
83 is odd
84 is even.
85 is odd and it is divisible by 5
86 is even.
87 is odd and it is divisible by 3
88 is even.
89 is odd
90 is even.
91 is odd and it is divisible by 7
92 is even.
93 is odd and it is divisible by 3
94 is even.
95 is odd and it is divisible by 5
96 is even.
97 is odd
98 is even.
99 is odd and it is divisible by 3
100 is even.
4.15.Counter Controlled Repetition
4.15.1.Using the While structure to
demonstrate counter-controlled repetition.
|
Module Tester
Sub Main()
Dim counter As Integer = 2
While counter <= 10
Console.Write(counter & " ")
counter += 2
End While
End Sub
End Module
|
2 4 6 8 10
4.15.2.Using the For/Next structure to demonstrate counter-controlled
repetition.
|
Module Tester
Sub Main()
Dim counter As Integer
For counter = 2 To 10 Step 2
Console.Write(counter & " ")
Next
End Sub
End Module
|
2 4 6 8 10
4.16.GoTo
4.16.1.GoTo statement with Label
|
Option Strict On
Imports System
Module Module1
Sub Main( )
Dim counterVariable As Integer = 0
repeat:
Console.WriteLine("counterVariable: {0}", counterVariable)
counterVariable += 1
If counterVariable < 10 Then
GoTo repeat
End If
End Sub
End Module
|
counterVariable: 0
counterVariable: 1
counterVariable: 2
counterVariable: 3
counterVariable: 4
counterVariable: 5
counterVariable: 6
counterVariable: 7
counterVariable: 8
counterVariable: 9
4.17.Imports
4.17.1.Imports System
|
Module MyModule
Enum Medal As Short
Gold
Silver
Bronze
End Enum
Sub Main()
Dim myMedal As Medal = Medal.Bronze
Console.WriteLine("My medal: " & myMedal.ToString)
End Sub
End Module
|
My medal: Bronze
4.17.2.Imports command
|
Imports System
Public Class Step2
Shared Sub Main()
Console.WriteLine("info")
End Sub
End Class
|
info
4.18.On Error GoTo
4.18.1.On Error GoTo errorhandle
|
Imports System.IO
public class Tester
public Shared Sub Main
Dim myfile As System.IO.File
Dim w As System.IO.StreamWriter
On Error GoTo errorhandle
w = myfile.AppendText("a:\temp.txt")
w.Write("a")
w.Close()
Exit Sub
errorhandle:
Console.WriteLine("Please Insert")
Exit Sub
Resume
End Sub
End class
|
Please Insert
4.18.2.Select Case Err.Number
|
Imports System.IO
public class Tester
public Shared Sub Main
On Error GoTo ErrorHandle
Exit Sub
ErrorHandle:
Select Case Err.Number
Case 52
Console.WriteLine("File Name Exception")
Case 53
Console.WriteLine("File cannot be found")
Case 54
Console.WriteLine("File Mode Exception")
Case 55
Console.WriteLine("File is aleady open")
Case 58
Console.WriteLine("File exists")
Case 68
Console.WriteLine("Device is not available")
Case 75
Console.WriteLine("Path Exception")
Case 76
Console.WriteLine("Path cannot be found")
Case Else
Console.WriteLine("Other")
End Select
End Sub
End class
|
4.19.Resume
4.19.1.Resume
|
Module Module1
Sub Main()
On Error GoTo Handler
Dim i1 As Integer = 0
Dim i2 As Integer = 128
Dim intResult As Integer
intResult = i2 / i1
Console.WriteLine("Press Enter to continue...")
Console.ReadLine()
Exit Sub
Handler:
Console.WriteLine("An overflow error occurred.")
Resume Next
End Sub
End Module
|
An overflow error occurred.
Press Enter to continue..
4.20.Using statement
4.20.1.Using statement
|
public class Test
public Shared Sub Main
Using _
emp1 As New Employee("Ann", "Archer"), _
emp2 As New Employee("Bob", "Beagle")
Dim i As Integer
End Using
End Sub
End class
Public Class Employee
Implements IDisposable
Public Sub New(ByVal first_name As String, ByVal last_name As String)
End Sub
Private disposedValue As Boolean = False
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
Console.WriteLine("TODO: free unmanaged resources when explicitly called")
End If
Console.WriteLine("TODO: free shared unmanaged resources")
End If
Me.disposedValue = True
End Sub
Public Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
Protected Overrides Sub Finalize()
Dispose(False)
MyBase.Finalize()
End Sub
End Class
|
TODO: free unmanaged resources when explicitly called
TODO: free shared unmanaged resources
TODO: free unmanaged resources when explicitly called
TODO: free shared unmanaged resources