Loop Continue ISYS 350
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;
Years to Reach Targeted Future Value Future Value=Present Value*(1+Rate) Year
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();
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();
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(); }
Searching: If fruit not exists, then add to the combobox
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); }
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); }
Straight Line Depreciation Table
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;
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; }
Nested Loop A Loop that is Inside Another Loop is Called a Nested Loop
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
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)
Nested Loop Multiplication Table
Code Example int rows=9, cols=9; string outputLine; listBox1.Items.Add(" "); 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
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 % $1, $1, $1, $1, % $1, $1, $1, $1, % $1, $1, $1, $2, % $1, $1, $1, $2, % $1, $1, $2, $2,653.30
Code Example double rate, year, presentValue, futureValue; presentValue = double.Parse(textBox1.Text); string lineOutput; listBox1.Items.Add("Rate/Year "); 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")
Using dataGridView
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; }
Exercise Monthly payment table term30 Loan rate100,000150,000200,000250,000 5%$ $ $1, $1, %$ $ $1, $1, %$ $ $1, $1, %$ $ $1, $1, %$ $ $1, $1, 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 Using listbox or using dataGridView to show the table