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:

Slides:



Advertisements
Similar presentations
Chapter 6 - VB 2005 by Schneider1 Do Loop Syntax Do While condition statement(s) Loop Condition is tested, If it is True, the loop is run. If it is False,
Advertisements

Looping Structures: Do Loops
CS 4 Intro to Programming using Visual Basic Do Loops Patchrawat Uthaisombut University of Pittsburgh 1 based on lecture notes by D. Schneider.
CS0004: Introduction to Programming Repetition – Do Loops.
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.
Iteration (Looping Constructs in VB) Iteration: Groups of statements which are repeatedly executed until a certain test is satisfied Carrying out Iteration.
Do Loops A Do..Loop terminates based on a condition that is specified Execution of a Do..Loop continues while a condition is True or until a condition.
Chapter 5: Control Structures II (Repetition)
Control Structures: Part 2. Introduction Essentials of Counter-Controlled Repetition For / Next Repetition Structure Examples Using the For / Next Structure.
More loops Linag, Chpt 3, pp The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition? loop-body.
5.05 Apply Looping Structures
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 9 Car Payment Calculator Application Introducing the Do While...Loop and Do Until...Loop.
© 1999, by Que Education and Training, Chapter 7, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
CIS 115 Lecture 8. There are 3 control structures common to most computer languages that determine the flow, or path of execution, of the code:  Sequential.
Chapter 12: How Long Can This Go On?
Chapter 6: Iteration Part 1. To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do While and Do Until Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops.
Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops.
Repetition Chapter 7. Overview u For Loop u Do Loop  Do While Loop  Do Until Loop  Do Loop While  Do Loop Until u Nested Loops.
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 2) UTPA – Fall 2011 Part of the slides is from Dr. John Abraham’s previous.
CONTROLLING PROGRAM FLOW
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 1B) UTPA – Fall 2011.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
Statements That Repeat. For...Next Loop Structure For counter = start To end Step increment statements Next counter Where Counter is tested to see if.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
CSC 162 Visual Basic I Programming. Repetition Structures Pretest Loop –Exit condition is tested before the body of code is executed Posttest Loop –Exit.
Visual Basic.net Loops. Used to do multiple executions of the same block of code Do while loops Do until loops For next loops.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Iterations Very Useful: Ability to repeat a block of code Example:
1 Chapter 9. To familiarize you with  Simple PERFORM  How PERFORM statements are used for iteration  Options available with PERFORM 2.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
CW-V1 SDD 0901 Principals of Software Design and Development Loops Starter: Water JugsWater Jugs.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Counting Loops.
Looping Structures Do Loops, For Next Do...Loop While structures check the condition after executing the code and repeat a code block until the test.
110 E-1 Variables, Constants and Calculations(2) Chapter 3: Operations on variables, scope of a variable, formatting data Doing Arithmetic.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?
Higher Computing Software Development -So Far- 5/10/10.
CSC 162 Visual Basic I Programming. Repetition Structures Pretest Loop –Exit condition is tested before the body of code is executed Posttest Loop –Exit.
Controlling Program Flow with Looping Structures
5b – For Loops Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
Input Boxes, List Boxes, and Loops Chapter 5. 2 Input Boxes Method for getting user’s attention to obtain input. InputBox() for obtaining input MessageBox()
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
BO65: PROGRAMMING ITERATION 1. Starter  Write down what you think the code below outputs on screen: Dim intCounter as integer intCounter = 0 Do while.
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 2B) UTPA – Fall 2011 Part of the slides is from Dr. John Abraham’s previous.
CPSC 233 Tutorial 5 February 2 th /3 th, Java Loop Statements A portion of a program that repeats a statement or a group of statements is called.
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.
1 VB-04-Control Structures 16 March 2016 Visual Basic Control Structures - Selection.
5a – While Loops Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
Chapter 6 Controlling Program Flow with Looping Structures.
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.
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
UNIT 5 Lesson 15 Looping.
Chapter 5- Control Structures: Part 2
Visual Basic 6 (VB6) Data Types, And Operators
Tutorial 10 – Class Average Application Introducing the Do…Loop While and Do…Loop Until Repetition Statements Outline Test-Driving the Class Average.
Repetition – For and Do While
Problem Solving and Control Statements: Part 2
Control Structures: Part 2
Programming Fundamentals
Introducing Do While & Do Until Loops & Repetition Statements
Chapter (3) - Looping Questions.
For loops intCount = 1 inCount = intCount + 1 True intCount
Case & Repetitive Statements
Prepared By: Deborah Becker
Repetition Statements (Loops) - 2
Presentation transcript:

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: Do Loops Do While/Loop, Do/Loop While Do Until/Loop, Do/Loop Until Chapter 7

