Classnote 8
Classnote 8
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
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.
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
intcount
1 >3
Print intcount
Stop
DO WHILE condition
[loop instructions]
LOOP
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
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
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