2. Agenda of Lecture
The if Selection
The if/Else Selection
Nested if/else Selection
3. Decision Making in C++
Decision making is about deciding the order of execution of statements based on certain conditions or
repeat a group of statements until certain specified conditions are met.
C++ handles decision-making by supporting the following statements,
If statement
switch statement
conditional operator statement
goto statement
4. Decision Making with If statement
The if statement may be implemented in different forms depending on the complexity of conditions to
be tested. The different forms are:
Simple if statement
If…else statement
Nested if…else statement
else if statement
5. Simple If statement
The general form of a simple if statement is,
If (expression)
{
Statement-inside;
}
Statement-outside;
If expression is true, then statement-inside is executed, other wise only statement-outside will
be executed.
6. The if Selection Structure
Selection structure:
Used to choose among alternative courses of action
Pseudocode:
If student’s grade is greater than or equal to 60
Print “Passed”
If condition true
Print statement executed and program goes on to next statement
If false, print statement is ignored and the program goes onto the next statement
Indenting makes programs easier to read
C++ ignores whitespace characters
7. The if Selection Structure
Pseudocode statement in C++:
If grade >= 60
Print “Passed”
if ( grade >= 60 )
cout<<"Passedn";
C++ code corresponds closely to the pseudocode
Diamond symbol (decision symbol)
Indicates decision is to be made
Contains an expression that can be true or false
Test the condition, follow appropriate path
8. The if Selection Structure
if structure is a single-entry/single-exit structure
true
false
grade >= 60 print “Passed”
A decision can be made
on any expression.
zero - false
nonzero – true
Example:
3 - 4 is true
9. Simple If statement - Example
#include<iostream.h>
int main()
{
int x,y;
x=15;
y=13;
If(x > y)
{
cout<<“x is greater than y”;
}
return 0;
}
10. The if/else Selection Structure
Test for multiple cases by placing if/else selection structures inside if/else selection
structures
11. The if/else Selection Structure
Pseudocode:
If student’s grade is greater than or equal to 60
Print “Passed”
Else
Print “Failed”
C++ code:
if ( grade >= 60 )
cout <<"Passedn";
else
cout << "Failedn";
Ternary conditional operator (?:)
cout << ( grade >= 60 ? "Passed" : "Failed" );
grade >= 60 ? cout << "Passed" : cout << "Failed";
12. The if/else Selection Structure
The general form of a simple if/else statement is,
If (expression)
{
Statement-block1;
}
else
{
Statement-block2;
}
If expression is true, then statement-block1 is executed, other wise only statement-
block2 will be executed.
13. The if/else Selection -Example
#include<iostream.h>
int main()
{
int x,y;
x=15;
y=13;
if(x > y){
cout<<“x is greater than y”;
}
else{
cout<<“y is greater than x”;
}
return 0;}
14. Nested if…else Selection - Statement(general-form)
if (expression)
{
if (expression1)
{
Statement-block1;
}
else
{
Statement-block2;
}
}
else
{
Statement-block3;
}
If ‘expression’ is false the ‘statement-
block3’ will be executed, otherwise it
continues to perform the test for
‘expression 1’ . If the ‘expression 1’ is
true the ‘statement block1’ is
executed otherwise ‘statement-block2’
is executed
15. Else…if Selection
The expression is tested from the top (of the ladder) downwards. As soon as the true condition is found, the
statement associated with it is executed.
17. Dangling - else Problem
The C++ compiler always associates an else with the immediately preceding if unless told to do
otherwise by the placement of braces ({ and }). This behavior can lead to what’s referred to as the
dangling-else problem.
(int x = 35; int y = 3; )
19. Blocks
The if selection statement expects only one statement in its body.
Similarly, the if and else parts of an if…else statement each expect only one body statement.
To include several statements in the body of an if or in either part of an if…else, enclose the
statements in braces ({ and }). A set of statements contained within a pair of braces is called a
compound statement or a block