What happens if no break statement in switch...case?
When using switch...case
statement in C / C++. We often wonder, "Should we use break
statement in switch...case
?" or "What happens if no break
statement in switch...case
?".
So, what would switch...case
look like without a break
statement. Does it work as well as we want it to? Let's start learning with us.
What happens if no break statement in switch...case?
break
statement enables program execution to exit the switch
construct. Without it, execution continues evaluating the following case
statements.Make sure you learn and understand the switch...case
statement. Let's review the syntax of the switch...case
statement.
switch (expression) { case constant1: // code to be executed if // expression is equal to constant1; break; case constant2: // code to be executed if // expression is equal to constant2; break; . . . default: // code to be executed if // expression doesn't match any constant }
Suppose, in the switch...case
statement, there are two cases, case1
and case2
. Then we have 4 possibilities as follows:
Case1
has a break statement andcase2
doesn't.Case2
has a break statement andcase1
doesn't.- Both
case1
andcase2
have a break statement. - Neither
case1
norcase2
have a break statement.
Now we will take a closer look at each possibility.
Case1
has a break statement and case2
doesn't:
- If condition
case1
is true. Then the code block insidecase1
is executed and exitsswitch...case
. - If condition
case1
is false and conditioncase2
is true. Then the block of code insidecase2
is executed andcase1
is not.
switch(expression){ case1 constant: //block code 1 break; case2 constant: //block code 2 }
Example:
#include <iostream> using namespace std; int main() { int a; cout<<"Enter a number: "; cin>>a; switch(a){ case 1: cout<<"hello, i'm case 1\n"; a++; break; case 2: cout<<"hello, i'm case 2\n"; } cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
Output:
Case2
has a break statement and case1
doesn't:
- If the condition of
case1
is true. Then, the code block insidecase1
is executed and continues to consider the condition ofcase2
. - If condition
case1
is false and conditioncase2
is true. Then the block of code insidecase2
is executed andcase1
is not.
switch(expression){ case1 constant: //block code 1 case2 constant: //block code 2 break; }
Example:
#include <iostream> using namespace std; int main() { int a; cout<<"Enter a number: "; cin>>a; switch(a){ case 1: cout<<"hello, i'm case 1\n"; a++; case 2: cout<<"hello, i'm case 2\n"; break; } cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
Output:
Both case1 and case2 have a break statement:
- If condition
case1
is true. Then the code block insidecase1
is executed and exitsswitch...case
. - If condition
case1
is false and conditioncase2
is true. Then the block of code insidecase2
is executed andcase1
is not.
switch(expression){ case1 constant: //block code 1 break; case2 constant: //block code 2 break; }
Example:
#include <iostream> using namespace std; int main() { int a; cout<<"Enter a number: "; cin>>a; switch(a){ case 1: cout<<"hello, i'm case 1\n"; a++; break; case 2: cout<<"hello, i'm case 2\n"; break; } cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
Output:
Neither case1
nor case2
have a break statement:
- If the condition of
case1
is true. Then, the code block insidecase1
is executed and continues to consider the condition ofcase2
. - If condition
case1
is false and conditioncase2
is true. Then the block of code insidecase2
is executed andcase1
is not.
switch(expression){ case1 constant: //block code 1 case2 constant: //block code 2 }
Example:
#include <iostream> using namespace std; int main() { int a; cout<<"Enter a number: "; cin>>a; switch(a){ case 1: cout<<"hello, i'm case 1\n"; a++; case 2: cout<<"hello, i'm case 2\n"; } cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
Output:
Should we use break statement in switch...case?
switch...case
, we should use break
statement. This helps us avoid unexpected errors.-
If you don't include
break
statement in any of case then all the case below will be executed and until it seesbreak
statement . -
And if you don't include
break
statement in default then it will cause no effect as there are not any case below this 'Default
' case. -
And not using
break
statement generally considered as a bad practice but some time it may also come handy because of its fall-through nature.
We have an example that checks even or odd numbers. Please see the results when using the break
statement and not using it.
Example: Checks Even or Odd numbers.
Using break
statement:
#include <iostream> using namespace std; int main() { int number, result; cout<<"Enter a number: "; cin>>number; result = number % 2; switch(result){ case 0: cout<<"This is a even number."; break; case 1: cout<<"This is a odd number."; break; } cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
Output:
Do not use the break
statement:
#include <iostream> using namespace std; int main() { int number, result; cout<<"Enter a number: "; cin>>number; result = number % 2; switch(result){ case 0: cout<<"This is a even number."; case 1: cout<<"This is a odd number."; } cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
Output: