Practical 4
Practical 4
Y8-Computing
Unit 9.1: Presenting choices: Combining Constructs
All programming languages have 3 elements in common that give us the power to implement
solutions to extremely complex problems.
These 3 elements are:
Sequence
Selection
Iteration
let’s describe these elements:
1. Sequence: This means that the computer will run your code in order, one line at a time
from the top to the bottom of your program. It will start at line 1, then execute line 2 then
line 3 and so on till it reaches the last line of your program.
2. Selection: Sometimes you only want some lines of code to be run only if a condition is met,
otherwise you want the computer to ignore these lines and jump over them. This is
achieved using IF statements. e.g., If a condition is met then lines 4, 5, 6 are executed
otherwise the computer jumps to line 7 without even looking at line 4,5 and 6.
3. Iteration: Sometimes you want the computer to execute the same lines of code several
times. This is done using a loop. There are three types of loops: For loops, while loops and
repeat until loops. That’s handy as it enables you not to have to copy the same lines of code
many times.
For Loop:
A count-controlled loop is where a series of program instructions is repeated a set number of
times. (note: loop variable always starts at 0)
For example, Variable name In is used to separate the variable from the
number of times the loop will run
for i in range (5):
Tells the computer that it will be the number of times the loop will run
running a for loop
2. Write a program that repeats Hello world 8 times using for loop.
Evidence 1: Take the screenshot of python shell & script mode both and paste it in your
evidence file.
Evidence 2: Take the screenshot of python shell & script mode both and paste it in your
evidence file.
7. Write a program that will give output A, then B, then it will alternate C’s and D’s
9. Modify the above program to print five C’s followed by five D’s, instead of alternating C’s and D’s to
produce the following output.
Evidence 3: Take the screenshot of python shell & script mode both and paste it
in your evidence file.
--------------------------------------------