You are on page 1of 13

CONDITIONAL STATEMENTS

PREPARED BY: MR. JOHN PATRICK S. PAULINO

ICT S112: FUNDAMENTALS OF COMPUTING 1


LEARNING OBJECTIVES

• Identify the different types of conditional statements.


• Familiarize the basic syntax used in creating conditional statements.
• Develop simple programs using conditional statements.

ICT S112: FUNDAMENTALS OF COMPUTING 2


BOOLEAN EXPRESSION
• evaluates to TRUE or FALSE. Boolean Algebra (George Boole) is a branch of
discrete mathematics that is dedicated to the study of the properties and the
manipulation of logical expressions. The simplest kinds of Boolean expressions
use relational operators to compare two expressions.

ICT S112: FUNDAMENTALS OF COMPUTING 3


CONDITIONAL STATEMENTS
also known as SELECTION STATEMENTS, are used to make decisions based on
a given condition. If the condition evaluates to TRUE, a set of statements is
executed, otherwise another set of statements is executed.

TYPES OF CONDITIONAL STATEMENTS


• If Statement
• If…else Statement
• Nested If Statement
• Switch Statement
ICT S112: FUNDAMENTALS OF COMPUTING 4
SIMPLE IF STATEMENT
The IF STATEMENT is a powerful statement for decision making and is used to
control the flow of execution of statements. It is basically a two-way decision-
making statement and is used in conjunction with an expression.

The structure of an if statement is as follows:

if(condition)
{
statement;
}

ICT S112: FUNDAMENTALS OF COMPUTING 5


SIMPLE IF STATEMENT
SAMPLE CODE SAMPLE FLOWCHART

ICT S112: FUNDAMENTALS OF COMPUTING 6


IF…ELSE STATEMENT
• An if statement can be followed by an optional else statement, which executes when
the Boolean expression is false.
• If the Boolean expression evaluates to true, then the if block will be executed,
otherwise, the else block will be executed
The structure of an if else statement is as follows:

if(condition)
{
statement 1;
}
else
{
statement 2;
}
ICT S112: FUNDAMENTALS OF COMPUTING 7
IF…ELSE STATEMENT

SAMPLE CODE SAMPLE FLOWCHART

if(Grade>=75)
{
cout<<“ Congratulations, You Passed!”;
}
else
{
cout<<“ You failed, better luck next time!”;
}

ICT S112: FUNDAMENTALS OF COMPUTING 8


NESTED IF STATEMENT
• It is always legal to nest if-else statements, which means you can use one if or
else if statement inside another if or else if statement(s).
• If the Boolean expression evaluates to true, then the if block will be executed,
otherwise, the else block will be executed.

if(condition) {

if(condition 1){
statement 1;

} else {
statement 2;
}

} else {
ICT S112: FUNDAMENTALS OF COMPUTING 9
statement 3;
}
NESTED IF STATEMENT

SAMPLE CODE SAMPLE FLOWCHART


int ctr = 0;
int Password;
if(Password==“JRUat100”)
{
cout<<“ Login Successfully!”;
}
else
{
cout<<“Login Failed!”;
ctr++;
if (ctr==3){
cout<<“Please Try again later!”;
}
} ICT S112: FUNDAMENTALS OF COMPUTING 10
SWITCH STATEMENT

• A switch statement allows a switch(variable)


variable to be tested for {
case 1:
equality against a list of
//execute your code
values. break;
• Switch statement is used
case n:
when you have multiple
//execute your code
possibilities for the if break;
statement.
default:
//execute your code
ICT S112: FUNDAMENTALS OF COMPUTING
} 11
SWITCH STATEMENT
SAMPLE CODE SAMPLE FLOWCHART
int num = 2;
cout<<"Pls enter number";
cin>>num;
switch(num) {

case 1:
cout<<“You selected 1.”;
break;
case 2:
cout<<“You selected 2”;
break;
default:
cout<<“Pls select a number from 1-2”;
}
ICT S112: FUNDAMENTALS OF COMPUTING 12
THE FOLLOWING RULES APPLY TO A SWITCH
STATEMENT
• The expression used in a switch statement must have an integral or enumerated
type or a class type in which the class has a single conversion function to an integral
or enumerated type.
• You can have any number of case statements within a switch. Each case is followed
by the value to be compared to and a colon.
• When a break statement is reached, the switch terminates, and the flow of control
jumps to the next line following the switch statement.
• A switch statement can have an optional default case, which must appear at the end
of the switch. The default case can be used for performing a task when none of the
cases is true. No break is needed in the default case.

ICT S112: FUNDAMENTALS OF COMPUTING 13

You might also like