Presentation is loading. Please wait.

Presentation is loading. Please wait.

Loop Continue ISYS 350. Straight Line Depreciation Table.

Similar presentations


Presentation on theme: "Loop Continue ISYS 350. Straight Line Depreciation Table."— Presentation transcript:

1 Loop Continue ISYS 350

2 Straight Line Depreciation Table

3 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 Clear all rows: dataGridView1.Rows.Clear(); 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;

4 Future Value Table FV = PV * (1 +Rate) Year

5 Code Example dataGridView1.ColumnCount = 2; dataGridView1.Columns[0].HeaderText = "Year"; dataGridView1.Columns[1].HeaderText = "Future Value"; double pv, rate, lastYear, fv; pv = double.Parse(textBox1.Text); rate = double.Parse(textBox2.Text); lastYear = double.Parse(textBox3.Text); dataGridView1.Rows.Clear(); for (int year = 1; year <= lastYear; year++) { dataGridView1.Rows.Add(); fv = pv * Math.Pow(1 + rate, year); dataGridView1.Rows[year - 1].Cells[0].Value = year; dataGridView1.Rows[year - 1].Cells[1].Value = fv.ToString("c"); }

6 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; }

7 Alternating Row Color dataGridView1.ColumnCount = 2; dataGridView1.Columns[0].HeaderText = "Year"; dataGridView1.Columns[1].HeaderText = "Future Value"; double pv, rate, lastYear, fv; pv = double.Parse(textBox1.Text); rate = double.Parse(textBox2.Text); lastYear = double.Parse(textBox3.Text); dataGridView1.Rows.Clear(); for (int year = 1; year <= lastYear; year++) { dataGridView1.Rows.Add(); fv = pv * Math.Pow(1 + rate, year); dataGridView1.Rows[year - 1].Cells[0].Value = year; dataGridView1.Rows[year - 1].Cells[1].Value = fv.ToString("c"); if (year % 2 == 0) dataGridView1.Rows[year - 1].DefaultCellStyle.BackColor = Color.AliceBlue; else dataGridView1.Rows[year - 1].DefaultCellStyle.BackColor = Color.Pink; }

8 Exiting a loop using the break statement In some cases it is necessary to end a loop before the test condition would end it Use the break statement: break;

9 Example while (1 > 0) { MessageBox.Show("Looping"); if (MessageBox.Show("Do you want to continue?", "Loop demo", MessageBoxButtons.YesNo) == DialogResult.No) { MessageBox.Show("Leaving loop"); break; }

10 Years to Reach Targeted Future Value Future Value=Present Value*(1+Rate) Year Note: When year=0, future value=present value

11 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();

12 Method 2 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 (true) { if (futureValue >= Target) break; YearToTarget = YearToTarget + 1; futureValue = PresentValue * Math.Pow(1 + Rate, YearToTarget); } textBox4.Text = YearToTarget.ToString();

13 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; true; YearToTarget++) { futureValue = PresentValue * Math.Pow(1 + Rate, YearToTarget); if (futureValue >= Target) break; } textBox4.Text = YearToTarget.ToString(); } Note : for (YearToTarget = 0; true; YearToTarget++)

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

15 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

16 Using dataGridView

17 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; }

18 Flexible rates and years

19 double startRate, endRate, rateIncrement, presentValue, futureValue; int startYear, endYear, yearIncrement; presentValue = double.Parse(textBox1.Text); startRate = double.Parse(textBox2.Text); endRate = double.Parse(textBox3.Text); rateIncrement = double.Parse(textBox4.Text); startYear = int.Parse(textBox5.Text); endYear = int.Parse(textBox6.Text); yearIncrement = int.Parse(textBox7.Text); int yearColumns = (endYear - startYear) / yearIncrement + 1; dataGridView1.ColumnCount = yearColumns + 1; dataGridView1.Columns[0].HeaderText = "Rate/Year"; int colIndex=1; for (int year = startYear;year<=endYear;year+=yearIncrement) { dataGridView1.Columns[colIndex].HeaderText = year.ToString(); ++colIndex; } int rowIndex = 0; dataGridView1.Rows.Clear(); for (double rate = startRate; rate<= endRate; rate += rateIncrement) { dataGridView1.Rows.Add(); colIndex = 0; dataGridView1.Rows[rowIndex].Cells[colIndex].Value = rate.ToString("p2"); for (int year = startYear; year <= endYear; year += yearIncrement) { colIndex += 1; futureValue = presentValue * Math.Pow(1 + rate, year); dataGridView1.Rows[rowIndex].Cells[colIndex].Value = futureValue.ToString("c"); } rowIndex += 1; }

20

21 Code Example int rows = 9, cols = 10, Product; dataGridView1.ColumnCount = cols; dataGridView1.Columns[0].HeaderText = ""; dataGridView1.Columns[0].Width = 25; for (int col = 2; col <= cols; col++) { dataGridView1.Columns[col-1].HeaderText = (col-1).ToString(); dataGridView1.Columns[col-1].Width = 25; } for (int rowIndex = 1; rowIndex <= rows; rowIndex++) { dataGridView1.Rows.Add(); dataGridView1.Rows[rowIndex - 1].Cells[0].Value = rowIndex.ToString(); for (int colIndex = 2; colIndex <= cols; colIndex++) { Product= rowIndex * (colIndex-1); dataGridView1.Rows[rowIndex - 1].Cells[colIndex-1].Value = Product.ToString(); }


Download ppt "Loop Continue ISYS 350. Straight Line Depreciation Table."

Similar presentations


Ads by Google