110-K2 Do While/Loop Do While condition Statements Loop The loop body is executed as long as the condition is true Flow Chart Note: if the condition is FALSE, the body of the loop is NOT executed (not even once) The statements must change the value of the condition. If not, we have a never ending loop True False Statements condition ?

110-K3 Example Draw three circles on a form simple solution ' Create a Pen object. Dim objPen As Pen objPen =New Pen(Color.Red, 1) ‘Draw three circles Me.CreateGraphics.DrawEllipse(objPen, 0, 0, 100, 100) Me.CreateGraphics.DrawEllipse(objPen, 0, 110, 100, 100) Me.CreateGraphics.DrawEllipse(objPen, 0, 210, 100, 100) But what if we want a column of 10 circles? Use a loop

110-K4 Example Dim intCount, intYCoord As Integer intCount = 1 Do While intCount <= 10 ‘Draw a circle with new Y coordinate Me.CreateGraphics.DrawEllipse(objPen, 0, intYCoord, 50, 50) ‘update coordinates intYCoordinate += 55 ‘increment counter intCount += 1 Loop

110-K5 What is going on? 14 intCount Beginning of the loop End of the loop After the loop, intCount is 6 6 Dim intCount, intYCoord As Integer intCount = 1 Do While intCount < 6 ‘Draw a circle with new Y coordinate Me.CreateGraphics.DrawEllipse(objPen, 0, intYCoord, _ 50, 50) ‘update coordinates intYCoordinate += 55 ‘increment counter intCount += 1 Loop

110-K6 Another example Goal: Compute the time needed to double the amount of money on a 5% interest account With a Do While loop amount<goal No goal = 2*amount Yes 5% increase year = year + 1 get the amount year = 0

110-K7 In VB 'Money Amount Dim decAmount As Decimal 'Investment goal Dim decGoal As Decimal 'Number of years necessary Dim intYear As Integer 'Initialization decAmount = CDec(txtInput.Text) decGoal = 2*decAmount 'Loop Do While decAmount < decGoal decAmount = decAmount *decAmount intYear = intYear + 1 Loop 'Display the number of years lblYear.Text = FormatNumber(intYear)

110-K8 Other Do Loops(1) Do Statements Loop While condition The loop body is executed at least once and then as long as the condition is true. False Statements True condition ? Flow Chart

110-K9 Example Compare: And Dim intCount As Integer intCount = 1 Do While intCount < 1 intCount = intCount + 1 Loop lblDisplay.Text= "intCount = “& intCount Dim intCount As Integer intCount = 1 Do intCount = intCount + 1 Loop While intCount < 1 lblDisplay.Text= "intCount = “& intCount intCount = 1intCount = 2

110-K10 Also: Do Until / Loop and Do / Loop Until loop is executed as long as the condition is FALSE (and at least once for Do/Loop Until) Do Until condition Statements Loop The loop body is executed as long as the condition is false. Flow Chart e.g. Do Until/Loop Other Do Loops(2) False True Statements condition ?

110-K11 Example Problem: Starting from 1, how many consecutive integers do we need to add to get a sum greater than 200? Dim intNumber As Integer Dim intSum As Integer 'Compute … Do Until intSum>200 intNumber = intNumber + 1 intSum = intSum + intNumber Loop ‘Display the answer lblDisplay.Text= "intNumber = “& intNumber

110-K12 Loop Pitfalls intCount = 1 Do Until intCount = 10 intCount = intCount + 2 Loop What happens? Get a never ending loop intCount is never 10 intCount=1 intDoubleCount = 1 Do intDoubleCount= intCount*2 intCount = intCount + 1 Loop Until intCount <= 10 What happens? VB executes the Loop only once. Don’t confuse While and Until