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
© Paradigm Publishing, Inc Excel 2013 Level 2 Unit 1Advanced Formatting, Formulas, and Data Management Chapter 2Advanced Functions and Formulas.
Advertisements

Section 2 - Selection and Repetition. Equality and Relational Operators.
Computer Science 1620 Loops.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Loops ISYS 350. Three Types of Loops while loop do while loop for loop.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
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.
More Looping Structures
Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,
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?
Intro to More Controls in C#. C# Demonstration We already touched on labels and buttons Ad-hoc demo of controls – Textboxes Multiline – Checkbox – Radiobutton.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
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.
Server-Side Scripting with PHP ISYS 475. PHP Manual Website
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 5A Repetition (Concepts)
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.
Loops and Files. 5.1 The Increment and Decrement Operators.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
1 Arrays of Arrays An array can represent a collection of any type of object - including other arrays! The world is filled with examples Monthly magazine:
Loop Continue ISYS 350. Straight Line Depreciation Table.
Loops ISYS 350. Two Types of Loops while loop for loop.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
مقدمة في البرمجة Lecture 7. Write VB.net project using the for loop to calculate : 1- the sum of numbers from 1 to (A) numbers. 2- the sum of Odd numbers.
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.
Parts of the Clock Click on the minute hand. Click on the clock’s face.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with While loops.
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:
Loops ISYS 350. Two Types of Loops while loop for loop.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
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.
© 2016, Mike Murach & Associates, Inc.
Array ISYS 350.
Loops ISYS 350.
Two Dimensional Array Mr. Jacobs.
Loops ISYS 350.
Programming Interface Controls
Loops ISYS 350.
Loop Continue ISYS 350.
Loops ISYS 350.
CIS 16 Application Development Programming with Visual Basic
More Looping Structures
Loop Continue ISYS 350.
Loops ISYS 350.
Alternate Version of STARTING OUT WITH C++ 4th Edition
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.
More Loops Topics Counter-Controlled (Definite) Repetition
Array ISYS 350.
Loops ISYS 350.
Loop Continue ISYS 350.
Loop Continue ISYS 350.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Programming Interface Controls
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Loop Continue ISYS 350.
More Looping Structures
More Loops Topics Counter-Controlled (Definite) Repetition
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;

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

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); futureValue = PresentValue; YearToTarget = 0; while (true) { if (futureValue >= Target) break; YearToTarget = YearToTarget + 1; futureValue = PresentValue * Math.Pow(1 + Rate, YearToTarget); } 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(); }

Accumulator Find the first N when the sum of all numbers between 1 and N is greater than a target value Method 1:

int Target, Sum, Num; Target = int.Parse(textBox1.Text); Sum =0; Num = 0; while (Sum<=Target) { ++Num; Sum += Num; } MessageBox.Show(Num.ToString()); Sum = 0; Num = 0; while (true) { ++Num; Sum += Num; if (Sum > Target) { MessageBox.Show(Num.ToString()); break; } } Sum = 0; for (Num=1;Num<=999999;Num++) { Sum += Num; if (Sum > Target) { MessageBox.Show(Num.ToString()); break; }

Allow user to enter password three times to view a picture

Button Click Event Code string goodPassword = "Hello", enteredPswd; int i; for (i = 1; i <= 3; i++) { enteredPswd = Interaction.InputBox("Enter password: "); if (enteredPswd == goodPassword) { pictureBox1.Visible = true; button1.Enabled = false; break; } if (i < 3) MessageBox.Show("Incorrect! Try again."); else { MessageBox.Show("You have tried three times already!"); button1.Enabled = false; }

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

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

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

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

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

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

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

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