a. What You Need To Know
Control structures
Control structures determine the flow of execution in a program.
There are three fundamental control structures:
- sequence
- selection
- iteration
Key Point
Control structures decide the order in which code runs. If you understand sequence, selection and iteration, you understand the backbone of program logic.
Sequence
A sequence is a set of instructions that runs one line at a time in the order it is written.
This is the simplest control structure. It is useful when the program just needs to follow a fixed process from start to finish.
Selection
A selection allows a program to make decisions based on whether a condition is true or false.
Selections are usually written with structures such as:
IFIF / ELSEIF / ELSEIF / ELSE
Alternative execution
Alternative execution means that one block of code runs if the condition is true, and a different block runs if the condition is false.
Chained selection
Chained selections are used when a program needs to test more than one possible condition.
Nested selection
Nested selections happen when one selection is placed inside another. These are useful when a second decision only matters after a first condition has already been met.
Common Error
Students often create logic errors by using separate IF statements when a chained ELSEIF structure is needed. That can cause more than one branch to run.
Iteration
Iteration means repeating a section of code until a condition is met, or for a known number of times.
WHILE loops
A WHILE loop repeats for as long as its condition remains true.
Use a WHILE loop when:
- the number of repetitions is not known in advance
- the loop depends on user input or another changing condition
DO/WHILE loops
A DO/WHILE loop also repeats while a condition remains true, but it always runs at least once before the condition is checked.
FOR loops
A FOR loop repeats a set number of times. It is especially useful for processing every item in an array or list.
Infinite loops
An infinite loop occurs when the ending condition is never reached.
WHILE vs DO/WHILE
WHILE is a pre-test loop, so it may not run at all. DO/WHILE is a post-test loop, so it always runs at least once.
Validation
Validation checks whether input data is reasonable before the program accepts and processes it.
Validation does not prove the data is true in the real world. It only checks whether the data fits the expected rules.
Existence checking
Existence checking makes sure a value has actually been entered.
This is useful for required fields, because blank input should often be rejected.
Type checking
Type checking makes sure the input is of the correct data type, such as numeric data where a number is required.
Range checking
Range checking makes sure the value is within acceptable limits or within a set of allowed values.
Examples:
- age must be between 15 and 17
- score must be from 0 to 100
- size must be small, medium or large
Validation Order
A sensible order is: check that data exists, then check the type, then check the range. There is no point checking the range of data that is missing or the wrong type.
Helpful thinking tools
Truth tables are useful when a selection contains more than one logical expression. They help you test every combination of true and false values so you can spot logic errors before coding or during testing.
Although truth tables are not the main focus of the course, they are very useful when working through complex conditions.
What this means for your folio
In a folio task, you should be able to:
- choose the correct control structure for the job
- explain why a loop or selection is needed
- validate input before processing it
- avoid logic errors caused by poor conditions or poor loop control
If control structures and validation are weak, the program may run but still behave incorrectly.