Loop Continue
Exiting a Loop Prematurely In some cases it is necessary to end a loop before the test condition would end it The following statements accomplish this Exit Do (used in Do While or Until loops) Exit For (used in For Next loops)
Years to Reach Targeted Future Value Future Value=Present Value Years to Reach Targeted Future Value Future Value=Present Value*(1+Rate)Year
Method 1 Dim PresentValue, Rate, Target, YearToTarget, futureValue As Double PresentValue = TextBox1.Text Rate = TextBox2.Text Target = TextBox3.Text futureValue = PresentValue YearToTarget = 1 Do While futureValue < Target YearToTarget = YearToTarget + 1 futureValue = PresentValue * (1 + Rate) ^ YearToTarget Loop TextBox4.Text = YearToTarget.ToString
Method 2 Dim PresentValue, Rate, Target, YearToTarget, futureValue As Double PresentValue = TextBox1.Text Rate = TextBox2.Text Target = TextBox3.Text YearToTarget = 1 Do While true futureValue = PresentValue * (1 + Rate) ^ YearToTarget If futureValue >= Target Then Exit Do End If YearToTarget = YearToTarget + 1 Loop MessageBox.Show(YearToTarget.ToString)
Method 3 Dim PresentValue, Rate, Target, YearToTarget, futureValue As Double PresentValue = TextBox1.Text Rate = TextBox2.Text Target = TextBox3.Text For YearToTarget = 1 To 9999 futureValue = PresentValue * (1 + Rate) ^ YearToTarget If futureValue >= Target Then Exit For End If Next MessageBox.Show(YearToTarget.ToString)
Searching: If fruit not exists, then add to the combobox
Code Example: Using Flag Dim testItem As String Dim myIndex As Integer Dim foundFlag As Boolean = False testItem = ComboBox1.Text For myIndex = 0 To ComboBox1.Items.Count - 1 If ComboBox1.Items(myIndex) = testItem Then foundFlag = True Exit For End If Next If foundFlag = True Then MessageBox.Show("Items already exists") Else MessageBox.Show("Item not exists") ComboBox1.Items.Add(testItem)
Method 2: Using VB’s tools Dim testItem As String testItem = ComboBox1.Text If ComboBox1.Items.Contains(testItem) Then MessageBox.Show("Items already exists") Else MessageBox.Show("Item not exists") ComboBox1.Items.Add(testItem) End If
A Loop that is Inside Another Loop is Called a Nested Loop
Nested Loop Example A clock is an example of a nested loop Minute hand repeats 60 times for each hour Second hand repeats 60 times for each minute Dim hours, minutes, seconds As Integer For hours = 0 To 24 For minutes = 0 To 59 For seconds = 0 To 59 MessageBox.Show("Hour: " + hours.ToString + " Minutes: " + minutes.ToString + " Seconds: " + seconds.ToString) Next seconds Next minutes Next hours
Nested Loop Dim i, j As Integer Dim outputLine As String For i = 1 To 4 outputLine = "" For j = 1 To 4 outputLine = outputLine + "(" + i.ToString + "," + j.ToString + ")" Next ListBox1.Items.Add(outputLine) ( 1, 1) ( 1, 2) ( 1, 3) ( 1, 4) ( 2, 1) ( 2, 2) ( 2, 3) ( 2, 4) ( 3, 1) ( 3, 2) ( 3, 3) ( 3, 4)
Nested Loop Multiplication Table 1 2 3 4 5 6 7 8 9 1 01 02 03 04 05 06 07 08 09 2 02 04 06 08 10 12 14 16 18 3 03 06 09 12 15 18 21 24 27 4 04 08 12 16 20 24 28 32 36 5 05 10 15 20 25 30 35 40 45 6 06 12 18 24 30 36 42 48 54 7 07 14 21 28 35 42 49 56 63 8 08 16 24 32 40 48 56 64 72 9 09 18 27 36 45 54 63 72 81
Code Example Dim i, j, product As Integer Dim outputLine As String ListBox1.Items.Add(" 1 2 3 4 5 6 7 8 9") For i = 1 To 9 outputLine = i.ToString For j = 1 To 9 product = i * j outputLine = outputLine + " " + product.ToString("D2") Next ListBox1.Items.Add(outputLine) Note: String(“D2”) ---- with leading zero
Future Value=Present Value*(1+Rate)Year Given PV, compute the FV starting from year 5 until specified year with a step of 5 and rate from 3% to specified rate with a step of 0.5% Rate/Year 5 10 15 20 3.00% $1,159.27 $1,343.92 $1,557.97 $1,806.11 3.50% $1,187.69 $1,410.60 $1,675.35 $1,989.79 4.00% $1,216.65 $1,480.24 $1,800.94 $2,191.12 4.50% $1,246.18 $1,552.97 $1,935.28 $2,411.71 5.00% $1,276.28 $1,628.89 $2,078.93 $2,653.30
Code Example Dim rate, year, presentValue, futureValue As Double presentValue = TextBox1.Text Dim lineOutput As String ListBox1.Items.Add("Rate/Year 5 10 15 20") For rate = 0.03 To 0.05 Step 0.005 lineOutput = rate.ToString("p2") For year = 5 To 20 Step 5 futureValue = presentValue * (1 + rate) ^ year lineOutput = lineOutput + " " + futureValue.ToString("c") Next ListBox1.Items.Add(lineOutput) rate.ToString("p2")
Exercise Create a monthly payment table for loans with term specified by user and rate range from 5% to any specified rate with an increment of .25% and loan from 100,000 to any specified loan with an increment of 50000. Monthly payment table term 30 Loan rate 100,000 150,000 200,000 250,000 5% $536.82 $805.23 $1,073.64 $1,342.05 5.25% $552.20 $828.31 $1,104.41 $1,380.51 5.50% $567.79 $851.68 $1,135.58 $1,419.47 5.75% $583.57 $875.36 $1,167.15 $1,458.93 6% $599.55 $899.33 $1,199.10 $1,498.88