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

Conditional Statements in C++

Conditional statements are used to control program flow based on conditions and allow decisions based on conditions being true or false. Common conditional statements include if, if-else, if-else-if, nested if, ternary operator, and switch. They execute different code blocks depending on conditions.

Uploaded by

saiflaghari04
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)
37 views

Conditional Statements in C++

Conditional statements are used to control program flow based on conditions and allow decisions based on conditions being true or false. Common conditional statements include if, if-else, if-else-if, nested if, ternary operator, and switch. They execute different code blocks depending on conditions.

Uploaded by

saiflaghari04
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/ 29

Conditional

Statements
CONDITIONAL STATEMENTS

Conditional statements are used to control the flow


of a program based on certain conditions. They allow you
to make decisions and execute different code blocks
depending on whether a specified condition is true or
false.
For example, the statement “If the milk shop is open, bring
one liter of milk while returning home from college.”
CONDITIONAL STATEMENTS

Following statements can be used to evaluate the conditions:


• If
• If-else
• If-else-if
• Nested if
• Conditional Operator (Ternary Operator)
• Switch
CONDITIONAL STATEMENTS

IF STATEMENT:
The if statement is the most basic form of a conditional
statement. It executes a block of code only if the specified
condition is true.
CONDITIONAL STATEMENTS

IF STATEMENT:

It means that if condition is true, then execute the following


statements. If statements are more than one, then we must
use braces { } to write number of statements.
CONDITIONAL STATEMENTS

IF STATEMENT:

Then
If condition is Process
true
Flow Chart For IF
Statement
CONDITIONAL STATEMENTS
IF STATEMENT:
CONDITIONAL STATEMENTS
IF STATEMENT:
CONDITIONAL STATEMENTS
IF-ELSE STATEMENT:
If structure executes the block of statements when the
condition is true. Otherwise execute else block statements.
If-else structure allows the programmer to specify the
statement when condition is false.
CONDITIONAL STATEMENTS
IF-ELSE STATEMENT:

Else Then
Process If condition is Process
true

Flow Chart For


IF-ELSE Statement
CONDITIONAL STATEMENTS
IF-ELSE STATEMENT:
CONDITIONAL STATEMENTS
IF-ELSE STATEMENT:
CONDITIONAL STATEMENTS
IF-ELSE-IF STATEMENT:
if-else-if statement is used to execute different blocks of
code based on the evaluation of multiple conditions. It allows
you to make choices among multiple options.
CONDITIONAL STATEMENTS
IF-ELSE-IF STATEMENT:

False
Condition 1
False
Condition 2
False
Condition 3
True
True False
Condition N
True
True
Block 1 Block 2 Block 3 Block N Else Block
CONDITIONAL STATEMENTS
IF-ELSE-IF STATEMENT:
CONDITIONAL STATEMENTS
IF-ELSE-IF STATEMENT:
CONDITIONAL STATEMENTS
NESTED-IF STATEMENT:
A nested if statement is an if statement that is
contained within another if statement. Nested if statements
allow you to make more complex decisions based on multiple
conditions.
CONDITIONAL STATEMENTS
NESTED-IF STATEMENT:

True
Condition 1

True False
Condition 2

statements statements
CONDITIONAL STATEMENTS
NESTED-IF STATEMENT:
CONDITIONAL STATEMENTS
NESTED-IF STATEMENT:
CONDITIONAL STATEMENTS
Ternary Operator:
The ternary operator, also known as the conditional
operator, is a shorthand way of writing an if-else statement. It
allows you to conditionally assign a value to a variable or
expression.
CONDITIONAL STATEMENTS
Ternary Operator:

Else Then
Process If condition is Process
true
CONDITIONAL STATEMENTS
Ternary Operator:
CONDITIONAL STATEMENTS
Ternary Operator:
CONDITIONAL STATEMENTS
Switch Statement:
It allows you to execute different blocks of code based
on the value of a variable or expression. It is a more efficient
alternative to nested if-else statements when there are
multiple possible cases.
CONDITIONAL STATEMENTS
Switch Statement:
CONDITIONAL STATEMENTS
Switch Statement:
Any Question?
Thank You

You might also like