Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops

Slides:



Advertisements
Similar presentations
Chapter 6: The Repetition Structure
Advertisements

Programming with Microsoft Visual Basic th Edition
CS0004: Introduction to Programming Repetition – Do Loops.
Programming Logic and Design, Third Edition Comprehensive
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.
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
Chapter 6 - Visual Basic Schneider
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
5.05 Apply Looping Structures
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?
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
Programming Logic and Design Sixth Edition Chapter 5 Looping.
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.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
ISAT 252 Visual Basic Repetition. Assignment Should have read Chapter 5 ( ) on loops Do tutorial 5-4.
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.
Tutorial 6 The Repetition Structure
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.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
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
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:
Controlling Program Flow with Looping Structures
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
Introduction to Computer CC111 Week 10 Visual Basic 3 1.
COMPUTER PROGRAMMING I 5.05 Apply 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.
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
Loop Structures.
Lists, Loops, Validation, and More
Lecture 07 More Repetition Richard Gesick.
Lecture 4B More Repetition Richard Gesick
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
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.
Outline Altering flow of control Boolean expressions
The List Box Control Items can be placed into the list at design time or run time The Sorted property causes items in the list to be sorted automatically.
Iteration: Beyond the Basic PERFORM
Iteration: Beyond the Basic PERFORM
CIS 16 Application Development Programming with Visual Basic
Introduction to Problem Solving and Control Statements
Chapter 6: Repetition Statements
Chapter 6 - VB.Net by Schneider
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

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

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.

Pretest 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

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 3: 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

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 Until 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"

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

Example 5: Output

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.

6.2 For…Next Loops General Form of a For…Next Loop Step Keyword Nested For…Next Loops Local Type Inference

For…Next Loops Used when we know how many times we want the loop to execute A counter controlled loop

For…Next Loop Syntax

Sample The loop counter variable, i, is initialized to 1 For i As Integer = 1 To 5 lstTable.Items.Add(i & " " & i ^ 2) Next The loop counter variable, i, is initialized to 1 tested against the stop value, 5 incremented by 1 at the Next statement

Similar Do While Loop Dim i As Integer = 1 Do While i <= 5 lstTable.Items.Add(i & " " & i ^ 2) i += 1 Loop

Example 1: Output

Example 1: Code Dim pop As Double = 300000 For yr As Integer = 2012 To 2016 lstTable.Items.Add(yr & " " & pop.ToString("N0") pop += 0.03 * pop Next

Step Keyword Normally after each pass the value of the counter variable increases by 1 If Step s is appended to the For statement, the value of s will be added to the counter variable after each pass. If the value of s is a negative number, the value of the counter variable will decrease after each pass.

Example with Negative Step Value For j As Integer = 10 To 1 Step -1 lstBox.Items.Add(j) Next lstBox.Items.Add("Blastoff")

Example: Nested For…Next 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 : Outer loop Inner loop

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

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.

Altering the Counter Variable The value of the counter 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.

Non-Integer Step Values Can lead to rounding 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.

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 an Ordered List

List Box Properties The total number of items in a list box is lstBox.Items.Count Note: Each item in lstBox is identified by an index number from 0 to lstBox.Items.Count – 1 The index number of the currently highlighted item is given by: lstBox.SelectedIndex

More List Box Properties lstBox.Items() is the list of items in the list box. The value of the item with an index of n is: lstBox.Items(n) The data type of the elements in the lstBox.Items() array is Object. To display the first element of lstBox.Items in a text box: txtBox.Text = CStr(lstBox.Items(0))

Currently Highlighted Item in a List Box The value of the currently highlighted item as a string can be obtained as lstBox.Text

The Sorted Property Items can be placed into the list at design time or run time The Sorted property causes items in the list to be ordered automatically as strings Caution: Numbers in a sorted list box will not necessarily be in increasing numerical order

Example 1: Form lstStates lstStates is filled at design time with the names of the U.S. states in the order they joined the union.

Example 1: Code Private Sub btnDisplay_Click(...) Handles _ 'Display last ten states to join the union Dim n As Integer = lstStates.Items.Count For i As Integer = (n - 1) To (n - 10) Step -1 lstLastTen.Items.Add(lstStates.Items(i)) Next End Sub

Example 1: Output

Example: Form lstAges txtAvgAge lstAges is filled at design time with the ages of the U.S. presidents when taking office.

Example: Code Private Sub btnCalculate_Click(...) Handles _ 'Calculate average age of presidents when assuming office Dim n As Integer = lstAges.Items.Count Dim sum As Double = 0 For i As Integer = 0 To (n – 1) sum += CDbl(lstAges.Items(i)) Next txtAvgAge.Text = (sum / n).ToString("N") End Sub

Example: Output

Searching an Ordered List of Strings A search often can be terminated earlier if the list is sorted. This is particularly the case when the sought-after item is not in the list. The search can be terminated when an item is reached that follows the sought-after item alphabetically.

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. Used in a loop to provides information to be used after loop terminates. Or, allows for the early termination of the loop.

Searching an Unsorted List A flag is used to indicate whether or not the sought-after item has been found. The flag variable is initially set to False and then set to True if and when the item is found.

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

More About Flags (continued) The statements Do While flagVar = True and Do While flagVar = False can be replaced by Do While flagVar Do While Not flagVar

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.