SlideShare a Scribd company logo
3
Most read
4
Most read
22
Most read
Made by:
Abhishek Kasana
Abhishek Sharma
Saurabh Aggarwal
Harsh Dabas
Flow Of Control
Flow of control is basically of three types:
 1) Sequential flow of
control
 2) Selection flow of
control
 3) Iteration flow of
control
Selection Flow Of Control
 Selection flow of
control is also known as
selective execution.
Here we may select one
of the two blocks to
execute based on a
certain condition.
Execution of one block
excludes the other .
Types Of Selection Flow Of Control
C++ provides two
Selection Statements:
 Single Branching
Statement [ If ()….else]
 Multiple Branching
Statement
[ Switch()..case]
if-else
Selection
switch
Single Branching Statement
*if()…else+
Single Branching statement is very versatile form of
selection statement. It offers following types of
selection.
 If
 If()…else
 Nested if
 Else if
The if Statement Of C++
 An if statement test a particular condition; if the condition evaluates to
true, a course-of-action is followed i.e., statement(s) following are
executed. Otherwise the course-of –action is ignored.
 Syntax:
if (expression )
{
statement(s);
}
Where if is the keyword, expression
is a booleon expression within a set
of parenthesis and statement can be
a simple or compound statement.
Some test expressions:
 (a) if(grade==‘A’)
cout<<“You Did Well”;
 (b) if(a>b)
cout<<“A has more than B has.”;
 (c) if(x)
{ cout<<“x is non-zeron”;
cout<<“Hence it results into “;
}
 (d) if((x>=2)&&(x<=10))
{
cout<<“A compound test condition resulted truen”;
}
Remember:
 A false value is 0 in C++ and a non–zero value is
considered true in C++.
 Please note that if(x) type of condition might not work
in newer compilers. Though it works in Turbo-C++, for
newer compilers like codeblocks, we change the
condition to if(x!=0) and change if(!x) into if(x==0).
Selection if( )…else …
 Also possible to make two way selection
 If the expression is
true, statement1 is
executed
 Otherwise statement2
is executed
 Syntax;
if (expression)
{
statement(s)1
}
else
{
statement(s)2
}
Always Remember:
 In an if-else statement, only the code associated with if
(i.e., statement 1) or the code associated with else (i.e.,
statement 2) executes, never both.
One or more if statements embedded within the if statement are
called nested ifs.
The following if-else statement is a nested if declaration nested to
level two:
Nested if
Nested if can have following the forms:
1)if nested inside if -
part
if(expresssion1)
{ :
if(expression2)
statement 1;
else
statement 2;
:
}
else
body-of-the-else;
2)If nested inside
else part
if (expression1)
body-of-if;
Else
{: if (expression 2)
statement-1;
else
statement-2;
:
}
3)If nested inside
both if part and else
part
If (exprssion1)
{ :
if (expression2)
statement 1;
else
statement 2;
:
}
Else
{ :
if(expression 3)
statement 3;
else statement 4;
:
}
The if else if ladder
 A common programming construct in C++ is the if-else-if ladder
,which is often also called the if-else-if staircase because of its
appearance . It takes the following general form:
if (expression1) statement 1;
else
if (expression 2) statement 2;
else
if (expression3)statement 3:
:
else statement n:
This can also be written as :
If (expression 1)
statement 2;
Else if (expression 2)
statement 2;
Else if (expression 3)
statement 3:
:
Else
statement n:
Graphical representation of if-else-if ladder:
The Dangling Else Problem
The nested if – else statement introduces a source of
potential ambiguity referred to as dangling else
problem. This problem arises when in a nested if
statement, number of ifs is more than the number of
else clause. This question then arise , with which if
does the additional else clause properly match up.
16
The Dangling else
 How to determine which if the else goes with
 Example:
if (abs (x - 7))
if (x < 7) cout << “x approaches 7 from left”;
else
cout << “x approaches 7 from the right”;
else
cout << “x not close to 7”;
Rule : An else goes with the closest unmatched if
?
?
17
The Dangling Else
 Rule : an else goes with the closest unmatched if
 Consider … how do you force an else to go with a
previous if?
if (x < y)
if (y > 3) cout << “message about y > 3”;
else cout << “message about x and y”;
if (x < y)
{ if (y > 3) cout << “message about y > 3”; }
else cout << “message about x and y”;
Use { curly brackets } to nest the
statements
This statement is used when we have to select one option out of many
alternatives.
It is a multi branch statement that makes the control to jump to one of
the several
statements based on the value of an integer variable or an expression.
The general
Form of the switch is:
switch(expression)
{
case constant 1: statement sequence 1;
break;
case constant 2: statement sequence 2;
break;
case constant 3: statement sequence 3;
break;
.
.
case constant n-1: statement sequence n-1;
break;
default: statement sequence;
}
Multiple Branching Statement
Switch statment
Selection statements
Nested Switch
Like if statement , a switch statement can also be nested .There can be a switch
as part of the statement sequence of another switch.
Example:
Switch(a)
{
Case 1 : switch(b)
{
case 0: cout<<“divide by Zero ---Error!!”;
break;
case 1: res=a/b;
}
break;
Case 2 :
:
:
}
Some important things to know about Switch
 A switch statement can work only for equality comparisons.
 No two case labels in the same switch can have identical values. But, in
case of nested switch statements the case constant of the inner and
outer switch can contain common values.
 If character constants are used in the switch statement , they are
automatically converted to their integers(i.e.. their equivalent ASCII
codes) .
 Switch Statement is more efficient than if in a situation that supports
the nature of switch operation.
 If a case statement does not include a break statement then fallthrough
occurs.
 Default statement gets executed when no match is found. The default
statement is optional and , if it is missing , no action takes place if all
matches fail.
Switch vs. If Else
S.no Switch If Else
1 The Expression is tested for equality only
.
The expression cam be tested for
inequality as well. (<,>)
2 Only one value is used to match against
all case labels.
Multiple expression can be tested for
branching.
3 Switch case is not effective when
checking for a range value.
If else is a better option to check ranges.
4 Switch case cannot handle floating point
values
If else can handle floating point values
5 The case label must be
constant(Characters of integers).
If else can use variables also for
conditions.
6 Switch statement provides a better way to
check a value against a number of
constants .
If else is not suitable for this porpose .
Conclusion
 C++ provides two kinds of selection statements: if and
switch.
 The if-else statement tests an expression and depending
upon its truth value, one of the two sets-of-action is
executed.
 The if-else can be nested also i.e., an if statement can
have another if statement .In a nested if-else statement, an
else goes to immediately preceding unmatched if .
 A switch is another selection statement in C++ that tests a
value against a set of integer or character constants.
 A switch statement can be nested also .
 An if-else is more flexible and versatile compared to
switch but switch is more efficient in a situation when the
same variable is compared against a set of values for
equality.
Selection statements

More Related Content

PPTX
C++ decision making
Zohaib Ahmed
 
PPTX
Switch case in C++
Barani Govindarajan
 
PDF
10. switch case
Way2itech
 
PPTX
Decision making statements in C programming
Rabin BK
 
PDF
Control statements
Kanwalpreet Kaur
 
PPTX
If else statement in c++
Bishal Sharma
 
PPTX
Presentation on C Switch Case Statements
Dipesh Panday
 
C++ decision making
Zohaib Ahmed
 
Switch case in C++
Barani Govindarajan
 
10. switch case
Way2itech
 
Decision making statements in C programming
Rabin BK
 
Control statements
Kanwalpreet Kaur
 
If else statement in c++
Bishal Sharma
 
Presentation on C Switch Case Statements
Dipesh Panday
 

What's hot (20)

PPT
Repetition Structure
PRN USM
 
PDF
Conditional operators
BU
 
PPTX
Control statements in c
Sathish Narayanan
 
PPTX
Operators and expressions in c language
tanmaymodi4
 
PPTX
Functions in c++
Rokonuzzaman Rony
 
PPTX
Control and conditional statements
rajshreemuthiah
 
PPTX
Loops in java script
Ravi Bhadauria
 
PPT
Operators in C++
Sachin Sharma
 
PPTX
C Programming: Control Structure
Sokngim Sa
 
PPTX
Variables, Data Types, Operator & Expression in c in detail
gourav kottawar
 
PPTX
Operators and expressions
vishaljot_kaur
 
PPTX
Exception handling c++
Jayant Dalvi
 
PPTX
Control structures in c++
Nitin Jawla
 
PPTX
Functions in c
kalavathisugan
 
PPTX
Function C programming
Appili Vamsi Krishna
 
PPTX
Passing an Array to a Function (ICT Programming)
Fatima Kate Tanay
 
PPTX
Expression and Operartor In C Programming
Kamal Acharya
 
PPTX
Conditional and control statement
narmadhakin
 
PPTX
Conditional statement c++
amber chaudary
 
PPTX
Operators and expressions in C++
Neeru Mittal
 
Repetition Structure
PRN USM
 
Conditional operators
BU
 
