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.

Slides:



Advertisements
Similar presentations
Chapter 6 - VB 2005 by Schneider1 Sample Dim i As Integer For i = 1 To 5 lstTable.Items.Add(i & " " & i ^ 2) Next The loop control variable, i, is Initialized.
Advertisements

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,
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.
Programming Logic and Design, Third Edition Comprehensive
Repeating Actions While and For Loops
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.
Executes a statement or statements for a number of times – iteration. Syntax for(initialize; test; increment) { // statements to be executed } Initial.
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
Chapter 6 - VB.Net by Schneider1 Chapter 6 – Repetition 6.1 Do Loops 6.2 Processing Lists of Data with Do Loops Peek Method Counters and Accumulators Flags.
Objectives You should be able to describe:
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 5: Repetition Statements. In this chapter, you will learn about: Basic loop structures while loops Interactive while loops for loops Loop programming.
Chapter 6 - Visual Basic Schneider
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Repetition Statements.
Iteration Conditional Loops Counted Loops. Charting the Flow of Control We’ve used flow charts to visualise the flow of control. The simplest form is.
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
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:
1 Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops 6.3 List Boxes and Loops.
CPS120 Introduction to Computer Science Iteration (Looping)
Programming Logic and Design Fifth Edition, Comprehensive
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.
Chapter 5 - VB 2005 by Schneider1 Chapter 5 – Decisions 5.1 Relational and Logical Operators 5.2 If Blocks.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice.
Compunet Corporation1 Programming with Visual Basic.NET While, Do and For – Next Loops Week 5 Tariq Ibn Aziz.
Statements That Repeat. For...Next Loop Structure For counter = start To end Step increment statements Next counter Where Counter is tested to see if.
Chapter 6 - Visual Basic Schneider 1 Chapter 6 Repetition.
CHAPTER SIX LOOPS © Prepared By: Razif Razali 1. FORMAT OR REFRESH!! WHAT HAVE WE LEARN? Differentiate between the types of selection structure? Which.
Chapter 71 Repetition - Do Loops n A Loops, is used to repeat a sequence of statements a number of time n There are two loops commands in Visual Basic.
Tutorial 6 The Repetition Structure
Chapter 4 Looping Statements Adapted From: Starting Out with Visual Basic 2008 (Pearson)
Visual Basic.net Loops. Used to do multiple executions of the same block of code Do while loops Do until loops For next loops.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
Chapter 6 - VB.Net by Schneider1 Chapter 6 – Repetition 6.1 Do Loops 6.2 Processing Lists of Data with Do Loops Peek Method Counters and Accumulators Flags.
6.2 For…Next Loops General Form of a For…Next Loop
1 Chapter 9. To familiarize you with  Simple PERFORM  How PERFORM statements are used for iteration  Options available with PERFORM 2.
CPS120 Introduction to Computer Science Iteration (Looping)
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Chapter 6 - VB 2005 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:
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Tutorial 6: The Repetition Structure1 Tutorial 6 The Repetition Structure.
Chapter 61 Example : For … To… Step For index = 0 To n Step s lstValues.Items.Add(index) Next Control variable Start value Stop value Amount to add to.
Controlling Program Flow with Looping Structures
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
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:
1 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: Analyze a Loan.
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.
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.
Chapter 3: Decisions and Loops
Programming Logic and Design Fourth Edition, Comprehensive
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
Outline Altering flow of control Boolean expressions
Iteration: Beyond the Basic PERFORM
Iteration: Beyond the Basic PERFORM
Chapter 8: More on the Repetition Structure
Chapter 6 - VB.Net by Schneider
Conditional Loops Counted Loops
REPETITION Why Repetition?
Presentation transcript:

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

Chapter 6 - VB 2005 by Schneider2 6.1 Do Loops A loop is one of the most important structures in programming. Used to repeat a sequence of statements a number of times. The Do loop repeats a sequence of statements either as long as or until a certain condition is true.

Chapter 6 - VB 2005 by Schneider3 Do Loop Syntax Do While condition statement(s) Loop Condition is tested, If it is True, the loop is run. If it is False, the statements following the Loop statement are executed. These statements are inside the body of the loop and are run if the condition above is True.

Chapter 6 - VB 2005 by Schneider4 Pseudocode and Flow Chart for a Do Loop

Chapter 6 - VB 2005 by Schneider5 Example 1 Private Sub btnDisplay_Click(...) _ Handles btnDisplay.Click 'Display the numbers from 1 to 7 Dim num As Integer = 1 Do While num <= 7 lstNumbers.Items.Add(num) num = num + 1 'Add 1 to the value of num Loop End Sub

Chapter 6 - VB 2005 by Schneider6 Post Test Loop Do statement(s) Loop Until condition Loop is executed once and then the condition is tested. If it is False, the loop is run again. If it is True, the statements following the Loop statement are executed.

Chapter 6 - VB 2005 by Schneider7 Example 2 Private Sub btnDisplay_Click(...) _ Handles btnDisplay.Click 'Display the numbers from 1 to 7 Dim num As Integer = 1 Do lstNumbers.Items.Add(num) num = num + 1 'Add 1 to the value of num Loop Until num > 7 End Sub

Chapter 6 - VB 2005 by Schneider8 Pseudocode and Flowchart for a Post-Test Loop

Chapter 6 - VB 2005 by Schneider9 6.3 For…Next Loops Declarations of Control Variables Nested For … Next Loops

Chapter 6 - VB 2005 by Schneider10 For…Next Loops Used when we know how many times we want the loop to execute A counter controlled loop

Chapter 6 - VB 2005 by Schneider11 Sample Dim i As Integer For i = 1 To 5 lstTable.Items.Add(i & " " & i ^ 2) Next The loop control variable, i, is Initialized to 1 Tested against the stop value, 5 Incremented by 1 at the Next statement

Chapter 6 - VB 2005 by Schneider12 Do While equivalent i = 1 Do While i <= 5 lstTable.Items.Add(i & " " & i ^ 2) i += 1 Loop

Chapter 6 - VB 2005 by Schneider13 For…Next Loop Syntax

Chapter 6 - VB 2005 by Schneider14 Example 2 For index = 0 To n Step s lstValues.Items.Add(index) Next Control variable Start value Stop value Amount to add to index

Chapter 6 - VB 2005 by Schneider15 Example with Negative Step Dim j As Integer For j = 10 To 1 Step -1 lstBox.Items.Add(j) Next lstBox.Items.Add("Blastoff")

Chapter 6 - VB 2005 by Schneider16 Declaration Inside For Statement Dim i As Integer For i = 1 To 5 lstTable.Items.Add(i & " " & i ^ 2) Next For i As Integer = 1 To 5 lstTable.Items.Add(i & " " & i ^ 2) Next

Chapter 6 - VB 2005 by Schneider17 Nested For…Next Loops

Chapter 6 - VB 2005 by Schneider18 Example: Seats on an Airplane For i As Integer = 65 To 70 For j As Integer = 1 To 25 lstBox.Items.Add(Chr(i) & j) Next OUTPUT: A1 A2 A3 : Inner loop Outer loop

Chapter 6 - VB 2005 by Schneider19 For and Next Pairs For and Next statements must be paired. If one is missing, the automatic syntax checker will complain with a wavy underline and a message such as “A ‘For’ must be paired with a ‘Next’.”

Chapter 6 - VB 2005 by Schneider20 Start, Stop, and Step values Consider a loop beginning with For i As Integer = m To n Step s. The loop will be executed exactly once if m equals n no matter what value s has. The loop will not be executed at all if m is greater than n and s is positive, or if m is less than n and s is negative.

Chapter 6 - VB 2005 by Schneider21 Altering the Control Variable The value of the control variable should not be altered within the body of the loop. Doing so might cause the loop to repeat indefinitely or have an unpredictable number of repetitions.

Chapter 6 - VB 2005 by Schneider22 Non-integer Step Values Can lead to round-off errors with the result that the loop is not executed the intended number of times.

Chapter 6 - VB 2005 by Schneider23 Non-integer Step Example A loop beginning with For i As Double = 1 To 2 Step 0.1 will be executed only 10 times instead of the intended 11 times. It should be replaced with For i As Double = _ 1 To 2.01 Step 0.1