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

Week 9 - Logical and Relational Operators

The document provides an overview of conditional statements in C++ programming, including Boolean expressions, relational operators, and various forms of conditional statements such as simple if, if-else, nested if-else, and switch statements. It explains the structure and usage of each type of statement, along with examples and flowcharts for better understanding. Additionally, it covers logical operators and rules applicable to switch statements.

Uploaded by

maryangelinebajo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Week 9 - Logical and Relational Operators

The document provides an overview of conditional statements in C++ programming, including Boolean expressions, relational operators, and various forms of conditional statements such as simple if, if-else, nested if-else, and switch statements. It explains the structure and usage of each type of statement, along with examples and flowcharts for better understanding. Additionally, it covers logical operators and rules applicable to switch statements.

Uploaded by

maryangelinebajo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

CONDITIONAL

STATEMENTS IN
C++ PROGRAMMING
BOOLEAN EXPRESSION

• Evaluates TRUE or FALSE statement

• Boolean Algebra
• Developed by George Boole

• A branch of discrete mathematics that is dedicated to the


study of the properties and the manipulation of logical
expressions
• Uses relational operators to compare expressions.
RELATIONAL OPERATORS

Used to compare the


value of two variables.
CONDITIONAL STATEMENTS

• also known as SELECTION STATEMENTS

• 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.
FORMS OF CONDITIONAL
STATEMENT
• Simple IF Statement

• IF – ELSE Statement

• NESTED IF Statement

• SWITCH Statement
SIMPLE IF STATEMENT

• a powerful statement for decision making and is used to


control the flow of execution of statements.

• 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; }
EXAMPLE OF SIMPLE IF
STATEMENT
Sample Code Sample Flowchart
EXAMPLE OF SIMPLE IF
STATEMENT
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
IF - ELSE STATEMENT
STRUCTURE
The structure of an if else statement is as follows:

if(condition)
{
statement 1;
}
else
{
statement 2;
}
EXAMPLE OF IF-ELSE
STATEMENT
Sample Code Sample Flowchart
if(Grade>=75)
{
cout<<“ Congratulations, You Passed!”;
}
else
{
cout<<“ You failed, better luck next
time!”;
}
EXAMPLE OF IF - ELSE
STATEMENT
NESTED IF - ELSE STATEMENT

• 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.
NESTED IF - ELSE STATEMENT
STRUCTURE
if(condition)

{
if(condition 1)
{ statement 1; }

else
{ statement 2; }
}
else
{ statement 3; }
EXAMPLE OF NESTED IF-ELSE
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!”;
}
}
SWITCH STATEMENT

• allows a variable to be tested for equality against a list


of values

• used when you have multiple possibilities for the if


statement
SWITCH STATEMENT STRUCTURE
switch(variable)
{
case 1:
//execute your code
break;

case n:
//execute your code
break;

default:
//execute your code
}
EXAMPLE OF SWITCH-CASE
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”;
}
THE FOLLOWING RULES APPLY
TO A SWITCH STATEMENT
1. 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.
THE FOLLOWING RULES APPLY
TO A SWITCH STATEMENT
2. 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.

3. When a break statement is reached, the switch


terminates, and the flow of control jumps to the next line
following the switch statement.
THE FOLLOWING RULES APPLY
TO A SWITCH STATEMENT
4. 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.
LOGICAL OPERATORS

Used to perform logical operations on the given two


variables.
LOGICAL OPERATORS
OPERATOR MEANING EXECUTION
&& AND Executes TRUE statement if all conditions
are TRUE.
|| OR Executes TRUE statement if either of the
conditions is TRUE.
! NOT Executes TRUE statement if the condition is
FALSE.
Executes FALSE statement if the condition
is TRUE
THE TRUTH
TABLE
EXAMPLE

You might also like