Presentation is loading. Please wait.

Presentation is loading. Please wait.

Loop Continue ISYS 350.

Similar presentations


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

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

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


Download ppt "Loop Continue ISYS 350."

Similar presentations


Ads by Google