Chapter 6 - VB.Net by Schneider

Slides:



Advertisements
Similar presentations
While loops.
Advertisements

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.
CHAPTER 5: LOOP STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Programming Logic and Design, Third Edition Comprehensive
Objectives In this chapter, you will learn about:
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.
Types of LOOP Structures Do While ……. Loop Do Until …… Loop For …… Next loop.
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.
An Object-Oriented Approach to Programming Logic and Design Chapter 6 Looping.
Chapter 6 - Visual Basic Schneider
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
1 Chapter 6 Repetition. 2 Outline & Objectives Loop Structure Loop Structure Elements of a Loop Structure Elements of a Loop Structure Processing Lists.
1 Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops 6.3 List Boxes and Loops.
Processing Lists of Data with Do Loops (Examples)
Lecture Set 5 Control Structures Part D - Repetition with Loops.
6.3 List Boxes and Loops Some Properties, Methods, and Events of List Boxes List Boxes Populated with Strings List Boxes Populated with Numbers Searching.
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.
Programming Logic and Design Fifth Edition, Comprehensive
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
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.
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 6: The Repetition Structure
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.
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.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
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 Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -
1 Chapter 9. To familiarize you with  Simple PERFORM  How PERFORM statements are used for iteration  Options available with PERFORM 2.
Chapter 6: Sections 6.2, 6.3 Roxana Gheorghiu Friday, April 1, 2011.
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:
Chapter Looping 5. The Increment and Decrement Operators 5.1.
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
Chapter 5: Looping. Using the while Loop Loop – A structure that allows repeated execution of a block of statements Loop body – A block of statements.
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.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al-Duwais1.
UNIT 5 Lesson 15 Looping.
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Programming Logic and Design Fourth Edition, Comprehensive
Loop Structures.
CE 311 K - Introduction to Computer Methods Daene C. McKinney
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Outline Altering flow of control Boolean expressions
Iteration: Beyond the Basic PERFORM
Iteration: Beyond the Basic PERFORM
CIS 16 Application Development Programming with Visual Basic
Chapter 6: Repetition Statements
Topics Introduction to Repetition Structures
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Conditional Loops Counted Loops
Presentation transcript:

Chapter 6 - VB.Net by Schneider Chapter 6 – Repetition 6.1 Do Loops 6.2 Processing Lists of Data with Do Loops Peek Method Counters and Accumulators Flags Nested Loops 6.3 For...Next Loops 6.4 A Case Study: Analyze a Loan Chapter 6 - VB.Net by Schneider

Chapter 6 - VB.Net by Schneider 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.Net by Schneider

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. Do While condition statement(s) Loop These statements are inside the body of the loop and are run if the condition above is True. A Do statement precedes the sequence of statements, and a Loop statement follows the sequence of statements. The condition, preceded by either the word “While” or the word “Until”, follows the word “Do” or the word “Loop”. Chapter 6 - VB.Net by Schneider

Pseudocode and Flow Chart for a Do Loop Chapter 6 - VB.Net by Schneider

Chapter 6 - VB.Net by Schneider 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 Num is the "loop control variable" because based on the value of num, the loop will either continue or stop. Note that the value of the loop control variable must change inside the loop. Chapter 6 - VB.Net by Schneider

Chapter 6 - VB.Net by Schneider Example 2 passWord = "" Do While passWord <> "SHAZAM" 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 6 - VB.Net by Schneider

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. If condition is true, then the program continues with the line after the Loop statement. If condition is false, then the entire process is repeated beginning with the Do statement. In other words, the statements inside the loop are executed once and then are repeatedly executed until the condition is true. Chapter 6 - VB.Net by Schneider

Chapter 6 - VB.Net by Schneider Example 3 Do passWord = InputBox("What is the password?") passWord = passWord.ToUpper Loop Until passWord = "SHAZAM" Chapter 6 - VB.Net by Schneider

Pseudocode and Flowchart for a Post-Test Loop Chapter 6 - VB.Net by Schneider

Chapter 6 - VB.Net by Schneider Comments Be careful to avoid infinite loops – loops that never end. VB.NET allows for the use of either the While keyword or the Until keyword at the top or the bottom of a loop. (This text will use only While at the top and only Until at the bottom.) Chapter 6 - VB.Net by Schneider

6.2 Processing Lists of Data with Do Loops Display all or selected items from lists Search lists for specific items Perform calculations on the numerical entries of a list Chapter 6 - VB.Net by Schneider

Chapter 6 - VB.Net by Schneider Terminology Counters calculate the number of elements in lists Accumulators sum numerical values in lists Flags record whether certain events have occurred Peek method can be used to determine when the end of a text file has been reached Chapter 6 - VB.Net by Schneider

Chapter 6 - VB.Net by Schneider Peek Method Data to be processed are often retrieved from a file by a Do loop To determine if we have reached the end of the file from which we are reading, use Peek Chapter 6 - VB.Net by Schneider

Chapter 6 - VB.Net by Schneider Peek Example a file has been opened as a StreamReader object named sr. sr.Peek is the ANSI value of the first character of the line about to be read with ReadLine. If the end of the file has been reached, the value of sr.Peek is -1 Chapter 6 - VB.Net by Schneider

