2. CONTROL STRUCTURES
1.1 Structured Program Theorem It is possible to write any computer program by using only three
basic control structures:
Executing one subprogram, and then another subprogram (sequence)
Executing one of two subprograms according to the value of a boolean expression (selection)
Executing a subprogram until a boolean expression is true (iteration or repetition)
Type of control structure
sequence
Selection
(iteration or repetition)
3. SELECTION CONTROL STRUCTURE
The selection structures are of very high importance, because they enable programs to branch
into different blocks of instructions depending on particular conditions. This way they provide a
kind of “intelligence” to programs.
A control structure is a block of programming that analyzes variables and chooses a direction in
which to go based on given parameters.
• The term flow control details the direction the program takes (which way program control "flows").
Hence it is the basic decision-making process in computing; flow control determines how a
computer will respond when given certain conditions and parameters.
4. Basic Terminologies Those initial conditions and parameters are called preconditions.
Preconditions are the state of variables before entering a control structure.
Based on those preconditions, the computer runs an algorithm (the control structure) to
determine what to do. The result is called a postcondition.
Postconditions are the state of variables after the algorithm is run.
An Example Let us analyze flow control by using traffic flow as a model.
5. A vehicle is arriving at an intersection. Thus, the precondition is the vehicle is in motion.
Suppose the traffic light at the intersection is red. The control structure must determine
the proper course of action to assign to the vehicle.
- Precondition: The vehicle is in motion.
- Control Structure Is the traffic light green? If so, then the vehicle may stay in motion. Is
the traffic light red? If so, then the vehicle must stop. End of Control
Structure
6. Type Description Best Use Case
if Executes when condition is true Single decision
if-else Executes one of two blocks Two outcomes
if-elif-else Checks multiple conditions Multiple outcomes
Nested if if inside another if Dependent decisions
switch-case
Matches against multiple
constant values
Clean multiple-choice selection
(non-Python)
SELECTION CONTROL STRUCTURE:
7. 1. Simple If Statement
Executes a block of code only when a specified condition is true.
2. If-Else Statement
Provides two paths: one block runs if the condition is true, the other runs if the condition is false.
3. Nested If Statement
Contains an if or if-else statement inside another if or else block, allowing multiple conditions to be checked in
a hierarchy.
4. If-Else If Ladder
Checks multiple conditions sequentially; the first true condition's block is executed.
5. Case (or Switch) Statement
Evaluates a single expression and executes one block of code among many alternatives, based on matching
values.
SELECTION CONTROL STRUCTURE:
8. 1. Simple if Statement :
A simple if statement is a basic control structure used in programming to execute a
block of code only if a certain condition is true. If the condition is false, the code inside
the if block is skipped.
9. START
int number1
int number2
IF number1 > number2 THEN
PRINT "The largest number is", number1
ELSE
PRINT "The largest number is", number2
ENDIF
END
Example : Write an Algorithm to find the Largest of Two Numbers
12. Case Statement
A case statement (also known as a switch statement in many programming languages) is a control
structure that allows a program to execute one block of code from multiple options, based on the value of
a single variable or expression.
It is used as an alternative to writing many if...else if statements, making the code more organized and
readable when handling multiple possible values of the same variable.
Example 1: Grade Evaluation
13. Write a CASE statement in pseudocode for
Grade Feedback Based on Letter Grade
INPUT grade
CASE grade OF
"A":
Display "Excellent!"
"B":
Display "Very Good!"
"C":
Display "Good"
"D":
Display "You passed"
"F":
Display "Failed"
OTHERWISE:
Display "Invalid grade"
ENDCASE
Write a CASE statement in pseudocode for Student Year
Classification. INPUT year Level
CASE yearLevel OF
1:
Display "You are a First-Year student"
2:
Display "You are a Second-Year student"
3:
Display "You are a Third-Year student"
4:
Display "You are a Final-Year student"
OTHERWISE:
Display "Invalid year level"
ENDCASE
14. • General Example:
Convert the following linear if statement into nested if statement.
linear if statement
IF a > 5 AND b < 7 THEN
statement(s)_A
ELSE IF c > 6 THEN
statement(s)_B
ELSE
statement(s)_C
ENDIF
nested if statement
IF a > 5 THEN
IF b < 7 THEN
statement(s)_A
ELSE IF c > 6 THEN
statement(s)_B
ELSE
statement(s)_C
ENDIF
ELSE IF c > 6 THEN
statement(s)_B
ELSE
statement(s)_C
ENDIF
15. Symbol Name Meaning
=
Assignment / Equal (varies by
context)
Often used to assign a value (e.g., x =
5)
== Equal to Checks if two values are equal
!= Not equal to Checks if two values are not equal
< Less than Example: x < 10
> Greater than Example: x > 5
<= Less than or equal to Example: x <= 10
>= Greater than or equal to Example: x >= 5
Comparison (Relational) Operators (used for comparing values)
16. SET i = 1
WHILE i <= 3 DO // Comparison Operator: <=
SET j = 1
WHILE j <= 3 DO // Comparison Operator: <=
DISPLAY i + " x " + j + " = " + (i * j) // Arithmetic Operator: *
SET j TO j + 1 // Arithmetic Operator: +
END WHILE
SET i TO i + 1 // Arithmetic Operator: +
END WHILE
Pseudocode with Operators
17. Operator Type Description
<= Comparison Operator Checks if the left value is less or equal
+ Arithmetic Operator Adds numbers or strings (concatenation)
* Arithmetic Operator Multiplies numbers
= Assignment Operator Assigns value to a variable
Operators Used Pseudocode
18. SET number = 7
IF number % 2 == 0 THEN // % is Modulus Operator
(remainder)
DISPLAY "Even Number"
ELSE
DISPLAY "Odd Number"
END IF
Check if a number is even or odd
Operators Used:
•% → Arithmetic Operator (modulus)
•== → Comparison Operator (equal to)
•= → Assignment Operator
19. SET a = 12
SET b = 8
SET c = 15
IF a > b AND a > c THEN // Logical Operator: AND
DISPLAY "a is the largest"
ELSE IF b > a AND b > c THEN
DISPLAY "b is the largest"
ELSE
DISPLAY "c is the largest"
END IF
Find the largest of three numbers