SlideShare ist ein Scribd-Unternehmen logo
1 von 24
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
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

Weitere ähnliche Inhalte

Was ist angesagt?

Data Type Conversion in C++
Data Type Conversion in C++Data Type Conversion in C++
Data Type Conversion in C++
Danial Mirza
 

Was ist angesagt? (20)

Operator.ppt
Operator.pptOperator.ppt
Operator.ppt
 
Switch statement, break statement, go to statement
Switch statement, break statement, go to statementSwitch statement, break statement, go to statement
Switch statement, break statement, go to statement
 
Nesting of if else statement & Else If Ladder
Nesting of if else statement & Else If Ladder Nesting of if else statement & Else If Ladder
Nesting of if else statement & Else If Ladder
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Exception handling in c++
Exception handling in c++Exception handling in c++
Exception handling in c++
 
CONTROL STRUCTURE IN VB
CONTROL STRUCTURE IN VBCONTROL STRUCTURE IN VB
CONTROL STRUCTURE IN VB
 
Data Type Conversion in C++
Data Type Conversion in C++Data Type Conversion in C++
Data Type Conversion in C++
 
While loop
While loopWhile loop
While loop
 
C lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshareC lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshare
 
Loops in c++ programming language
Loops in c++ programming language Loops in c++ programming language
Loops in c++ programming language
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
 
C++ decision making
C++ decision makingC++ decision making
C++ decision making
 
If-else and switch-case
If-else and switch-caseIf-else and switch-case
If-else and switch-case
 
Exception Handling in object oriented programming using C++
Exception Handling in object oriented programming using C++Exception Handling in object oriented programming using C++
Exception Handling in object oriented programming using C++
 
Loops in c
Loops in cLoops in c
Loops in c
 
Control structures repetition
Control structures   repetitionControl structures   repetition
Control structures repetition
 
Switch Case in C Programming
Switch Case in C ProgrammingSwitch Case in C Programming
Switch Case in C Programming
 
Increment and Decrement operators in C++
Increment and Decrement operators in C++Increment and Decrement operators in C++
Increment and Decrement operators in C++
 
Pseudocode
PseudocodePseudocode
Pseudocode
 
Control structures in c++
Control structures in c++Control structures in c++
Control structures in c++
 

Ähnlich wie Selection statements

Chapter 8 - Conditional Statement
Chapter 8 - Conditional StatementChapter 8 - Conditional Statement
Chapter 8 - Conditional Statement
Deepak Singh
 
C statements.ppt presentation in c language
C statements.ppt presentation in c languageC statements.ppt presentation in c language
C statements.ppt presentation in c language
chintupro9
 

Ähnlich wie Selection statements (20)

Flow of Control
Flow of ControlFlow of Control
Flow of Control
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
 
Flow of control by deepak lakhlan
Flow of control by deepak lakhlanFlow of control by deepak lakhlan
Flow of control by deepak lakhlan
 
itft-Decision making and branching in java
itft-Decision making and branching in javaitft-Decision making and branching in java
itft-Decision making and branching in java
 
Chapter 8 - Conditional Statement
Chapter 8 - Conditional StatementChapter 8 - Conditional Statement
Chapter 8 - Conditional Statement
 
Chapter 4(1)
Chapter 4(1)Chapter 4(1)
Chapter 4(1)
 
Decision control structures
Decision control structuresDecision control structures
Decision control structures
 
Decision Making and Branching in C
Decision Making and Branching  in CDecision Making and Branching  in C
Decision Making and Branching in C
 
Computer programming 2 Lesson 9
Computer programming 2  Lesson 9Computer programming 2  Lesson 9
Computer programming 2 Lesson 9
 
Computer programming 2 - Lesson 7
Computer programming 2 - Lesson 7Computer programming 2 - Lesson 7
Computer programming 2 - Lesson 7
 
Control Statement programming
Control Statement programmingControl Statement programming
Control Statement programming
 
C statements
C statementsC statements
C statements
 
2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt
 
C++ STATEMENTS
C++ STATEMENTS C++ STATEMENTS
C++ STATEMENTS
 
Control statements
Control statementsControl statements
Control statements
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
 
Constructs (Programming Methodology)
Constructs (Programming Methodology)Constructs (Programming Methodology)
Constructs (Programming Methodology)
 
C statements.ppt presentation in c language
C statements.ppt presentation in c languageC statements.ppt presentation in c language
C statements.ppt presentation in c language
 
Java Decision Control
Java Decision ControlJava Decision Control
Java Decision Control
 
Decision Making Statement in C ppt
Decision Making Statement in C pptDecision Making Statement in C ppt
Decision Making Statement in C ppt
 

Kürzlich hochgeladen

%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
chiefasafspells
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 

Kürzlich hochgeladen (20)

Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 

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
  • 19.
  • 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.