Selection Control Structure

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

SELECTION

CONTROL
STRUCTURE
Selection Control Structure
● The IF statement is used in
representing the
SELECTION/DECISION control
structure. There are three (3) main
forms of the IF statement.
● IF-THEN-ENDIF
● IF-THEN-ELSE-ENDIF
● NESTED IF
IF-THEN-ENDIF
IF <condition> THEN
statement 1
statement 2
ENDIF
if (gender = “male”) then
print “You can be admitted”
end if
IF-THEN-ELSE-ENDIF
IF <condition> THEN
statement 1
statement 2
ELSE
statement 3
statement 4
ENDIF
.
.
if (age <= 18) then
fee = 40
else
fee = 100
end if
..
NESTED IF
IF <condition1> THEN
statement 1
ELSE
IF <condition2> THEN
statement 2
ELSE
IF <condition3> THEN
statement 3
ELSE
statement 4
ENDIF
ENDIF
ENDIF
IF-THEN-ENDIF(FLOWCHART)
START

FALSE CONDITI TRUE


ON

STATEMENT 1

STOP
IF-THEN-ELSE-ENDIF(FLOWCHART)

START

FALSE CONDITI TRUE


ON

STATEMENT 3 STATEMENT 1

STATEMENT 4 STATEMENT 2

STOP
NESTED IF
START

F CONDITI T
ON 1

F CONDITI T STATEMENT 1
ON 2

F CONDITI T STATEMENT 2
ON 3

STATEMENT 4 STATEMENT 3

STOP
Selection Statement
● ALGORITHM 1 ● ALGORITHM 2

IF it is raining THEN IF it is raining THEN


take umbrella take umbrella
END IF ELSE
don’t take umbrella
END IF
RELATIONAL OPERATORS (Forming
Conditions)
Symbol Meaning
= Equal to

=! OR <> Not Equal to

> Greater than

< Less than

>= Greater than or equal to

<= Less than or equal to


Questions
1. You are given two variables, numb1 and
numb2, both have been assigned with the
following values 40 and 50 respectively. Write
the pseudecode and draw a flowchart to print
the value of numb1 if it is greater than
numb2.
2. Write a pseudocode and draw a flowchart to
print the larger value of the two variables A &
B.
1. You are given two variables, numb1 and numb2,
both have been assigned with the following values 40
and 50 respectively. Write the pseudecode and draw
a flowchart to print the value of numb1 if it is
greater than numb2.
Algorithm larger
Start
Declare numb1, numb2 as integer
numb1  40
numb2  50
if (numb1> numb2) then
print numb1
end if
Stop
1. Writea pseudocode and draw a flowchart to print the
larger value of the two variables A & B.
Algorithm larger
Start
Declare a, b as integer
print ”Please enter a number”
read A
print ”Enter another number”
read B
if (A>B) then
print “The larger number is ”, A
else
print “The larger number is ”, B
end if
Stop
Questions (cont’d)
3. Write a pseudocode and construct a
flowchart to read TWO numbers into
variables A & B. The algorithm should
store the smaller in A and the larger in B,
and print the content of A & B.
4. Write a pseudocode and construct a
flowchart to read values into two variables
A and B and subtract the smaller number
from the larger number.
NESTED IF
START

F CONDITI T
ON 1

F CONDITI T STATEMENT 1
ON 2

F CONDITI T STATEMENT 2
ON 3

STATEMENT 4 STATEMENT 3

STOP
Questions (cont’d)
5. Write a pseudocode and construct a
flowchart to read values into the variables
A, B, and C. Print the largest of the of the
numbers.
6. Write a pseudocode and construct a
flowchart to read an integer value for
GRADE. The algorithm should print “PASS”
if the grade is greater or equal than 60
and “FAIL” if the grade is less than 60.
5. Write a pseudocode to read values into the
variables A, B, and C. Print the largest of the of
the numbers.
Start
Read A
Read B
Read C
If A > B Then
If A > C Then
Print “The largest number is “, A
Else
Print “The largest number is “, C
End if
Else
If B > C Then
Print“The largest number is “, B
Else
Print “The largest number is “, C
End if
End if
Stop
Questions (cont’d)
7. Write a pseudocode and construct a flowchart
to read an integer value for MARK and print the
appropriate grade based on the following:

MARK GRADE
80 or more A
Less than 80 but 65 or more B
Less than 65 but 50 or more C
Less than 50 but 45 or more D
Less than 45 F
Algorithm grade
Start
Declare mark as integer
Print “Please enter mark obtained”
Read mark
If mark >=80 Then
print “You got and A”
Else if mark >= 65 Then
print “You got a B”
else if mark >= 50 Then
print “You got a C”
else if mark >= 45 Then
print “You got a D”
else
print “You got a F”
end if
end if
end if
End If
Stop

You might also like