Docsity

Structured Control Constructs in Programming: Sequence, Selection, Iteration and Recursion, Lecture notes of Computer science

Partial preview of the text

Download Structured Control Constructs in Programming: Sequence, Selection, Iteration and Recursion and more Lecture notes Computer science in PDF only on Docsity!

Programming

LESSON [03] – STRUCTURED CONTROL CONSTRUCTS

Lesson Outline

 Structured Programming

 Sequence

 Selection with if / else

 Selection with switch

 While & For loops

 Do while & Repeat/until loops

 Recursion

Sequence

Sequence control structure

This refers to the line-by-line

execution, in which statements are

executed sequentially, in the same

order in which they appear in the

script.

a = 15 b = 10 Total = a + b Print “Total = “, Total Pseudocode a = 15 # An integer assignment b = 10 # An integer assignment total = a + b # Calculating the total print (“Total = “, total)

Selection Selection control structure

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:

1. If and else statement

2. Nested If statement

3. Switch statement

if and else statement Num = 15 Print Num, Type If Num % 2= No Yes Type = “EVEN” Type = “ODD” Num = 15 IF Num % 2 = 0 THEN Type = “EVEN” ELSE Type = “ODD” END IF Print Num, Type Pseudocode Num^ =^10 if Num % 2 == 0 : Type = ”EVEN” else: Type = ”ODD” print (Num,” is “,Type)

Nested if statement Marks = 50 if Marks >= 80 : Grade = ”Distinction” elif Marks >= 60 : Grade = ”Credit” elif Marks >= 40 : Grade = ”Pass” else: Grade = ”Fail” print(“You got a “,Grade) Marks = If Marks >= 80 Yes No (^) Grade = “D” Print Grade If Marks >= 60 Grade = “C” If Marks >= 40 Grade = “F” Grade = “P” No No Yes Yes Marks = 50 IF Marks > = 80 THEN Grade = “Distinction” ELSE IF Mark >= 60 THEN Grade = “Credit” ELSE IF Mark >= 40 THEN Grade = “Pass” ELSE Grade = “Fail” END IF END IF END IF Print Grade Pseudocode

Switch statement Code = 1 Case Code = 1 Yes No (^) Color = “Red” Print Color Case Code = 2 Color = “Green” Case Code = 3 Color = “White” Color = “Blue” No No Yes Yes Code = 1 CASE OF Code 1 : Color = ”Red”; BREAK : 2 : Color = ”Green”; BREAK ; 3 : Color = ”Blue”; BREAK ; DEFAULT : Color = “White” END CASE Print Color Pseudocode

This allows you to make a

selection on the value of a

variable which has discrete values

like integer.

However, Python doesn’t have a

switch statement because that

requirement can be fulfilled from

the above.

Repetition

Repetition control structure

This is a control structure that allows the

execution of a block of statements

multiple times until a specified condition

is met. Different types of repetition

controls are available.

Different types of iterations

1. While loop

2. For loop

3. Do While loop

4. Repeat / until loop

5. Recursion

You may not find all of them in one

programming language.

For loop Total = 0 for x in range ( 1 , 6 ): print (“x = “,x) Total = Total + x print(“Total = “,Total) FOR x = 1 TO 5 STEP 1 Print x Total = Total + x END FOR Print Total Pseudocode For x = 1 to 5 No Yes print x Total = Total + x Print Total

Do while loop x = 1 DO Print x Total = Total + x x = x + 1 WHILE x <= 5 Print Total Pseudocode While x <= 5 False True print x Total = Total + x Print Total x = x + 1 x = Python doesn’t support do while loop.

Repeat / until loop x = 1 REPEAT Print x Total = Total + x x = x + 1 UNTIL x > 5 Print Total Pseudocode Until x > 5 True False print x Total = Total + x Print Total x = x + 1 x = Python doesn’t support Repeat / until loop.

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 result of the self-call the block of statements in the procedure of function

get executed repeatedly.

 However, this is relatively more complex, and it will be discussed later in this

book when we elaborate more on procedures and functions.