Download presentation
Presentation is loading. Please wait.
Published byCandice Cain Modified over 8 years ago
1
Loop Continue ISYS 350
2
Exiting a Loop Prematurely In some cases it is necessary to end a loop before the test condition would end it Use the break statement: break;
3
Years to Reach Targeted Future Value Future Value=Present Value*(1+Rate) Year
4
Method 1 double PresentValue, Rate, Target, YearToTarget, futureValue; PresentValue = double.Parse(textBox1.Text); Rate = double.Parse(textBox2.Text); Target = double.Parse(textBox3.Text); futureValue = PresentValue; YearToTarget = 0; while (futureValue < Target) { YearToTarget = YearToTarget + 1; futureValue = PresentValue * Math.Pow(1 + Rate,YearToTarget); } textBox4.Text = YearToTarget.ToString();
5
Method 2 double PresentValue, Rate, Target, YearToTarget, futureValue; PresentValue = double.Parse(textBox1.Text); Rate = double.Parse(textBox2.Text); Target = double.Parse(textBox3.Text); YearToTarget = 0; while (true) { futureValue = PresentValue * Math.Pow(1 + Rate, YearToTarget); if (futureValue >= Target) break; YearToTarget = YearToTarget + 1; } textBox4.Text = YearToTarget.ToString();
6
Method 3 double PresentValue, Rate, Target, YearToTarget, futureValue; PresentValue = double.Parse(textBox1.Text); Rate = double.Parse(textBox2.Text); Target = double.Parse(textBox3.Text); for (YearToTarget = 0; YearToTarget<99999; YearToTarget++) { futureValue = PresentValue * Math.Pow(1 + Rate, YearToTarget); if (futureValue >= Target) break; } textBox4.Text = YearToTarget.ToString(); }
7
Searching: If fruit not exists, then add to the combobox
8
Code Example: Using Flag string testItem; testItem = comboBox1.Text; Boolean foundFlag=false; for (int counter = 0; counter <= comboBox1.Items.Count - 1; counter++) { if (testItem == comboBox1.Items[counter].ToString()) { foundFlag = true; break; } if (foundFlag) MessageBox.Show("item already exist"); else { MessageBox.Show("item added"); comboBox1.Items.Add(testItem); }
9
Method 2: Using.Net’s Support string testItem; testItem = comboBox1.Text; if (comboBox1.Items.Contains(testItem)) MessageBox.Show("item already exist"); else { MessageBox.Show("item added"); comboBox1.Items.Add(testItem); }
10
Straight Line Depreciation Table
11
dataGridView Control Properties – ColumnCount: specify the number of columns dataGridView1.ColumnCount = 4; – Columns collection each column is identified by an index Each column has a HeaderText property: – Ex. dataGridView1.Columns[0].HeaderText = "Year"; – Rows collection each row is identified by an index Adding a blank row: – dataGridView1.Rows.Add(); – Each Row has a Cells collection Each cell is identified by an index Has a value property: – dataGridView1.Rows[year - 1].Cells[0].Value = year;
12
Depreciation Table Code Example dataGridView1.ColumnCount = 4; dataGridView1.Columns[0].HeaderText = "Year"; dataGridView1.Columns[1].HeaderText = "Value at Begining of Year"; dataGridView1.Columns[2].HeaderText = "Depreciation During Year"; dataGridView1.Columns[3].HeaderText = "Total depreciation to End of Year"; double value, life, depreciation, totalDepreciation=0; value = double.Parse(textBox1.Text); life = double.Parse(textBox2.Text); depreciation = value / life; dataGridView1.Rows.Clear(); for (int year = 1; year <= life; year++) { dataGridView1.Rows.Add(); dataGridView1.Rows[year - 1].Cells[0].Value = year; dataGridView1.Rows[year - 1].Cells[1].Value = value.ToString("c"); dataGridView1.Rows[year-1].Cells[2].Value = depreciation.ToString("c"); totalDepreciation += depreciation; dataGridView1.Rows[year - 1].Cells[3].Value = totalDepreciation.ToString("c"); value -= depreciation; }
13
Nested Loop A Loop that is Inside Another Loop is Called a Nested Loop
14
Nested Loop Example for (int hour = 0; hour < 24; hour++) { for (int minute = 0; minute < 60; minute++) { MessageBox.Show("Hour: " + hour.ToString() + " Minute: " + minute.ToString()); } A clock is an example of a nested loop – For each hour, Minute hand moves 60 times for each hour
15
Nested Loop int rows=3, cols=4; string outputLine; for (int rowIndex = 1; rowIndex <= rows; rowIndex++) { outputLine = ""; for (int colIndex = 1; colIndex <= cols; colIndex++) { outputLine = outputLine + "(" + rowIndex.ToString() + "," + colIndex.ToString() + ")"; } 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)
16
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
17
Code Example int rows=9, cols=9; string outputLine; listBox1.Items.Add(" 1 2 3 4 5 6 7 8 9"); for (int rowIndex = 1; rowIndex <= rows; rowIndex++) { outputLine = ""; for (int colIndex = 1; colIndex <= cols; colIndex++) { outputLine = outputLine + " " + (rowIndex * colIndex).ToString("D2"); } listBox1.Items.Add(outputLine); } Note: String(“D2”) ---- with leading zero
18
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
19
Code Example double rate, year, presentValue, futureValue; presentValue = double.Parse(textBox1.Text); string lineOutput; listBox1.Items.Add("Rate/Year 5 10 15 20"); for (rate = 0.03; rate <= 0.05; rate += 0.005) { lineOutput = rate.ToString("p2"); for (year = 5; year <= 20; year += 5) { futureValue = presentValue * Math.Pow(1 + rate, year); lineOutput = lineOutput + " " + futureValue.ToString("c"); } listBox1.Items.Add(lineOutput); } rate.ToString("p2")
20
Using dataGridView
21
double rate, year, presentValue, futureValue; presentValue = double.Parse(textBox1.Text); dataGridView1.ColumnCount = 5; dataGridView1.Columns[0].HeaderText = "Rate/Year"; dataGridView1.Columns[1].HeaderText = "5"; dataGridView1.Columns[2].HeaderText = "10"; dataGridView1.Columns[3].HeaderText = "15"; dataGridView1.Columns[4].HeaderText = "20"; int rowIndex = 0, colIndex; dataGridView1.Rows.Clear(); for (rate = 0.03; rate <= 0.05; rate += 0.005) { dataGridView1.Rows.Add(); colIndex = 0; dataGridView1.Rows[rowIndex].Cells[colIndex].Value = rate.ToString("p2"); for (year = 5; year <= 20; year += 5) { futureValue = presentValue * Math.Pow(1 + rate, year); colIndex += 1; dataGridView1.Rows[rowIndex].Cells[colIndex].Value = futureValue.ToString("c"); } rowIndex += 1; }
22
Exercise Monthly payment table term30 Loan rate100,000150,000200,000250,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 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. Using listbox or using dataGridView to show the table
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.