You are on page 1of 11

Lesson 7

CONDITIONAL
EXPRESSIONS IN C++

Conditional Expressions 1
CONDITIONAL EXPRESSIONS IN C++

• In C++ if statement is used to check the


truthness of the expression.
• Condition written inside If statement
consists of a boolean expression.
• If the condition is determined to be true,
and optionally, other statements to be
executed if the condition is determined to
be false.
Conditional Expressions 2
IF STATEMENT

• An ‘if’ statement consists of a boolean


expression followed by one or more
statements.
– if(boolean_expression)
–{
– // statement(s) will execute if the
– // boolean expression is true
–}

Conditional Expressions 3
IF STATEMENT

Conditional Expressions 4
IF...ELSE STATEMENT

• An ‘if’ statement can be followed by an


optional ‘else’ statement, which executes
when the boolean expression is false.
– if(boolean_expression)
– { // True Block
–}
– else
– { // False Block
–} Conditional Expressions 5
IF...ELSE STATEMENT

Conditional Expressions 6
NESTED IF STATEMENTS

• You can use one ‘if’ or ‘else if’ statement


inside another ‘if’ or ‘else if’ statement(s).
– if(boolean_expression 1) – else if( boolean_expression 3)
– { – {
– // When expression 1 is true – // When expression 3 is true
– } – }
– else if( boolean_expression 2) – else
– { – {
– // When expression 2 is true – // When none of expression is true
– } – }

Conditional Expressions 7
NESTED IF STATEMENTS

Conditional Expressions 8
SWITCH STATEMENT

• A ‘switch’ statement allows a variable to be


tested for equality against a list of values.
– switch(expression) – break;
–{ – case value3 :
– Body3
– case value1 :
– break;
– Body1 – default :
– break; – default-body
– case value2 : – break;
– body2 Conditional Expressions –} 9
SWITCH STATEMENT

Conditional Expressions 10
Thank you...

Conditional Expressions 11

You might also like