0% found this document useful (0 votes)
4 views

Structured - Control - Constructs

Structured Control Constructs

Uploaded by

Rowen Fernando
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Structured - Control - Constructs

Structured Control Constructs

Uploaded by

Rowen Fernando
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

UNIT1 - Programming

Week [03] –Structured Control Constructs

1
Lesson Learning Outcome
Pass Merit Distinction

LO1 Define basic algorithms to carry out an operation and


outline the process of programming an application

P1 Provide a definition of M1 Determine the step taken D1 Examine the


what an algorithm is and from writing code to implementation of an
outline the process in execution. algorithm in a suitable
building an application. language. Evaluate the
relationship between the
written algorithm and the
code variant.

2
An Algorithm

Algorithm = Procedure = Blueprint = Recipe


▪ We've all seen food recipes - they list the ingredients needed and a set of
steps on how to make the described meal. Well, an algorithm is just like
that.
▪ You can think of a programming algorithm as a recipe that describes the
exact steps needed for the computer to solve a problem or to reach a
goal.
▪ Algorithms are generally created independent of underlying languages,
i.e. an algorithm can be implemented in more than one programming
language.

3
Characteristics of Algorithms

▪ Unambiguous − An Algorithm should be clear and unambiguous.


Each of its steps must lead to only one meaning.
▪ Input − An algorithm should have 0 or more well-defined inputs.
▪ Output − An algorithm should have 1 or more well-defined outputs,
and should match the desired output.
▪ Finiteness − Algorithms must terminate after a finite number of
steps.
▪ Feasibility − Should be feasible with the available resources.
▪ Independent − Should be independent of any programming code.

4
Structured Programming

According to the structure theorem, any computer program can be written


using the basic control structures:

▪ Sequence – Executing statements one after the other.

▪ Selection – Making a choice of one or more alternative statements.

▪ Iteration (Repetition) – Running one statement or a block of


statements many times
They can be combined in any way necessary to deal with a given problem

5
Sequence

This refers to the line-


by-line execution, in
which statements are
executed sequentially,
in the same order in
which they appear in
a = 15 # An integer assignment

PYTHON Code
the script. b = 10 # An integer assignment
total = a + b # Calculating the total
print (“Total = “, total)

6
Selection

Depending on whether a condition is true or


false, the selection control structure may
skip the execution of an entire block of
statements or even execute one block of
statements instead of another.
Selection control constructs are:
▪ If and else statement
▪ Nested If statement
▪ Switch statement

7
if without else

Pseudocode PYTHON Code


a = 15 a = 15
IF a > 10 THEN if a > 10:
Print a print(“Value of a= “,a)

a = 10
if a > 10:
print(“Value of a= “,a)
print (“END”)

8
if and else statement

Pseudocode PYTHON Code


Num = 15 Num = 10
IF Num % 2 = 0 THEN
if Num % 2 == 0:
Type = “EVEN”
Type = ”EVEN”
ELSE
Type = “ODD”
else:
END IF Type = ”ODD”
Print Num, Type print (Num,” is “,Type)

9
Nested if statement
Pseudocode PYTHON Code
Marks = 50
IF Marks > = 80 THEN Marks = 50
Grade = “Distinction” if Marks >= 80:
ELSE IF Mark >= 60 THEN
Grade = ”Distinction”
Grade = “Credit”
elif Marks >= 60:
ELSE IF Mark >= 40 THEN
Grade = ”Credit”
Grade = “Pass”
elif Marks >= 40:
ELSE
Grade = ”Pass”
Grade = “Fail”
END IF else:
END IF Grade = ”Fail”
END IF print(“You got a “,Grade)
Print Grade

10
Desk check this code

Test Case Input marks Expected Result Actual Result Test Passed? Marks = 50
if Marks >= 80:
1 80 Distinction Distinction Yes
Grade = ”Distinction”
2 75 Credit Credit Yes
elif Marks >= 60:
3 59 Pass Pass Yes Grade = ”Credit”
4 60 Credit Credit Yes elif Marks >= 40:

5 48 Pass Pass Yes Grade = ”Pass”


else:
6 39 Fail Pass No
Grade = ”Pass”
7 101 Invalid Input Distinction No
print(“You got a “,Grade)
8 -5 Invalid Input Pass No

11
Switch statement

Pseudocode This allows you to make a


Code = 1
Code = 1 selection on the value of a
CASE OF Code variable which has
Yes
Case Code = 1
1 : Color = ”Red”; BREAK: discrete values like integer.
No Color = “Red”
2 : Color = ”Green”; BREAK;
Case Code = 2 Yes
3 : Color = ”Blue”; BREAK; However, Python doesn’t
No Color = “Green” DEFAULT: Color = “White” have a switch statement
Case Code = 3 Yes END CASE because that requirement
No Print Color can be fulfilled from the
Color = “White” Color = “Blue” above.

Print Color

12
Repetition

This is a control structure Different types of iterations


that allows the execution of 1. While loop
a block of statements 2. For loop
multiple times until a
3. Do While loop
specified condition is met.
4. Repeat / until loop
Different types of repetition
controls are available. 5. Recursion
You may not find all of them in one
programming language.

13
While loop
Pseudocode PYTHON Code
x= 1
x = 1
Total = 0
Total = 0
WHILE x <= 5
while x <= 5:
Print x
Print (“x = “,x)
Total = Total + x
Total = Total + x
x=x+1
END WHILE x =x + 1

Print Total print(“Total = “,Total)

14
For loop

Pseudocode PYTHON Code


FOR x = 1 TO 5 STEP 1 for x in range (1, 6):
Print x
Print (“x = “,x)
Total = Total + x
Total = Total + x
END FOR
print(“Total = “,Total)
Print Total

15
Do while loop

Pseudocode PYTHON Code


x=1
DO Python doesn’t
Print x support do while
loop.
Total = Total + x
x = x +1
WHILE x <= 5
Print Total

16
While loop vs Do while loop

While Loop Do While Loop

Compare A repetition control A repetition control

Repeats while True Repeats while true

Change need is explicitly done Change need is explicitly done

Condition is Tested first Condition is Tested at last


Contrast

First Check then Process First Process then Check

Zero iteration possible Process at least once

17
Repeat / until loop
Pseudocode PYTHON Code
x=1
REPEAT
Python doesn’t
Print x support repeat until
Total = Total + x loop.

x = x +1
UNTIL x > 5
Print Total

18
Recursion

▪ Recursion is very a powerful alternative to the loops.


▪ This is a technique of calling subroutine (a procedure or a
function) by the same subroutine (self-calling).
▪ As a result of the self-call the block of statements in the
procedure of function gets executed repeatedly.
▪ However, this is relatively more complexed, and it will be
discussed later in this book when we elaborate more on
procedures and functions.

19
Lesson Summary

▪ Algorithms its Characteristics


▪ Structured Programming
▪ Sequence
▪ Selection with if / else
▪ Selection with switch
▪ While & For loops
▪ Do while & Repeat/until loops
▪ Recursion

20

You might also like