Structure of procedural programs
Key Terms
- Statement - Any line in code which will perform a action.
- Procedure - This is a named section of code, which can have parameters, which can be called to perform a specific task.
- Subroutine - A procedure which does not return a value or have any parameters
- Function - A procedure which can return a value
- Parameter - A value which can be passed through to a procedure or function
- Sequence - The order which instructions will be called. Also known as the "flow" of code. Is effected by control statements
- Control statements - These effect the flow of a program. This includes IF statements and loops.
- Itteration - Code which loops such as FOR, WHILE or REPEAT-UNTIL
- Selection - IF statements which can effect the flow of code.
- Psuedocode - Language nutral code which is used to design and express algorithims or other programming ideas.
- Recursion - A procedure which can call itself
- Terminator - A IF statement inside a recursive procedure which acts as a way to prevent further recursion from happening. Stops recursion from becoming an infinite loop.
- Case / select - A special form of IF statment which allows many equalities to be tested at once.
Itteration key ideas
- There are three types of loops. FOR, WHILE and REPEAT-UNTIL
- FOR loops have 3 main parts. The initialization (or the start point), the end point and the increment.
- FOR A=0 TO 10 STEP 4 - A=0 is the start point, TO 10 is the end point and STEP is the increment
- FOR loops use the keyword NEXT to end the loop
- WHILE loops will ONLY repeat if the condition is true.
- REPEAT-UNTIL will ONLY repeat if the condition is false.
- REPEAT-UNTIL will always run once
- WHILE and REPEAT-UNTIL must have the increment and initialization manually inserted.
- Increment should normally be the LAST part of the loop
- The condition should ALWAYS evaluate to boolean
IF statements
- You can use AND, OR and NOT in a condition.
- ELSE is run if the condition is false. ELSE is optional
- Nested IF statements are IF statements with another IF inside either the true or false block (or both!)
- ELSE IF allows a test to be run if the first test evaluates to false. You can chain as many of these together as you like.
- Use indentation when writing IF statements. Especially when nesting them with other IF's or itteration.
Recursion
- Recursive procedures use a call stack to help keep track of procedure calls.
- When a recursive procedure ends it will return to the point it was called from.
- Terminators will stop recursion from continuing forever.
- Recursion and itteration can be interchanged. Some problems are better solved by recursion some by itteration.