Program Constructs: Iteration & Recursion
Nesting
When a construct is written completely contained within another. They are not allowed to overlap.
Recursion
When a subroutine calls itself with different parameters
Disadvantages of Iteration
Algorithm may be more difficult to follow/trace because variables are being reused. Need to be careful to get conditions of loops correct. Humans often express the problem in a recursive way (rather than an iterative way)
Advantages of SELECT over IF
All the alternatives depend on the value of one variable. So this makes the code clearer and easier to read/write. Allows multiple branches and avoids nested Ifs. Avoids numerous repeats of similar conditions
Advantage of Recursion
Can be easier to program or more intuitive with an inherently recursive algorithm
Iteration
Code is executed repeatedly for a given number of time or until a condition is met.
Count Controlled Loop (FOR)
FOR loop is set up with a fixed number of iterations. Uses a loop variable e.g. FOR Count = 1 TO 10 ......NEXT Count
Condition Controlled Loop (REPEAT UNTIL)
REPEAT UNTIL loop repeats until some condition become true. E.g. REPEAT....UNTIL Count > 10
Disadvantage of Recursion
Uses many sets of variables (one set per call) therefore less efficient use of memory and often slower and can run out of stack space causing run time error.
Advantages of Iteration
Uses only one set of variables so more efficient use of memory and can be faster. Less likely to run out of space
SELECT (CASE)
Value of variable/expression used to determine which statement of options is executed. There can be a default option
Condition Controlled Loop (WHILE)
WHILE loops run repeatedly depending on a condition (it repeats while the condition is true. E.g. WHILE Count < 11.....END WHILE