80 likes | 260 Views
Tutorial 6 The Repetition Structure. 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
E N D
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
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
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)
The Do…Loop Statement • 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
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 Using Counters and Accumulators