Control statements in c
Sathish Narayanan
 
Operators and expressions in c language
tanmaymodi4
 
Functions in c++
Rokonuzzaman Rony
 
Control and conditional statements
rajshreemuthiah
 
Loops in java script
Ravi Bhadauria
 
Operators in C++
Sachin Sharma
 
C Programming: Control Structure
Sokngim Sa
 
Variables, Data Types, Operator & Expression in c in detail
gourav kottawar
 
Operators and expressions
vishaljot_kaur
 
Exception handling c++
Jayant Dalvi
 
Control structures in c++
Nitin Jawla
 
Functions in c
kalavathisugan
 
Function C programming
Appili Vamsi Krishna
 
Passing an Array to a Function (ICT Programming)
Fatima Kate Tanay
 
Expression and Operartor In C Programming
Kamal Acharya
 
Conditional and control statement
narmadhakin
 
Conditional statement c++
amber chaudary
 
Operators and expressions in C++
Neeru Mittal
 
Ad

Similar to Selection statements (20)

PPT
Flow of Control
Praveen M Jigajinni
 
PPTX
Introduction to Selection control structures in C++
Neeru Mittal
 
PPTX
Flow of control C ++ By TANUJ
TANUJ ⠀
 
PPTX
Lec-5-IF-ELSE-SWITCH Programming Fundamentals.pptx
season12id
 
PPT
CHAPTER-3a.ppt
Tekle12
 
PDF
Selection
Jason J Pulikkottil
 
PPTX
Lec16.pptx problem specifications computer
samiullahamjad06
 
PPTX
C++ programming Unit 5 flow of control
AAKASH KUMAR
 
PDF
controlflowwSoftware Development Fundamentals (SDF) – I ODD 2024 Jaypee Insti...
ruvirgandhi123
 
PDF
Chapter 2 - Flow of Control Part I.pdf
KirubelWondwoson1
 
PPTX
Chapter 3 Conditional Statements&Looping (1).pptx
burkagemechu
 
PPTX
IF & SWITCH (conditional control) in computer science
RaianaTabitha
 
PPTX
CPP04 - Selection
Michael Heron
 
PPT
Control structures in C++ Programming Language
Ahmad Idrees
 
PPTX
POLITEKNIK MALAYSIA
Aiman Hud
 
PDF
Chap 4 c++
Venkateswarlu Vuggam
 
PPT
C++ chapter 4
SHRIRANG PINJARKAR
 
PPTX
week 3 Programming lecture 05 (1) j.pptx
ZainabNoor83
 
PDF
Unit ii chapter 2 Decision making and Branching in C
Sowmya Jyothi
 
PPT
Chap 4 c++
Venkateswarlu Vuggam
 
Flow of Control
Praveen M Jigajinni
 
Introduction to Selection control structures in C++
Neeru Mittal
 
Flow of control C ++ By TANUJ
TANUJ ⠀
 
Lec-5-IF-ELSE-SWITCH Programming Fundamentals.pptx
season12id
 
CHAPTER-3a.ppt
Tekle12
 
Lec16.pptx problem specifications computer
samiullahamjad06
 
C++ programming Unit 5 flow of control
AAKASH KUMAR
 
controlflowwSoftware Development Fundamentals (SDF) – I ODD 2024 Jaypee Insti...
ruvirgandhi123
 
Chapter 2 - Flow of Control Part I.pdf
KirubelWondwoson1
 
Chapter 3 Conditional Statements&Looping (1).pptx
burkagemechu
 
IF & SWITCH (conditional control) in computer science
RaianaTabitha
 
CPP04 - Selection
Michael Heron
 
Control structures in C++ Programming Language
Ahmad Idrees
 
POLITEKNIK MALAYSIA
Aiman Hud
 
C++ chapter 4
SHRIRANG PINJARKAR
 
week 3 Programming lecture 05 (1) j.pptx
ZainabNoor83
 
Unit ii chapter 2 Decision making and Branching in C
Sowmya Jyothi
 
Ad

Recently uploaded (20)

PPTX
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
PPTX
Presentation about variables and constant.pptx
kr2589474
 
PPT
Why Reliable Server Maintenance Service in New York is Crucial for Your Business
Sam Vohra
 
PPTX
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
PPTX
AI-Ready Handoff: Auto-Summaries & Draft Emails from MQL to Slack in One Flow
bbedford2
 
PDF
Exploring AI Agents in Process Industries
amoreira6
 
PDF
Immersive experiences: what Pharo users do!
ESUG
 
