Programming constructs

<< Previous: Programming fundamentalsNext: Data types >>

Programming constructs

Programs are built by combining the three basic programming constructs used to control the flow of a program:



Sequence

Sequence is where code is executed one instruction after the next. Whenever we are not using the other constructs, code runs sequentially. The example program below is executed one line after the next.



Selection

Selection is where we check a Boolean condition and depending on the result run different code. A Boolean condition is one that can evaluate as True or False e.g. name == "Bob" Selection can be carried out using if statements or case select statements.

If statements

There are three types of if statements we can use:

With a basic if, a condition is checked and code is either executed or no code is executed, and the program moves on. With an if-else condition some code will always be executed. If the condition is true the first block of code and if it is false the block of code indented under the else command. So in the example below, the program on the left only outputs a message if the condition is true where as the program on the right will always produce some output.

Else if allows us to check more than one condition and provide different code for each possible answer. We can include as many else if conditions as we like in an if statement. Conditions are checked in the order they are supplied. The if statement ends after evaluating any condition as true and executing its associated code. You can choose to include or not include an else condition. Without it no code will be executed if none of the conditions are true and with it, the else cpondition is only executed if all other conditions are false.

In the example below you may be confused as to why we don't need to check AND score < 40. This os because the if statement only reaches this elif condition if the original condition was false, so we already know score was not greater than or equal to 40. It is important to get checks like this in the right order. If we were to first check score > 20 and assign "C" then it would not check any subsequent elif statements for a higher value and even those that should get B or A would get output C.



Switch/Case select statements

A switch, or case select statement, accepts a parameter as a variable and performs a different outcome depending what that parameter is. It is a neater way of completing a long if-elif-else statement. The default case is the one to execute if none of the other cases are matched.



Iteration

Iteration is sometimes called looping. It's where a section of code is repeated more than once. There are 2 types of loop you must be familiar with:

Count controlled iteration

With count controlled iteration we specify how many times the loop should run. A for loop is a type of count controlled iteration. It runs as many times as specified. In OCR reference language the numbers are inclusive but this may vary in implementation in actual languages.


You can use a step command with a for loop to increment or decrement the loop counter by any positive or negative value.

Condition controlled iteration

With condition controlled iteration we specify the condition that causes the code to stop looping. There are two types of condition controlled loops to be aware of:

While loops

A while loop checks a condition and if it evaluates as true, executes some code before checking again. If the condition evaluates as false initially, the code in a while loop never runs. If you don't code in a way for the condition to eventually evaluate as false, you can end up with an infinite loop. The code in green below is correct. The code in red would create an infinite loop as there is no way for the value in the variable guess to change.

Do until loop

A do until loop is very similar to a while loop except, in a do until loop the check is at the end of the loop meaning the code in a do until loop is always executed at least once.



Knowledge check


Questions:
Correct:

Question text


<< Previous: Programming fundamentalsNext: Data types >>

© All materials created by and copyright S.Goff