You are on page 1of 3

ALGORITHM AND PROGRAMMING : MODULE 2

Introduction to Decision Structure

A. Concept

A decision structure, also called branching operation, is a condition where the program
needs to fulfill a certain condition to run an operation(s). Decision structures will always
use either TRUE or FALSE to determine the next operation in a program. Take the
following program for example :

The program asks for a number between 1 and 5, and then a decision structure is
implemented to validate the input.

Another important thing to note is that a decision structure will always consist of one or
more block of statements.
B. If, Else, and Else If

There are 3 kinds of branching operations : If, Else, and Else If.

- IF : If is inclusive, meaning that only and IF ONLY the stated condition is fulfilled, will
the program be able to run the block of statement .
- ELSE : Else is exclusive, meaning that only if prior conditions are not fulfilled, will the
program be able to run the block of statement.
- ELSE IF : Else-if is inclusive-exclusive, meaning that only if prior conditions are not
fulfilled AND the stated condition is fulfilled will the program be able to run the
block of statement.

C. Nested Decision Structure


If a decision structure contains another decision structure, then that decision structure
is called a Nested Decision Structure.
D. Assignment

1. Write a program that prompts the user to enter 2 different username, along with a
password for each name.
2. The program then should ask the user to log in using one of the two usernames.
Assuming the wrong username is entered, the program should output an error
message.
3. The then user must input the correct password for the username. Assuming the
wrong password is entered, the program should output an error message.
4. If the username and password matched, the program should tell the user they have
successfully logged into the system.

~ That’s all, folks. Good luck! ~

You might also like