3.3.2 Define basic programming
What is a program?
A computer program is a set of instructions given to a computer, to be executed in a specific order. Because computers do not speak human languages, computer programming languages were created. These languages allow humans to write instructions in a way that computers can understand. While there are several different computer languages, all computer languages are based on logical structures.
The figure shows the most common logical structures found in programming languages:
- IF condition THEN instructions (If/Then) - This is one of the most common programming structures. It is used to introduce conditional code execution. The set of instructions following the THEN keyword is only executed if the condition given is true. If the condition is false, the instructions are never executed. For example, IF password = 12345, THEN display'password correct'. The code above would only show the 'password correct' message if a password of 12345 is entered.
- FOR expression DO instructions (For/Do) - This logical structure is used to create controlled loops. The set of instructions is executed as many times as defined inexpression. When expression is no longer met, the loop ends and the computer moves on to the next instruction. For example, FOR count<=10 DO display 'not 10 yet!'. The program will check the value of the variable called count. As long as the count is less than or equal to 10, it will display 'not 10 yet!' on the screen. As soon as the count is greater than 10, the structure is abandoned and the computer moves on to the next line of code.
- WHILE condition DO instructions (While/Do) - The WHILE logical structure is also used to create controlled loops, but in a different way. WHILE executes instructions as long as thecondition is true. When the condition is no longer true, the structure is abandoned and the computer moves on to the next line of code. For example, WHILE temperature sensor> 80DOshow 'temperature too high!' on screen. The message 'temperature too high!' will display repeatedly until the value of the temperature sensor is less than or equal to 80.
Logical conditions like these are the building blocks of computer programs.