Presentation is loading. Please wait.

Presentation is loading. Please wait.

Loop Continue.

Similar presentations


Presentation on theme: "Loop Continue."— Presentation transcript:

1 Loop Continue

2 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)

3 Years to Reach Targeted Future Value Future Value=Present Value
Years to Reach Targeted Future Value Future Value=Present Value*(1+Rate)Year

4 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

5 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)

6 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)

7 Searching: If fruit not exists, then add to the combobox

8 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)

9 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

10 A Loop that is Inside Another Loop is Called a Nested Loop

11 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

12 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)

13 Nested Loop Multiplication Table

14 Code Example Dim i, j, product As Integer Dim outputLine As String
ListBox1.Items.Add(" ") 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

15 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 3.00% $1, $1, $1, $1,806.11 3.50% $1, $1, $1, $1,989.79 4.00% $1, $1, $1, $2,191.12 4.50% $1, $1, $1, $2,411.71 5.00% $1, $1, $2, $2,653.30

16 Code Example Dim rate, year, presentValue, futureValue As Double
presentValue = TextBox1.Text Dim lineOutput As String ListBox1.Items.Add("Rate/Year ") 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")

17 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 Monthly payment table term 30 Loan rate 100, , , ,000 5% $ $ $1, $1,342.05 5.25% $ $ $1, $1,380.51 5.50% $ $ $1, $1,419.47 5.75% $ $ $1, $1,458.93 6% $ $ $1, $1,498.88


Download ppt "Loop Continue."

Similar presentations


Ads by Google