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.

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.
Objectives In this chapter, you will learn about:
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
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.
Chapter 61 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.
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.
Chapter 61 Flags A flag is a variable that keeps track of whether a certain situation has occurred. The data type most suited to flags is Boolean.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 6 - Visual Basic Schneider
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Repetition  Some additional operators increment and decrement.
©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.
1 Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops 6.3 List Boxes and Loops.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
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.
CPS120 Introduction to Computer Science Iteration (Looping)
Programming Logic and Design Fifth Edition, Comprehensive
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Chapter 5 - VB 2005 by Schneider1 Chapter 5 – Decisions 5.1 Relational and Logical Operators 5.2 If Blocks.
Programming Logic and Design Sixth Edition Chapter 5 Looping.
1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
Compunet Corporation1 Programming with Visual Basic.NET While, Do and For – Next Loops Week 5 Tariq Ibn Aziz.
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.
Chapter 4 Looping Statements Adapted From: Starting Out with Visual Basic 2008 (Pearson)
R EPETITION 1 Chapter 6. C HAPTER 6 – R EPETITION 6.1 Do Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops 6.4 A Case Study: Analyze.
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
Repetition Structures
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:
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
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.
For…Next and Do...While Loops! Happy St. Patrick’s Day!
Controlling Program Flow with Looping Structures
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
Fourth Quarter.  Involves loops or cycles ◦ Loops: means that a process may be repeated as long as certain condition remains true or remains false. ◦
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Dani Vainstein1 VBScript Session 5. Dani Vainstein2 What we learn last session? Branching Branching using If … Then … Else statement. Branching using.
Learning Javascript From Mr Saem
Introduction to Computer CC111 Week 10 Visual Basic 3 1.
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.
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.
Lecture 07 More Repetition Richard Gesick.
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
PROGRAM FLOWCHART Iteration Statements.
Conditional Loops Counted Loops
Presentation transcript:

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 Schneider2 5.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 5 - VB 2008 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 5 - VB 2008 by Schneider4 Pseudocode and Flow Chart for a Do Loop

Chapter 5 - VB 2008 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 += 1 'Add 1 to the value of num Loop End Sub

Chapter 5 - VB 2008 by Schneider6 Example: Repeat Request as Long as Response in Incorrect Dim passWord As String = "" Do While passWord <> “AAAAAA" passWord = InputBox("What is the password?") passWord = passWord.ToUpper Loop passWord is the loop control variable because the value stored in passWord is what is tested to determine if the loop should continue or stop.

Chapter 5 - VB 2008 by Schneider7 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 frue, the statements following the Loop statement are executed.

Chapter 5 - VB 2008 by Schneider8 Example: Repeat Request Until Proper Response is Given Do passWord = InputBox("What is the password?") passWord = passWord.ToUpper Loop Until passWord = “AAAAA"

Chapter 5 - VB 2008 by Schneider9 Pseudocode and Flowchart for a Post-Test Loop

Chapter 5 - VB 2008 by Schneider10 Example 4: Form txtAmount txtWhen

Chapter 5 - VB 2008 by Schneider11 Example 4: Code Private Sub btnCalculate_Click(...) Handles btnCalculate.Click Dim balance As Double, numYears As Integer balance = CDbl(txtAmount.Text) Do While balance < balance += 0.05 * balance numYears += 1 Loop txtWhen.Text = "In " & numYears & _ " years you will have a million dollars." End Sub

Chapter 5 - VB 2008 by Schneider12 Example 4: Output

Chapter 5 - VB 2008 by Schneider13 Comments Be careful to avoid infinite loops – loops that never end. Visual Basic allows for the use of either the While keyword or the Until keyword at the top or the bottom of a loop. This textbook will use only While at the top and only Until at the bottom.

Chapter 5 - VB 2008 by Schneider14 Nested Loops Statements inside a loop can contain another loop.

Chapter 5 - VB 2008 by Schneider15 More About Flags When flagVar is a variable of Boolean type, the statements If flagVar = True Then and If flagVar = False Then can be replaced by If flagVar Then and If Not flagVar Then

Chapter 5 - VB 2008 by Schneider16 Flags continued The statements Do While flagVar = True and Do While flagVar = False can be replaced by Do While flagVar and Do While Not flagVar

Chapter 5 - VB 2008 by Schneider For…Next Loops Nested For … Next Loops Local Type Inference

Chapter 5 - VB 2008 by Schneider18 For…Next Loops Used when we know how many times we want the loop to execute A counter controlled loop

Chapter 5 - VB 2008 by Schneider19 Sample For i As Integer = 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 5 - VB 2008 by Schneider20 Similar Do While Loop i = 1 Do While i <= 5 lstTable.Items.Add(i & " " & i ^ 2) i += 1 Loop

Chapter 5 - VB 2008 by Schneider21 For…Next Loop Syntax

Chapter 5 - VB 2008 by Schneider22 Example 1: Output

Chapter 5 - VB 2008 by Schneider23 Example 1: Code Dim pop as Double = Dim fmtStr As String = "{0,4}{1,12:N0}" For yr As Integer = 2008 To 2012 lstPop.Items.Add(String.Format( _ fmtStr, yr, pop) pop += 0.03 * pop Next

Chapter 5 - VB 2008 by Schneider24 Example 2 For i As Integer = 0 To n Step s lstValues.Items.Add(i) Next Control variable Start value Stop value Amount to add to i Data type

Chapter 5 - VB 2008 by Schneider25 Example with Negative Step For j As Integer = 10 To 1 Step -1 lstBox.Items.Add(j) Next lstBox.Items.Add("Blastoff")

Chapter 5 - VB 2008 by Schneider26 Example: Nested Loops 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 5 - VB 2008 by Schneider27 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 5 - VB 2008 by Schneider28 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 5 - VB 2008 by Schneider29 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 5 - VB 2008 by Schneider30 Non-integer Step Values Can lead to round-off errors with the result that the loop is not executed the intended number of times. We will only use Integers for all values in the header.