0% found this document useful (0 votes)
7 views

9 - Control Structures

IT

Uploaded by

5c6z4c7qn9
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

9 - Control Structures

IT

Uploaded by

5c6z4c7qn9
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

DECISION CONTROL

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 { }.

2. You may have as many else if statements as


needed.

3. The condition inside the if statement must always


result/evaluate to either TRUE or FALSE.

4. Do not be confused between the assignment


operator (=) and the equality operator (==). When
comparing the equality of two values in the condition,
use the equality operator.
Note:
5. The keyword else if in the nested if statement is
written in two words and not as one word elseif.

6. The curly braces { } in the if block can be omitted


if there is only statement inside it.

7. The else part/block is optional.

8. The if-else if-else statement is preferable for


conditions that deal with value ranges.
switch Statement
- The switch statement, just like if statement, is
used to test conditions before executing a block of
codes. However, in switch, the value options on the
expression being tested is definite and
enumerated.
switch Statement
Syntax:
The expression inside the
switch is the entity to be
tested. Options for its value is
enumerated in different cases,
if the value of the expression
conforms to a case value, the
block of codes inside the case
will be executed.
If no case is tested TRUE, the default block is executed instead. The block of codes
for all cases as wells as the default is not enclosed by { }; rather, it is defined by the
start of the case value declaration and the break keyword which signifies the end of
the block for a case and the switch.
Sample Program:
Sample Program:
Note:
1. The default case in switch is optional just like the else in if-
else if-else statement.

2. Never forget to put the keyword break in every end of a case


block in a switch statement.

3. if-else if-else statements are used for ranges whereas the


switch statement is for definite non-range values.

4. A variable’s scope refers to its lifeline in the program flow. A


variable’s life or existence in the computer’s memory ends at
the end of the block where it was declared.
Exercises:
1. Create a program that would determine whether an input
integer number is odd or even. An even number is a number
that is exactly divisible by two, otherwise, it is odd.

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:

Print the inputted percentage grade, its


equivalent grade in UNP grading
system, and its remark.
Thank you!

You might also like