Chapter 6 - VB.Net by Schneider Example 1 Dim sr As IO.StreamReader = _ IO.File.OpenText("PHONE.TXT") lstNumbers.Items.Clear() Do While sr.Peek <> -1 name = sr.ReadLine phoneNum = sr.ReadLine lstNumbers.Items.Add(name & " " _ & phoneNum) Loop sr.Close() Chapter 6 - VB.Net by Schneider

Pseudocode and Flow Chart for Processing Data from a File Chapter 6 - VB.Net by Schneider

Chapter 6 - VB.Net by Schneider Example 2 Do While (name <> txtName.Text) _ And (sr.Peek <> -1) name = sr.ReadLine phoneNum = sr.ReadLine Loop As long as the name being searched for has not been found AND the end of the file has not been reached, the loop will continue Chapter 6 - VB.Net by Schneider

Counters and Accumulators A counter is a numeric variable that keeps track of the number of items that have been processed. An accumulator is a numeric variable that totals numbers. Chapter 6 - VB.Net by Schneider

Chapter 6 - VB.Net by Schneider Example 3 numCoins = 0 sum = 0 Do While sr.Peek <> -1 coin = sr.ReadLine numCoins += 1 sum += CDbl(coin) Loop Sum is an accumulator. It is used to total up the values of the coins. numCoins is a counter, it increases by 1 each time through the loop Chapter 6 - VB.Net by Schneider

Chapter 6 - VB.Net by Schneider 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. Chapter 6 - VB.Net by Schneider

Chapter 6 - VB.Net by Schneider Example 4 Do While (sr.Peek <> -1) word2 = sr.ReadLine wordCounter += 1 If word1 > word2 Then orderFlag = False End If word1 = word2 Loop This program counts the number of words in the file WORDS.TXT and then reports whether the words are in alphabetical order. In each execution of the loop, a word is compared to the next word in the list. The flag variable, called orderFlag, is initially assigned the value True and is set to False if a pair of adjacent words is out of order. Chapter 6 - VB.Net by Schneider

Chapter 6 - VB.Net by Schneider Nested Loops Statements inside a loop can contain another loop. Chapter 6 - VB.Net by Schneider

Chapter 6 - VB.Net by Schneider Example 5 Do While (foundFlag = False) And (sr1.Peek <> -1) fileName = sr1.ReadLine Dim sr2 As IO.StreamReader = _ IO.File.OpenText(fileName) Do While (name <> txtName.Text) And _ (sr2.Peek <> -1) name = sr2.ReadLine phoneNum = sr2.ReadLine Loop sr2.Close() If name = txtName.Text Then txtNumber.Text = name & " " & phoneNum foundFlag = True End If Outer loop Inner loop Chapter 6 - VB.Net by Schneider

Chapter 6 - VB.Net by Schneider 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 If Not flagVar Then Chapter 6 - VB.Net by Schneider

Chapter 6 - VB.Net by Schneider Flags continued The statements Do While flagVar = True and Do While flagVar = False can be replaced by Do While flagVar Do While Not flagVar Chapter 6 - VB.Net by Schneider

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

Chapter 6 - VB.Net by Schneider Sample 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.Net by Schneider

Chapter 6 - VB.Net by Schneider Do While equivalent i = 1 Do While i <= 5 lstTable.Items.Add(i & " " & i ^ 2) i += 1 Loop Note how much more simple the For loop is Chapter 6 - VB.Net by Schneider

Chapter 6 - VB.Net by Schneider For…Next Loop Syntax Chapter 6 - VB.Net by Schneider

Chapter 6 - VB.Net by Schneider Example 1 lstTable.Items.Clear() For yr = 2002 To 2006 lstTable.Items.Add(String.Format _ (fmtStr, yr, pop)) pop+= 0.03 * pop Next Yr is the control variable 2002 is the start value 2006 is the stop value Yr will keep being increased by 1 at the Next statement until yr is > 2006 Chapter 6 - VB.Net by Schneider

Chapter 6 - VB.Net by Schneider Example 2 Start value Control variable Stop value Amount to add to index For index = 0 To n Step s lstValues.Items.Add(index) Next Start value must be less than or equal to the stop value when there is a positive step value Chapter 6 - VB.Net by Schneider

Chapter 6 - VB.Net by Schneider Example 3 For j = m–1 To 0 Step -1 temp &= info.Substring(j, 1) Next Start value must be >= the stop value when there is a NEGATIVE step value Chapter 6 - VB.Net by Schneider

Chapter 6 - VB.Net by Schneider Nested For…Next Loops You can put a loop inside a loop, but the inner loop must be fully contained inside the outer loop Chapter 6 - VB.Net by Schneider

Chapter 6 - VB.Net by Schneider Example 4 For j = 1 To 3 row = "" For k = 1 To 3 entry = j & " x " & k & " = " & (j * k) row &= entry & " " Next lstTable.Items.Add(row) Outer loop Inner loop Chapter 6 - VB.Net by Schneider

Chapter 6 - VB.Net by Schneider Example 4 Output Chapter 6 - VB.Net by Schneider

Chapter 6 - VB.Net by Schneider 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.Net by Schneider

Start, Stop, and Step values Consider a loop beginning with For i = 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.Net by Schneider

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.Net by Schneider

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.Net by Schneider

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