Loop Continue.

Slides:



Advertisements
Similar presentations
CSC 221: Computer Programming I Fall 2001  conditional repetition  repetition for: simulations, input verification, input sequences, …  while loops.
Advertisements

Loops (Part 1) Computer Science Erwin High School Fall 2014.
Do/Loops A loop repeats a series of instructions. An iteration is a single execution of the statement(s) in the loop. Used when the exact number of iterations.
Slide 1 VB Program Flow Control. Slide 2 Making Decisions v Decision Statement: control the execution of parts of the program based on conditions. v The.
Loops ISYS 350. Three Types of Loops while loop do while loop for loop.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
More Looping Structures
110-K1 Iterations (1) Up to now, need to call the procedure again (e.g. click again on a command button) To repeat a piece of code: Can also use loops:
Lecture Set 5 Control Structures Part D - Repetition with Loops.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
ENGR 112 Decision Structures.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
Visual Basic.net Loops. Used to do multiple executions of the same block of code Do while loops Do until loops For next loops.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
Lab 4 Range Review, Control Logic and Loops ► Range Review ► Control Logic and Loops ► Exercise.
CSC115 Introduction to Computer Programming Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA 19383
Intro to Nested Looping Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
7-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
For…Next Loops, Checked List Boxes, and Combo Boxes Chapter 5.
Loop Continue ISYS 350. Straight Line Depreciation Table.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 16 I’m on the Inside; You’re on the Outside.
For…Next and Do...While Loops! Happy St. Patrick’s Day!
Lab 6 (1) Range Review, Control Logic and Loops ► Control Logic and Loops ► Exercise.
مقدمة في البرمجة 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.
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:
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:
Loop Blocks Chapter 6 Part A. Program Blocks 1.Actions- commands, messages, methods 2.Branches- decisions to be made 3.Loops- actions to be repeated.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 9: Chapter 5: Slide 1 Unit 9 Do Until and For… Next Loops Chapter 5 Lists,
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
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.
Chapter 5 - VB 2008 by Schneider1 Chapter 5 – Repetition 5.1 Do Loops 5.2 Processing Lists of Data with Do Loops 5.3 For...Next Loops.
Chapter 6 - VB 2008 by Schneider1 Chapter 6 – Repetition 6.1 Do Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops 6.4 A Case Study:
26/06/ Iteration Loops For … To … Next. 226/06/2016 Learning Objectives Define a program loop. State when a loop will end. State when the For.
CE En 270 Brigham Young University Norm Jones
Loops ISYS 350.
Lists, Loops, Validation, and More
Loops ISYS 350.
Topics The if Statement The if-else Statement Comparing Strings
JavaScript: Control Statements.
Incrementing ITP © Ron Poet Lecture 8.
Topics Introduction to Repetition Structures
3rd prep. – 2nd Term MOE Book Questions.
CSC115 Introduction to Computer Programming
Introducing Do While & Do Until Loops & Repetition Statements
حلقات التكرار.
Loops ISYS 350.
Loop Continue ISYS 350.
Chapter (3) - Looping Questions.
Iteration: Beyond the Basic PERFORM
CIS 16 Application Development Programming with Visual Basic
More Looping Structures
Loop Continue ISYS 350.
Do … Loop Until (condition is true)
Introduction to Problem Solving and Control Statements
Loops ISYS 350.
3.1 Iteration Loops For … To … Next 18/01/2019.
Loops ISYS 350.
Loop Continue ISYS 350.
Loops ISYS 350.
Loop Continue ISYS 350.
Topics Introduction to Repetition Structures
Loops ISYS 350.
Loop Continue ISYS 350.
Loop Continue ISYS 350.
Loop Continue ISYS 350.
GCSE Computing:: Selection (IF statements)
More Looping Structures
Presentation transcript:

Loop Continue

Exiting a Loop Prematurely In some cases it is necessary to end a loop before the test condition would end it The following statements accomplish this Exit Do (used in Do While or Until loops) Exit For (used in For Next loops)

Years to Reach Targeted Future Value Future Value=Present Value Years to Reach Targeted Future Value Future Value=Present Value*(1+Rate)Year

Method 1 Dim PresentValue, Rate, Target, YearToTarget, futureValue As Double PresentValue = TextBox1.Text Rate = TextBox2.Text Target = TextBox3.Text futureValue = PresentValue YearToTarget = 1 Do While futureValue < Target YearToTarget = YearToTarget + 1 futureValue = PresentValue * (1 + Rate) ^ YearToTarget Loop TextBox4.Text = YearToTarget.ToString

