Programming Constructs
Programs are designed using common building blocks. These building blocks, known as programming constructs (or programming concepts), form the basis for all programs.
There are three basic building blocks to consider:
- sequence is the order in which instructions occur and are processed
- selection determines which path a program takes when it is running
- iteration is the repeated execution of a section of code when a program is running
Sequence is the order in which programming statements are executed. Programming statements usually run one after another in order, unless one of the other programming constructs is used. The sequence of a program is extremely important as once these are translated, carrying out instructions in the wrong order leads to a program performing incorrectly.
The following example code will execute each line in order/sequence
|
|
Selection is a programming construct where a section of code is run only if a condition is met. In programming, there are often occasions when a decision needs to be made. Selection is the process of making a decision. The result of the decision can either be TRUE or FALSE, this determines which path the program will take next.
The following code uses selection to test the condition varHeight > 1.6, if this condition is TRUE then “Tall enough to enter ride” is printed.
|
|
The following code uses selection to test the condition username == “Callum”, if this condition is TRUE then “Access granted” is printed. If the condition is FASLE we can use else: to execute a different line of code.
|
|
Programs often need to repeat certain steps while or until a condition has been met. This process is known as iteration.
Iteration or repetition is often referred to as looping, since the program ‘loops’ back to an earlier line of code.
Iteration allows programmers to simplify a program and make it more efficient. Instead of writing out the same lines of code again and again(mistages happen), a programmer can write a section of code once, and ask the program to execute the same line repeatedly until no longer needed.
Python programming offers two kinds of loop, the for loop and the while loop. Using these loops along with loop control statements like break and continue, we can create various forms of loop.
We can create an infinite loop using while statement. If the condition of while loop is always True, we get an infinite loop.
Example #1: Infinite loop using while
|
|
Enter an integer: 3
The double of 3 is 6
Enter an integer: 5
The double of 5 is 10
Enter an integer: 6
The double of 6 is 12
Enter an integer:
Traceback (most recent call last):
This is a normal while loop without break statements. The condition of the while loop is at the top and the loop terminates when this condition is False.
Example #2: Loop with condition at the top
|
|
This kind of loop can be implemented using an infinite loop along with a conditional break in between the body of the loop.
Flowchart of Loop with Condition in Middle
|
|
Enter a vowel: r
That is not a vowel. Try again!
Enter a vowel: 6
That is not a vowel. Try again!
Enter a vowel: ,
That is not a vowel. Try again!
Enter a vowel: u
Thank you!
This kind of loop ensures that the body of the loop is executed at least once. It can be implemented using an infinite loop along with a conditional break at the end. This is similar to the do…while loop in C.
Flowchart of Loop with Condition at Bottom
Example #4: Loop with condition at the bottom
|
|
Press enter to roll the dice
You got 1
Roll again?(y/n) y
Press enter to roll the dice
You got 5
Roll again?(y/n)
The for loop in Python is used to iterate over a sequence (list, tuple, string) or other iterable objects. Iterating over a sequence is called traversal.
Syntax of for Loop
for val in sequence:
loop body
Loop continues until we reach the last item in the sequence. The body of for loop is separated from the rest of the code using indentation.
Flowchart of for Loop
Example: Python for Loop
|
|
The sum is 48