0% found this document useful (0 votes)
16 views

Classnote 8

The document discusses different types of repetition structures in programming including FOR NEXT loops, DO WHILE loops, and DO UNTIL loops. It provides examples and flowcharts for each type of loop, explaining how they work and when each is executed. Counters and accumulators are also described which are used within repetition structures.

Uploaded by

imashad97
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Classnote 8

The document discusses different types of repetition structures in programming including FOR NEXT loops, DO WHILE loops, and DO UNTIL loops. It provides examples and flowcharts for each type of loop, explaining how they work and when each is executed. Counters and accumulators are also described which are used within repetition structures.

Uploaded by

imashad97
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

PROGRAMMING LANGUAGES

Class Notes #8 Repetition Structure December 19, 2003

Program Structures
The three structures used in programs are:
• Sequence
• Selection
• Repetition

The sequence structure (or sequential processing) is the processing of instructions, one
after another, in the order in which the instructions are entered in the code window.
The selection structure (or decision structure) is when the program must make a decision
or comparison and based on the result of the decision or comparison, selects one of two
paths.
The repetition structure (also called looping or iteration) instructs the computer to repeat
one or more program instructions either a specified number of times or until some
condition (e.g. loop condition) is met.

Repetition Structure
There are three forms of the repetition structure:
• FOR NEXT
• DO WHILE
• DO UNTIL

1. The FOR NEXT LOOP


The FOR NEXT loop, also called an automatic counter loop, repeats a block of
statements a specified number of times. The syntax is given below:

FOR counter = startvalue TO endvalue [STEP stepvalue]


[instructions]
NEXT counter

In the syntax, counter is a numeric variable used by the loop to keep track of the number
of times the loop instructions are processed. The startvalue, endvalue, and stepvalue
must be numeric and they can be either positive or negative, integer or non-integer.

Table 1: Examples of the FOR NEXT loop


Correct Not Correct
FOR intcount = 1 TO 3 step 1 FOR intcount = 3 TO 1 step 1
FOR intcount = 3 TO 1 step –1 FOR intcount = 1 TO 3 step –1

Given below is an example and figure one illustrates the flow chart and pseudocode for
the example:
FOR intcount = 1 to 3 step 1
Print intcount
NEXT intcount

1
Flowchart Pseudocode

1. Declare intcount variable for the


Start
FOR NEXT loop
2. Repeat for intcount = 1 to 3 by 1
print intcount
Declare intcount variable for End repeat for intcount
the FOR NEXT loop

intcount

1 >3

Print intcount

Stop

Figure 1: Flowchart and pseudocode: FOR NEXT loop

2. The DO WHILE loop


The block of instructions is repeated while the condition is true. The syntax for the DO
WHILE loop is given below:

DO WHILE condition
[loop instructions]
LOOP

The condition may contain variables, constants, properties, functions, mathematical


operators, relational operators, and logical operators. As in the case of the
IF…THEN…ELSE statement, the condition must also evaluate to either True or False.
The DO WHILE loop is also known as the pretest loop because the loop evaluates the
condition before processing the loop instructions. An example of the DO WHILE loop is
given below along with the flowchart and pseudocode in figure two.

2
DIM intcount as integer
Intcount = 1
DO WHILE intcount <= 3
Print intcount
Intcount = intcount + 1
LOOP

Flowchart Pseudocode
1. Declare intcount variable
Start 2. Assign 1 to intcount
3. Repeat while incount <= 3
Display intcount
add 1 to intcount
end repeat while intcount <= 3
Declare intcount variable

intcount = 1

F
intcount <= 3

Stop
T

Display
intcount

intcount = intcount + 1

Figure 2: Flowchart and Pseudocode: DO WHILE loop

3
3. The DO UNTIL loop
The DO UNTIL loop repeats a block of instructions until a condition becomes true. The
syntax for the DO UNTIL loop is given below:

DO
[loop instructions]
LOOP UNTIL condition

As before, the condition can contain variables, constants, properties, functions,


mathematical operators, relational operators, and logical operators. As in the case of the
IF…THEN…ELSE statement, the condition must also evaluate to either True or False.
The DO UNTIL loop is also known as the posttest loop because the loop evaluates the
condition after processing the instructions within the loop. An example of the DO
UNTIL loop is given below along with the flowchart and pseudocode in figure three.

DIM intcount as integer


Intcount = 1
DO
Print intcount
Intcount = intcount + 1
LOOP UNTIL intcount > 3

Note: The DO WHILE loop may fail the condition and never execute the loop
instructions, whereas the DO UNTIL loop may fail the condition but the loop instructions
is executed for one cycle only.

4
Flowchart Pseudocode
1. Declare intcount variable
Start 2. Assign 1 to intcount
3. Repeat
Display intcount
add 1 to intcount
end repeat Until intcount > 3
Declare intcount variable

intcount = 1

Display
intcount

intcount = intcount + 1

T
intcount > 3

Stop
F

Figure 3: Flowchart and Pseudocode: DO UNTIL loop

Counters and Accumulators


Counters and accumulators are used within a repetition structure to calculate subtotals,
totals, and averages. A counter is a numeric variable used for counting something
(example: number of employees paid in a week). An accumulator is a numeric variable
used for accumulating (adding) something (example: total dollars amount of a week’s
payroll). Two tasks are associated with counters and accumulators: initializing and
updating. Initializing is to assign a beginning value to the counter or accumulator.
Updating (incrementing) means adding a number to the value stored in the counter or
accumulator. A counter is always incremented by a constant value, whereas an
accumulator is incremented by a value that varies.

You might also like