The three basic programming constructs
programSequences of instructions for a computer. are designed using common building blocks. These building blocks, known as programming constructs, form the basis for all programs.
There are three basic constructs to consider:
- sequenceIn computer programming, this is a set of instructions that follow on one from another. - the order in which instructionA single action that can be performed by a computer processor. occur and are processed
- selectionA decision within a computer program when the program decides to move on based on the results of an event. - determines which path a program takes when it is running
- iterationThe repetition of a block of statements within a computer program. - the repeated executionThe process of a program being run on a computer. of a section of code when a program is running
There are two types of iteration:
- count-controlled iterationA loop that repeats a set number of times, usually implemented using a FOR loop. A loop counter is used to keep track of how many times the loop has iterated. - repeatedly executes a section of code a fixed number of predetermined times
- condition-controlled iterationA loop that repeats until a condition is either met or no longer met. This is usually implemented using a WHILE or REPEAT UNTIL loop. - repeatedly executes a section of code until a conditionIn computing, this is a statement that is either true or false. A computation depends on whether a condition equates to true or false. is met or no longer met
All programs use one or more of these constructs. The longer and more complex the program, the more these constructs will be used repeatedly.