1. DECISION
STRUCTURES
THE IF-ELSE IF STATEMENT AND NESTED IF STATEMENTS IN C++
Presentators:
M. Tahir Bashir
Fahad Iftikhar
Bilal Ahmad
C++
@UMT 8-Apr-2016
2. CONDITIONAL STATEMENTS
• The C++ conditional statements are the:
• Therefore they are sometimes called selection statements
• Conditional statements give us the power to make basic decisions
• if statement
• if-else statement
• switch statement
• A conditional statement lets us choose which statement will be executed next
3. FLOW OF CONTROL
• Unless specified otherwise, the order of statement execution through a function is
linear: one statement after another in sequence
• Some programming statements allow us to:
• decide whether or not to execute a particular statement
• execute a statement over and over, repetitively
• These decisions are based on Boolean expressions (or conditions) that evaluate to
true or false
• The order of statement execution is called the flow of control
4. THE IF STATEMENT
• The if statement has the following syntax:
if ( condition )
statement;
if is a C++
reserved word
The condition must be a
Boolean expression. It must
evaluate to either true or false.
If the condition is true, the statement is executed.
If it is false, the statement is skipped.
5. LOGIC OF AN IF STATEMENT
condition
evaluated
statement
true
false
6. THE IF STATEMENT
• An example of an if statement:
if (FirstNo > SecondNo)
result = FirstNo - SecondNo;
cout<<"The resultant is"<< result;
• First the condition is evaluated -- the value of FirstNo is either greater than the
value of SecondNo, or it is not
• If the condition is true, the assignment statement is executed -- if it isn’t, it is
skipped.
• Either way, the call to cout is executed next
7. RELATIONAL OPERATORS
• A condition often uses one of C++'s equality operators or relational operators
== equal to
< less than
!= not equal to
• Note the difference between the equality operator (==) and the assignment
operator (=)
> greater than
<= less than or equal to
>= greater than or equal to
8. LOGICAL OPERATORS
• C++ provides logical operators.
• The binary logical operators combine two Boolean expressions into one.
• The unary logical operator switches the value of a Boolean expression.
• Binary logical operators have lower precedence than relational operators
• NOT has the same precedence as negation.
Operator Meaning Kind
&& AND Binary
|| OR Binary
! NOT Unary
9. LOGICAL NOT
• The logical NOT operation is also called logical negation or logical complement
• If some condition a is true, then !a is false; if a is false, then !a is true
• Logical expressions can be shown using a truth table
a !a
True False
False True
10. LOGICAL AND & LOGICAL OR
• The logical AND expression
a && b
is true if both a and b are true, and false otherwise
• The logical OR expression
a || b
is true if a or b or both are true, and false otherwise
11. IF-ELSE STATEMENT:
General form of an if-else statement:
if(BooleanExpression)
statement or block 1
else
statement or block 2
12. LOGIC OF AN IF-ELSE STATEMENT
condition
evaluated
statement1
true false
statement2
13. THE CONDITIONAL OPERATOR
• C++ provides and operator to create short expressions that work like if-else
statements.
BooleanExpression ? Value1 : Value2;
• If BooleanExpression is true, Value1 is returned
• If BooleanExpression is false, Value2 is returned
• Example:
if (score < 50)
cout<<“Sorry! You Have Failed…";
else
cout<<"You Have Successfully Passed! ";
14. THE IF-ELSE IF STATEMENT
• Sometimes you need to be able to test a series of conditions
• You can do this with the if-else if statement
• General form:
if (BooleanExpression1)
statement or block 1
else if (BooleanExpression2)
statement or block 2
else
statement or block 3
• If BooleanExpression1 is true, then statement or block 1 is executed.
• If BooleanExpression1 is false, then BooleanExpression2 is tested.
• If BooleanExpression2 is true, then statement or block 2 ais executed.
• If BooleanExpression2 is false, then statement or block 3 is executed.
Note: You can have as many if else clauses as is
needed.
15. NESTED IF STATEMENTS
• Nesting is enclosing one structure inside of another.
• A block in C++ can contain any valid C++ code, this includes other if statements:
if(BooleanExpression1) {
if(BooleanExpression2) {
statement1;
statement2;
}
statement3;
statement4;
}
• If BooleanExpression1 is true and BooleanExpression2 is true , what is executed?
• statement1 , statement2 , statement3 , statement4
• If BooleanExpression1 is true and BooleanExpression2 is false , what is executed?
• statement3 , statement4