0% found this document useful (0 votes)
5 views12 pages

Conditional Statements in C

The document provides an overview of conditional statements in C++, including their purpose and types such as if, if-else, nested if-else, if-else-if ladder, and switch statements. Each type is explained with syntax and examples to illustrate their usage in decision-making within C++ programs. Conditional statements are essential for controlling the flow of program execution based on evaluated conditions.

Uploaded by

sanjivsem
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)
5 views12 pages

Conditional Statements in C

The document provides an overview of conditional statements in C++, including their purpose and types such as if, if-else, nested if-else, if-else-if ladder, and switch statements. Each type is explained with syntax and examples to illustrate their usage in decision-making within C++ programs. Conditional statements are essential for controlling the flow of program execution based on evaluated conditions.

Uploaded by

sanjivsem
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/ 12

C++ Programming

Topperworld.in

Conditional Statements

 The conditional statements (also known as decision control structures)


such as if, if else, switch, etc. are used for decision-making purposes in
C++ programs.
 They are also known as Decision-Making Statements and are used to
evaluate one or more conditions and make the decision whether to
execute a set of statements or not.
 These decision-making statements in programming languages decide the
direction of the flow of program execution.

 Need of 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.

 Types of Conditional Statements in C++

1. if in C++

 The if statement is the most simple decision-making statement.


 It is used to decide whether a certain statement or block of statements
will be executed or not i.e if a certain condition is true then a block of
statements is executed otherwise not.

Syntax:
if(condition)
{
// Statements to execute if // condition is true
}

©Topperworld
C++ Programming

 Here, the condition after evaluation will be either true or false. C if


statement accepts boolean values – if the value is true then it will
execute the block of statements below it otherwise not.
 If we do not provide the curly braces ‘{‘ and ‘}’ after if(condition) then by
default if statement will consider the first immediately below statement
to be inside its block.

Flowchart:

©Topperworld
C++ Programming

Example:

// C++ program to illustrate If statement


#include <iostream>
using namespace std;

int main()
{
int i = 10;

if (i > 15) {
cout <<
"10 is
greater
than
15";
}

cout << "I am Not in if";

Output:return 0;
}
I am Not in if

2. if-else in C++

 The if statement alone tells us that if a condition is true it will execute a


block of statements and if the condition is false it won’t. But what if we
want to do something else when the condition is false?

©Topperworld
C++ Programming

 Here comes the C++ else statement. We can use the else statement with
the if statement to execute a block of code when the condition is false.
 The if-else statement consists of two blocks, one for false expression
and one for true expression.

Syntax:

if (condition)
{
// Executes this block if // condition is true
}else
{
// Executes this block if // condition is false
}

Flowchart:

©Topperworld
C++ Programming

Example:

// C++ program to illustrate if-else statement

#include <iostream>
using namespace std;

int main()
{
int i = 20;

if (i < 15)
cout <<
"i is
smaller
than
15";
else
cout <<
"i is
greater
Output than
15";
i is greater than 15
return 0;
The block
} of code following the else statement is executed as the condition
present in the if statement is false.

©Topperworld
C++ Programming

3. Nested if-else in C++

 A nested if in C is an if statement that is the target of another if


statement. Nested if statements mean an if statement inside another if
statement. Yes, C++ allow us to nested if statements within if statements,
i.e, we can place an if statement inside another if statement.

Syntax:

if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
else
{
// Executes when condition2 is false
}

Flowchart:

©Topperworld
C++ Programming

Example:

// C++ program to illustrate nested-if statement


#include <iostream>
using namespace std;

int main()
{
int i = 10;

if (i == 10) {
// First if statement
if (i < 15)
cout << "i is
smaller than
15\n";

if (i < 12)
cout << "i is
smaller than
12 too\n";
else
cout << "i is
greater than
15";
}

Output:
return 0;
i }is smaller than 15
i is smaller than 12 too

©Topperworld
C++ Programming

4. if-else-if Ladder in C++

 The if else if statements are used when the user has to decide among
multiple options.
 The C++ if statements are executed from the top down. As soon as one
of the conditions controlling the if is true, the statement associated
with that if is executed, and the rest of the C++ else-if ladder is
bypassed.
 If none of the conditions is true, then the final else statement will be
executed. if-else-if ladder is similar to the switch statement.

Syntax:
if (condition)
statement;else if (condition)
statement;
.
.
else
statement;

Flowchart:

©Topperworld
C++ Programming

Example:

// C++ program to illustrate if-else-if ladder


#include <iostream>
using namespace std;
int main()
{
int i = 20;

if (i == 10)
cout << "i is 10";
else if (i == 15)
cout << "i is 15";
else if (i == 20)
cout << "i
is 20";
else
cout << "i is not present";
return 0;
}

Output

i is 20

©Topperworld
C++ Programming

5. switch Statement in C++

 The switch case statement is an alternative to the if else if ladder that


can be used to execute the conditional code based on the value of the
variable specified in the switch statement. The switch block consists of
cases to be executed based on the value of the switch variable.

Syntax :
switch (expression) {
case value1:
statements;
case value2:
statements;
....
....
....
default:
statements;
}

Note: The switch expression should evaluate to either integer or


character. It cannot evaluate any other data type.

©Topperworld
C++ Programming

Flowchart:

Example:

// C++ Program to illustrate the use of switch statement

#include <iostream>
using namespace std;
// driver code
int main()
{

// variable to be used in switch statement


int var = 2;
// declaring switch cases

switch (var) {
case 1:
cout << "Case 1 is executed";
break;

©Topperworld
C++ Programming

case 2:
cout << "Case 2 is executed";
break;
default:
cout << "Default Case is executed";
break;
}
return 0;

Output

Case 2 is executed

©Topperworld

You might also like