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

Lesson 7 C Conditional Statements

Uploaded by

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

Lesson 7 C Conditional Statements

Uploaded by

j
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

C++ CONDITIONAL STATEMENTS

 There come situations in real life when we need to


make some decisions and based on these decisions,
we decide what should we do next.
 Similar situations arise in programming also where we
need to make some decisions and based on these
decisions we will execute the next block of code
WHAT ARE CONDITIONAL
STATEMENTS IN C++?
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
1. If Statement
The if statement is the
simplest decision making
statement. It is used to
decide whether a certain
statement or block of
statements will be
executed or not.
SYNTAX:
It means:
if(condition)
 If the condition evaluates to TRUE,
{ the code inside the body of If is
// Statements to execute if executed.
 If the conditions evaluates to
// condition is true False, the code inside the body of
If is skipped.
}
SAMPLE CODE 1: Problem
Problem
SAMPLE CODE 2 :
Find the age whether he or she can vote on the next election

#include <iostream>
using namespace std;
int main()
{
int age; cin >> age;

if(age == 21)
{
cout << "I can vote on the next election";
}
return 0;
}
2. If …Else Statement
Sometimes you have a condition and you want to execute
a block of code if condition is true and execute another
piece of code if the same condition is false. This can be
achieved in C++ using if-else statement.
SYNTAX:
if (condition)
It means:
{
// Executes this block if If the condition is TRUE, the code inside the body of
If is executed.
// condition is true The code inside the body of else is skipped from
execution.
}
If the condition evaluates False, the code inside the
else body of else is executed.
The code inside the vbody of If is skipped from
{ execution
// Executes this block if
// condition is false
}
Find the number whether it is greater than or
SAMPLE CODE 1: less than.
#include <iostream> using namespace std;

int main()
{
int num=900; I
if( num < 95 ){

cout<<"num is less than 95";


}
else {
cout<<"num is greater than 95";
}
return 0;
}
SAMPLE CODE 2:
EXERCISE:

You might also like