Method 2 Dim PresentValue, Rate, Target, YearToTarget, futureValue As Double PresentValue = TextBox1.Text Rate = TextBox2.Text Target = TextBox3.Text YearToTarget = 1 Do While true futureValue = PresentValue * (1 + Rate) ^ YearToTarget If futureValue >= Target Then Exit Do End If YearToTarget = YearToTarget + 1 Loop MessageBox.Show(YearToTarget.ToString)

Method 3 Dim PresentValue, Rate, Target, YearToTarget, futureValue As Double PresentValue = TextBox1.Text Rate = TextBox2.Text Target = TextBox3.Text For YearToTarget = 1 To 9999 futureValue = PresentValue * (1 + Rate) ^ YearToTarget If futureValue >= Target Then Exit For End If Next MessageBox.Show(YearToTarget.ToString)

Searching: If fruit not exists, then add to the combobox

Code Example: Using Flag Dim testItem As String Dim myIndex As Integer Dim foundFlag As Boolean = False testItem = ComboBox1.Text For myIndex = 0 To ComboBox1.Items.Count - 1 If ComboBox1.Items(myIndex) = testItem Then foundFlag = True Exit For End If Next If foundFlag = True Then MessageBox.Show("Items already exists") Else MessageBox.Show("Item not exists") ComboBox1.Items.Add(testItem)

Method 2: Using VB’s tools Dim testItem As String testItem = ComboBox1.Text If ComboBox1.Items.Contains(testItem) Then MessageBox.Show("Items already exists") Else MessageBox.Show("Item not exists") ComboBox1.Items.Add(testItem) End If

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

Nested Loop Example A clock is an example of a nested loop Minute hand repeats 60 times for each hour Second hand repeats 60 times for each minute Dim hours, minutes, seconds As Integer For hours = 0 To 24 For minutes = 0 To 59 For seconds = 0 To 59 MessageBox.Show("Hour: " + hours.ToString + " Minutes: " + minutes.ToString + " Seconds: " + seconds.ToString) Next seconds Next minutes Next hours

Nested Loop Dim i, j As Integer Dim outputLine As String For i = 1 To 4 outputLine = "" For j = 1 To 4 outputLine = outputLine + "(" + i.ToString + "," + j.ToString + ")" Next 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 1 2 3 4 5 6 7 8 9 1 01 02 03 04 05 06 07 08 09 2 02 04 06 08 10 12 14 16 18 3 03 06 09 12 15 18 21 24 27 4 04 08 12 16 20 24 28 32 36 5 05 10 15 20 25 30 35 40 45 6 06 12 18 24 30 36 42 48 54 7 07 14 21 28 35 42 49 56 63 8 08 16 24 32 40 48 56 64 72 9 09 18 27 36 45 54 63 72 81

Code Example Dim i, j, product As Integer Dim outputLine As String ListBox1.Items.Add(" 1 2 3 4 5 6 7 8 9") For i = 1 To 9 outputLine = i.ToString For j = 1 To 9 product = i * j outputLine = outputLine + " " + product.ToString("D2") Next 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 5 10 15 20 3.00% $1,159.27 $1,343.92 $1,557.97 $1,806.11 3.50% $1,187.69 $1,410.60 $1,675.35 $1,989.79 4.00% $1,216.65 $1,480.24 $1,800.94 $2,191.12 4.50% $1,246.18 $1,552.97 $1,935.28 $2,411.71 5.00% $1,276.28 $1,628.89 $2,078.93 $2,653.30

Code Example Dim rate, year, presentValue, futureValue As Double presentValue = TextBox1.Text Dim lineOutput As String ListBox1.Items.Add("Rate/Year 5 10 15 20") For rate = 0.03 To 0.05 Step 0.005 lineOutput = rate.ToString("p2") For year = 5 To 20 Step 5 futureValue = presentValue * (1 + rate) ^ year lineOutput = lineOutput + " " + futureValue.ToString("c") Next ListBox1.Items.Add(lineOutput) rate.ToString("p2")

Exercise 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 50000. Monthly payment table term 30 Loan rate 100,000 150,000 200,000 250,000 5% $536.82 $805.23 $1,073.64 $1,342.05 5.25% $552.20 $828.31 $1,104.41 $1,380.51 5.50% $567.79 $851.68 $1,135.58 $1,419.47 5.75% $583.57 $875.36 $1,167.15 $1,458.93 6% $599.55 $899.33 $1,199.10 $1,498.88