PPTX
Explanation about Structures in C language.pptx
Veeral Rathod
 
PPTX
TRAVEL APIs | WHITE LABEL TRAVEL API | TOP TRAVEL APIs
philipnathen82
 
PPTX
Role Of Python In Programing Language.pptx
jaykoshti048
 
PPTX
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
Adobe Illustrator Crack Full Download (Latest Version 2025) Pre-Activated
imang66g
 
PDF
lesson-2-rules-of-netiquette.pdf.bshhsjdj
jasmenrojas249
 
PDF
10 posting ideas for community engagement with AI prompts
Pankaj Taneja
 
PDF
What to consider before purchasing Microsoft 365 Business Premium_PDF.pdf
Q-Advise
 
PDF
Generating Union types w/ Static Analysis
K. Matthew Dupree
 
PDF
Key Features to Look for in Arizona App Development Services
Net-Craft.com
 
PPTX
The-Dawn-of-AI-Reshaping-Our-World.pptxx
parthbhanushali307
 
PPTX
Presentation about variables and constant.pptx
safalsingh810
 
PPTX
GALILEO CRS SYSTEM | GALILEO TRAVEL SOFTWARE
philipnathen82
 
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
Presentation about variables and constant.pptx
kr2589474
 
Why Reliable Server Maintenance Service in New York is Crucial for Your Business
Sam Vohra
 
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
AI-Ready Handoff: Auto-Summaries & Draft Emails from MQL to Slack in One Flow
bbedford2
 
Exploring AI Agents in Process Industries
amoreira6
 
Immersive experiences: what Pharo users do!
ESUG
 
Explanation about Structures in C language.pptx
Veeral Rathod
 
TRAVEL APIs | WHITE LABEL TRAVEL API | TOP TRAVEL APIs
philipnathen82
 
Role Of Python In Programing Language.pptx
jaykoshti048
 
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Adobe Illustrator Crack Full Download (Latest Version 2025) Pre-Activated
imang66g
 
lesson-2-rules-of-netiquette.pdf.bshhsjdj
jasmenrojas249
 
10 posting ideas for community engagement with AI prompts
Pankaj Taneja
 
What to consider before purchasing Microsoft 365 Business Premium_PDF.pdf
Q-Advise
 
Generating Union types w/ Static Analysis
K. Matthew Dupree
 
Key Features to Look for in Arizona App Development Services
Net-Craft.com
 
The-Dawn-of-AI-Reshaping-Our-World.pptxx
parthbhanushali307
 
Presentation about variables and constant.pptx
safalsingh810
 
GALILEO CRS SYSTEM | GALILEO TRAVEL SOFTWARE
philipnathen82
 

Selection statements

  • 1. Made by: Abhishek Kasana Abhishek Sharma Saurabh Aggarwal Harsh Dabas
  • 2. Flow Of Control Flow of control is basically of three types:  1) Sequential flow of control  2) Selection flow of control  3) Iteration flow of control
  • 3. Selection Flow Of Control  Selection flow of control is also known as selective execution. Here we may select one of the two blocks to execute based on a certain condition. Execution of one block excludes the other .
  • 4. Types Of Selection Flow Of Control C++ provides two Selection Statements:  Single Branching Statement [ If ()….else]  Multiple Branching Statement [ Switch()..case] if-else Selection switch
  • 5. Single Branching Statement *if()…else+ Single Branching statement is very versatile form of selection statement. It offers following types of selection.  If  If()…else  Nested if  Else if
  • 6. The if Statement Of C++  An if statement test a particular condition; if the condition evaluates to true, a course-of-action is followed i.e., statement(s) following are executed. Otherwise the course-of –action is ignored.  Syntax: if (expression ) { statement(s); } Where if is the keyword, expression is a booleon expression within a set of parenthesis and statement can be a simple or compound statement.
  • 7. Some test expressions:  (a) if(grade==‘A’) cout<<“You Did Well”;  (b) if(a>b) cout<<“A has more than B has.”;  (c) if(x) { cout<<“x is non-zeron”; cout<<“Hence it results into “; }  (d) if((x>=2)&&(x<=10)) { cout<<“A compound test condition resulted truen”; }
  • 8. Remember:  A false value is 0 in C++ and a non–zero value is considered true in C++.  Please note that if(x) type of condition might not work in newer compilers. Though it works in Turbo-C++, for newer compilers like codeblocks, we change the condition to if(x!=0) and change if(!x) into if(x==0).
  • 9. Selection if( )…else …  Also possible to make two way selection  If the expression is true, statement1 is executed  Otherwise statement2 is executed  Syntax; if (expression) { statement(s)1 } else { statement(s)2 }
  • 10. Always Remember:  In an if-else statement, only the code associated with if (i.e., statement 1) or the code associated with else (i.e., statement 2) executes, never both.
  • 11. One or more if statements embedded within the if statement are called nested ifs. The following if-else statement is a nested if declaration nested to level two: Nested if
  • 12. Nested if can have following the forms: 1)if nested inside if - part if(expresssion1) { : if(expression2) statement 1; else statement 2; : } else body-of-the-else; 2)If nested inside else part if (expression1) body-of-if; Else {: if (expression 2) statement-1; else statement-2; : } 3)If nested inside both if part and else part If (exprssion1) { : if (expression2) statement 1; else statement 2; : } Else { : if(expression 3) statement 3; else statement 4; : }
  • 13. The if else if ladder  A common programming construct in C++ is the if-else-if ladder ,which is often also called the if-else-if staircase because of its appearance . It takes the following general form: if (expression1) statement 1; else if (expression 2) statement 2; else if (expression3)statement 3: : else statement n: This can also be written as : If (expression 1) statement 2; Else if (expression 2) statement 2; Else if (expression 3) statement 3: : Else statement n:
  • 14. Graphical representation of if-else-if ladder:
  • 15. The Dangling Else Problem The nested if – else statement introduces a source of potential ambiguity referred to as dangling else problem. This problem arises when in a nested if statement, number of ifs is more than the number of else clause. This question then arise , with which if does the additional else clause properly match up.
  • 16. 16 The Dangling else  How to determine which if the else goes with  Example: if (abs (x - 7)) if (x < 7) cout << “x approaches 7 from left”; else cout << “x approaches 7 from the right”; else cout << “x not close to 7”; Rule : An else goes with the closest unmatched if ? ?
  • 17. 17 The Dangling Else  Rule : an else goes with the closest unmatched if  Consider … how do you force an else to go with a previous if? if (x < y) if (y > 3) cout << “message about y > 3”; else cout << “message about x and y”; if (x < y) { if (y > 3) cout << “message about y > 3”; } else cout << “message about x and y”; Use { curly brackets } to nest the statements
  • 18. This statement is used when we have to select one option out of many alternatives. It is a multi branch statement that makes the control to jump to one of the several statements based on the value of an integer variable or an expression. The general Form of the switch is: switch(expression) { case constant 1: statement sequence 1; break; case constant 2: statement sequence 2; break; case constant 3: statement sequence 3; break; . . case constant n-1: statement sequence n-1; break; default: statement sequence; } Multiple Branching Statement Switch statment
  • 20. Nested Switch Like if statement , a switch statement can also be nested .There can be a switch as part of the statement sequence of another switch. Example: Switch(a) { Case 1 : switch(b) { case 0: cout<<“divide by Zero ---Error!!”; break; case 1: res=a/b; } break; Case 2 : : : }
  • 21. Some important things to know about Switch  A switch statement can work only for equality comparisons.  No two case labels in the same switch can have identical values. But, in case of nested switch statements the case constant of the inner and outer switch can contain common values.  If character constants are used in the switch statement , they are automatically converted to their integers(i.e.. their equivalent ASCII codes) .  Switch Statement is more efficient than if in a situation that supports the nature of switch operation.  If a case statement does not include a break statement then fallthrough occurs.  Default statement gets executed when no match is found. The default statement is optional and , if it is missing , no action takes place if all matches fail.
  • 22. Switch vs. If Else S.no Switch If Else 1 The Expression is tested for equality only . The expression cam be tested for inequality as well. (<,>) 2 Only one value is used to match against all case labels. Multiple expression can be tested for branching. 3 Switch case is not effective when checking for a range value. If else is a better option to check ranges. 4 Switch case cannot handle floating point values If else can handle floating point values 5 The case label must be constant(Characters of integers). If else can use variables also for conditions. 6 Switch statement provides a better way to check a value against a number of constants . If else is not suitable for this porpose .
  • 23. Conclusion  C++ provides two kinds of selection statements: if and switch.  The if-else statement tests an expression and depending upon its truth value, one of the two sets-of-action is executed.  The if-else can be nested also i.e., an if statement can have another if statement .In a nested if-else statement, an else goes to immediately preceding unmatched if .  A switch is another selection statement in C++ that tests a value against a set of integer or character constants.  A switch statement can be nested also .  An if-else is more flexible and versatile compared to switch but switch is more efficient in a situation when the same variable is compared against a set of values for equality.