Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tutorial 6: The Repetition Structure1 Tutorial 6 The Repetition Structure.

Similar presentations


Presentation on theme: "Tutorial 6: The Repetition Structure1 Tutorial 6 The Repetition Structure."— Presentation transcript:

1 Tutorial 6: The Repetition Structure1 Tutorial 6 The Repetition Structure

2 Tutorial 6: The Repetition Structure2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code the repetition structure using the For…Next and Do…Loop statements  Write pseudocode for the repetition structure  Create a flowchart for the repetition structure  Display a message in the Output window while an application is running  Change the location and size of a control while an application is running  Initialize and update counters and accumulators

3 Tutorial 6: The Repetition Structure3 The Repetition Structure - Loops  Programmers use the repetition structure, referred to more simply as a loop, when they need the computer to repeatedly process one or more program instructions.  In a pretest loop, the evaluation occurs before the instructions within the loop are processed  In a posttest loop, the evaluation occurs after the instructions within the loop are processed

4 Tutorial 6: The Repetition Structure4 The For … Next Loop  You can use the For…Next statement for processing a precise number of times  Syntax: For counter = startValue To endValue [Step stepValue] [instructions you want repeated] Next  counter is the name of a numeric variable and it keeps track of how many times the loop instructions are repeated  startValue, endValue, and stepValue must be numeric and they can be either positive or negative, integer or non- integer (default stepValue is 1)

5 Tutorial 6: The Repetition Structure5 For … Next Examples Dim intCount As Integer For intCount = 0 to 3 Debug.WriteLine(intCount) Next Dim intCount As Integer For intCount = 3 to 0 Step -1 Debug.WriteLine(intCount) Next Dim intCount As Integer For intCount = 0 to 10 Step 2 Debug.WriteLine(intCount) Next Dim sngLoc As Single For sngLoc = 0.5 To 15 Step 0.5 Debug.WriteLine(sngLoc) Next

6 Tutorial 6: The Repetition Structure6 The Do…Loop Statement Pretest condition Do While Condition [loop instructions] Loop Do Until Condition [loop instructions] Loop Posttest condition Do [loop instructions] Loop While Condition Do [loop instructions] Loop Until Condition Unlike the For…Next statement, the Do…Loop statement can be used for processing a conditional number of times – pretest or posttest The Do…Loop statement begins with the Do clause and ends with the Loop clause

7 Tutorial 6: The Repetition Structure7 Do … Loop Examples Dim intCount As Integer = 1 Do While intCount < 3 Debug.WriteLine(intCount) intCount = intCount + 1 Loop Dim intCount As Integer = 1 Do Until intCount > 3 Debug.WriteLine(intCount) intCount = intCount + 1 Loop Dim intCount As Integer = 1 Do Debug.WriteLine(intCount) intCount = intCount + 1 Loop While intCount < 3 Dim intCount As Integer = 1 Do Debug.WriteLine(intCount) intCount = intCount + 1 Loop Until intCount > 3

8 Tutorial 6: The Repetition Structure8 Using Counters and Accumulators  Counters and accumulators are used within a repetition structure to calculate subtotals, totals, and averages  Initializing (usually to 0 or 1) means to assign a beginning value to the counter or accumulator before the loop  A counter is a numeric variable used for counting something and is typically updated by 1  An accumulator is a numeric variable used for accumulating (adding together) and is updated by an amount that varies


Download ppt "Tutorial 6: The Repetition Structure1 Tutorial 6 The Repetition Structure."

Similar presentations


Ads by Google