9 - Control Structures
9 - Control Structures
STRUCTURES
(Conditional Statements)
Objectives
At the end of the lesson, student should be able to:
1 Use if statement to test a condition before executing a block of
codes,
2 5
2 Use if-else statement for branching conditions, and
3
3 Use switch statement for conditions with specific options.
if Statement
• It is used to test a condition if it is TRUE before
executing the codes in its block. If the condition
is tested FALSE, the block of codes inside it will
not be executed and the point of execution will
jump at the end of the if block.
Syntax:
Sample Program
if-else Statement
• The if-else statement behaves similarly as the if
statement, however, in if-else, if the condition is
tested FALSE, the block of codes under else is
executed instead.
Syntax:
Sample Program:
if-else if-else Statement
- The if-else if-else statement is also called nested if.
This is used for multiple, branching, or series of
conditions.
Syntax:
Sample Program:
Note:
1. Indent codes that are inside a block. A block of
codes is enclosed with { }.
Sample Output1:
Enter a number: 98
98 is an even number.
Sample Output2:
Enter a number: 341
341 is an odd number.
Exercises:
2. Create a program that would convert a grade in percentage
format into its equivalent value in UNP grading system. Consider
the following matrix: