- Simple selection control structure is easy to understand and self-defining. The whole statement is base on a condition and its veracity. When the statement or "test" is found to be true, a statement is executed, and if it is false, another test is given or the program continues to the next block. The structure is represented in pseudocode using the keywords IF, THEN, ELSE and ENDIF.
- Simple selection with null false branch is a variation of the simple IF structure. It is used when a task is performed only when a particular condition is true. If the condition is false, then no processing will take place and the IF statement will be bypassed.
- Combine selection is a combination of if statement, which contain multiple conditions, each connected with the logical operators AND or OR. If the connector AND is used to combine the conditions then both conditions must be true for the combined condition to be true. If the connector OR is used to combine two conditions then one of the conditions need to true in order to make the combination true. When neither condition is true, then the combined condition is considered false.
- Nested selection is used for multiple cases by placing if…….else selection statements inside other if………else selection statement. For example:
If student_grade >= 90 then
Print “A”
Else
If student_grade >= 80 then
Print “B”
Else
If student_grade >= 70 then
Print “C”
Else
If student_grade >= 60 then
Print “D”
Else
Print “F”
Endif
Endif
Endif
Endif
Note that there are an equal number of IF, ELSE and ENDIF statements, that each ELSE and ENDIF statement is positioned so that it corresponds with its matching IF statement, and that the correct indentation makes it easy to read and understand.
The other type of structure control is repetition control. The repetition structure is a bit more complicated than the selection structure, but it is easy to understand. A repetition structure consists of a block of statements that are executed repetitively. They might be executed once or a hundred times depending on the controls placed on the structure. There are three different ways in which a set of instructions can be repeated, and each way is determined by where the decision to repeat is placed:
- at the beginning of the loop (leading decision loop)
- at the end of the loop (trailing decision loop)
- A counted number of times (counted loop).
Leading decision loop is DOWHILE construct, the condition is tested before any statements are executed. Its format is:
DOWHILE condition p is true
statement block
ENDDO
- Condition p is test
- If p is true, the statement blocks are executed once. The delimiter ENDDO then triggers a return of control to the retesting condition p.
- If condition p is still true, the statements are executed again, and so the repetition process continues until the condition is found to be false.
- If condition p is found to be false, control passes to the next statement after the delimiter ENDDO and no further processing takes place within the loop.
In trailing decision loop, REPEAT……. UNTIL has been used to test the condition at the end of the loop. This means that the statements within the loop will be executed once before the condition is tested. If the condition is false, the statements will be repeated UNTIL the condition becomes true. The format of the REPEAT…UNTIL structure is:
REPEAT
statement
statement
.
.
UNTIL condition is true
Counted repetition occurs when the exact number of loop iterations is known in advance. The execution of the loop is controlled by a loop index, and instead of using DOWHILE, or REPEAT…UNTIL, the simple keyword DO is used as follows:
DO loop_index = initial_value to final_value
statement block
ENDDO
The DO loop does more than just repeat the statement block. It will:
- initialize the loop_index to the required initial_value
- increment the loop_index by 1 for each pass through the loop
- test the value of loop_index at the beginning of each loop to ensure that it is within the stated range of values
- terminate the loop when the loop_index has exceeded the specified final_value. .
Reference:
- Simple Program Design: A Step by Step Approach, Chapter 4, 5e by Lesley Anne Robertson.
- Simple Program Design: A Step by Step Approach, Chapter 5, 5e by Lesley Anne Robertson