THE THREE BASIC CONSTRUCTS Paper: 2
The Three Basic Constructs:
The three basic programming constructs: sequence, selection and iteration, allows us to control
how the program will execute the statements in it
1. Sequence: A sequence is a control structure in which a set of instructions is each executed
once, in the order in which they are written.
Example: The following program is an example of sequence that calculates the average of two numbers.
declare total as integer
declare average as real
declare a, b as integer
input a
input b
total = a + b
average = total/2
output total, average
2. Selection: Selection is a control structure in which an option of statements is provided and a
condition is used to decide which (if any) statements should be executed. The selection includes:
a) IF statements
b) CASE statements
a) IF statements: The common format/Syntax of IF statements are given below
i) IF <condition> THEN
<Statement>
END IF
ii) IF <condition> THEN
< Statement1>
ELSE
< Statement2 >
END IF
Example 1: (a/i)
IF amount >= 10 Then
Print “You can have Tea”
End If
IF amount >= 20 Then
Print “You can have Coffee”
End If
IF amount >= 50 Then
Print “You can have Ice-cream”
End If
Teacher: Md Nazrul Islam Email: | School: BAF SEMS 1
, THE THREE BASIC CONSTRUCTS Paper: 2
Example 2: (a/ii)
IF mark >= 50 Then
Print “Student Passed”
Else
Print “Student Failed”
End If
Example 3: (a/ii) [Do the dry run of it]
total = 15
k=5
input k
IF k >= 3 THEN
k=k*k
total = total + k
ELSE
total = total + k
END IF
print total
b) Syntax of CASE statements: The common format of CASE statement is given below:
SELECT CASE <expression>
CASE <value1>
<One or more statements>
CASE <value2>
< One or more statements >
CASE <value3>
< One or more statements >
…
CASE Else
< One or more statements >
END SELECT
Teacher: Md Nazrul Islam Email: | School: BAF SEMS 2
The Three Basic Constructs:
The three basic programming constructs: sequence, selection and iteration, allows us to control
how the program will execute the statements in it
1. Sequence: A sequence is a control structure in which a set of instructions is each executed
once, in the order in which they are written.
Example: The following program is an example of sequence that calculates the average of two numbers.
declare total as integer
declare average as real
declare a, b as integer
input a
input b
total = a + b
average = total/2
output total, average
2. Selection: Selection is a control structure in which an option of statements is provided and a
condition is used to decide which (if any) statements should be executed. The selection includes:
a) IF statements
b) CASE statements
a) IF statements: The common format/Syntax of IF statements are given below
i) IF <condition> THEN
<Statement>
END IF
ii) IF <condition> THEN
< Statement1>
ELSE
< Statement2 >
END IF
Example 1: (a/i)
IF amount >= 10 Then
Print “You can have Tea”
End If
IF amount >= 20 Then
Print “You can have Coffee”
End If
IF amount >= 50 Then
Print “You can have Ice-cream”
End If
Teacher: Md Nazrul Islam Email: | School: BAF SEMS 1
, THE THREE BASIC CONSTRUCTS Paper: 2
Example 2: (a/ii)
IF mark >= 50 Then
Print “Student Passed”
Else
Print “Student Failed”
End If
Example 3: (a/ii) [Do the dry run of it]
total = 15
k=5
input k
IF k >= 3 THEN
k=k*k
total = total + k
ELSE
total = total + k
END IF
print total
b) Syntax of CASE statements: The common format of CASE statement is given below:
SELECT CASE <expression>
CASE <value1>
<One or more statements>
CASE <value2>
< One or more statements >
CASE <value3>
< One or more statements >
…
CASE Else
< One or more statements >
END SELECT
Teacher: Md Nazrul Islam Email: | School: BAF SEMS 2