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:

Slides:



Advertisements
Similar presentations
While loops.
Advertisements

Programming with Microsoft Visual Basic 2008 Fourth Edition
Programming with Microsoft Visual Basic th Edition
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
Loops ISYS 350. Three Types of Loops while loop do while loop for loop.
Chapter 8 Using Repetition with Loops and Lists. Class 8: Loops and Lists Write Do loops to execute statements repeatedly Write For loops to execute statements.
JAVA Control Structures: Repetition. Objectives Be able to use a loop to implement a repetitive algorithm Practice, Practice, Practice... Reinforce the.
Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Loops ISYS 350. A Box of Chocolate Repeat this process until box is empty: – Take one chocolate from the box – Eat the chocolate – Box has more chocolate?
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
Lecture 16: Working with Complex Data Arrays. Double-Subscripted Arrays Commonly used to represent tables of values consisting of information arranged.
Multi-Dimensional Arrays Arrays that have more than one index: Example of differences between basic data types and arrays using integers: Basic integer:
Chapter 6: The Repetition Structure
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Loops ISYS 350. Compute the sum of a list of numbers: Example: 5, 2, 10, 8, 4 Process: Sum= 0 Get a number from the list Sum = Sum + the number Repeat.
Chapter 15 I’m on the Inside; You’re on the Outside (Nested Loops) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,
Array and ArrayList ISYS 350. Array An array allows you to store a group of items of the same type together. Processing a large number of items in an.
7-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Loop Continue ISYS 350. Straight Line Depreciation Table.
Loops ISYS 350. Two Types of Loops while loop for loop.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 16 I’m on the Inside; You’re on the Outside.
Visual Basic.NET BASICS Lesson 11 List Boxes, For Next Loops, and Label Settings.
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:
CS 161 Introduction to Programming and Problem Solving Chapter 17 Nested Loops Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
Loops ISYS 350. Two Types of Loops while loop for loop.
Introduction to Methods ISYS 350. Methods Methods can be used to break a complex program into small, manageable pieces – This approach is known as divide.
Loops ISYS 350. Two Types of Loops while loop for loop.
Loops ISYS 350. Write a Program that asks user to enter any numbers and displays the largest number Process: Largest = the first number Get the next number.
Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,
Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,
Array, ArrayList and List ISYS 350. Array An array allows you to store a group of items of the same type together. Processing a large number of items.
Nested Loops, Break/Continue. Nested Loops Nested loops : loop inside a loop – Loop through rows and columns – Loop through every word in every file –
ESC101: Introduction to Computing
Array ISYS 350.
Loops ISYS 350.
Two Dimensional Array Mr. Jacobs.
Tutorial 9 - Car Payment Calculator Application Introducing the while Repetition Statement Outline 9.1 Test-Driving the Car Payment Calculator Application.
Loops ISYS 350.
Incrementing ITP © Ron Poet Lecture 8.
IS 350 Loops.
Programming Interface Controls
Array ISYS 350.
Array ISYS 350.
Chapter 5 Repetition.
Array ISYS 350.
Loops ISYS 350.
Loop Continue ISYS 350.
Loops ISYS 350.
CIS 16 Application Development Programming with Visual Basic
Loop Continue ISYS 350.
Introduction to Problem Solving and Control Statements
Loops ISYS 350.
Lecture Set 10 Windows Controls and Forms
SELECTIONS STATEMENTS
Loops ISYS 350.
Loop Continue.
ECE 103 Engineering Programming Chapter 19 Nested Loops
Loop Continue ISYS 350.
Loops ISYS 350.
Loop Continue ISYS 350.
Loops ISYS 350.
Array ISYS 350.
Loops ISYS 350.
Loop Continue ISYS 350.
Loop Continue ISYS 350.
Programming Interface Controls
Loop Continue ISYS 350.
Presentation transcript:

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