Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next 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

CS 4 Intro to Programming using Visual Basic Do Loops Patchrawat Uthaisombut University of Pittsburgh 1 based on lecture notes by D. Schneider.
Programming with Microsoft Visual Basic th Edition
CS0004: Introduction to Programming Repetition – Do Loops.
Repeating Actions While and For Loops
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
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.
An Introduction to Programming with C++ Fifth Edition
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.
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
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.
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 - 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.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice.
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)
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Visual Basic Programming
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Class Average Application Introducing the Do...Loop While and Do...Loop Until.
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.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
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:
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Tutorial 6: The Repetition Structure1 Tutorial 6 The Repetition Structure.
Controlling Program Flow with Looping Structures
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Fourth Quarter.  Involves loops or cycles ◦ Loops: means that a process may be repeated as long as certain condition remains true or remains false. ◦
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Looping Structures. A B dialog box that pops up and prompts the user for input Text area that pops up and prompts the user for to wait while it gets.
Introduction to Computer CC111 Week 10 Visual Basic 3 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.
UNIT 5 Lesson 15 Looping.
REPETITION CONTROL STRUCTURE
while Repetition Structure
Lists, Loops, Validation, and More
Chapter 5: Repetition Structures
( Iteration / Repetition / Looping )
Chapter 4 – Control Structures Part 1
Programming Fundamentals
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Lecture 07 More Repetition Richard Gesick.
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
Lecture 4B More Repetition Richard Gesick
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Control Structure Senior Lecturer
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Alternate Version of STARTING OUT WITH C++ 4th Edition
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
CIS 16 Application Development Programming with Visual Basic
Introduction to Problem Solving and Control Statements
Let’s all Repeat Together
Chapter 6 - VB.Net by Schneider
Chapter 4: Repetition Structures: Looping
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Presentation transcript:

Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops 6.3 List Boxes and Loops

Do Loops Pretest Form of a Do Loop Posttest Form of a Do Loop

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.

Pretext Do 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.

Pseudocode and Flow Chart

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 Loop Test Loop Body Question #1: How many times will the test be done? Question #2: How many times will the body of the loop execute?

Example 1 (cont.) 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 Note: A ListBox’s Items property contains a collection (list) of data items. You can programmatically place items in the list using the Add method.

Example: Repeat Request as Long as Response in Incorrect Dim passWord As String = "" 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.

Example: Sentinel-Controlled Loop Dim num As Double = 0 Dim prompt As String = "Enter a nonnegative number. Enter -1 " & "to terminate entering numbers." num = CDbl(InputBox(prompt)) Do While num <> -1 '-1 is sentinel value . Loop First input precedes the loop A sentinel value is a special value that is used to decide that a loop should terminate Each subsequent input is in the last statement of the loop body

Example 3 (cont): The full loop body calculating average The loop body statements do the following: Count user inputs Accumulate the sum of numbers together Input the user’s next input value

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.

Example 3 (cont): The full loop body calculating average After the loop is completed, the average can be determined by dividing the sum by the count.

Posttest Do 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.

Example: Repeat Request Until Proper Response Dim passWord As String = "" Do passWord = InputBox("What is the password?") passWord = passWord.ToUpper Loop Until passWord = "SHAZAM" This is a BOTTOM-tested loop. In this case the loop body executes first, and then the test is done. So the number of tests EQUALS the number of body-executions. Unlike a TOP-tested loop, in which the number of tests is always one more than the number of body-executions.

Pseudocode and Flowchart

Example 5: Form txtAmount txtWhen

Example 5: Code Private Sub btnCalculate_Click(...) Handles _ Dim balance As Double, numYears As Integer balance = CDbl(txtAmount.Text) Do While balance < 1000000 balance += 0.06 * balance numYears += 1 Loop txtWhen.Text = "In " & numYears & " years you will have a million dollars." End Sub Question 1: what’s the accumulator variable? Question 2: what’s the counter variable?

Example 5: Output