LATEST

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?

The 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:

  1. Case1 has a break statement and case2 doesn't.
  2. Case2 has a break statement and case1 doesn't.
  3. Both case1 and case2 have a break statement.
  4. Neither case1 nor case2 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 inside case1 is executed and exits switch...case.
  • If condition case1 is false and condition case2 is true. Then the block of code inside case2 is executed and case1 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:

faq 01 PNG

Case2 has a break statement and case1 doesn't:

  • If the condition of case1 is true. Then, the code block inside case1 is executed and continues to consider the condition of case2.
  • If condition case1 is false and condition case2 is true. Then the block of code inside case2 is executed and case1 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:

faq 02 PNG

Both case1 and case2 have a break statement:

  • If condition case1 is true. Then the code block inside case1 is executed and exits switch...case.
  • If condition case1 is false and condition case2 is true. Then the block of code inside case2 is executed and case1 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:

faq 01 1  PNG

Neither case1 nor case2 have a break statement:

  • If the condition of case1 is true. Then, the code block inside case1 is executed and continues to consider the condition of case2.
  • If condition case1 is false and condition case2 is true. Then the block of code inside case2 is executed and case1 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:

faq 02 PNG

Should we use break statement in switch...case?

When working with 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 sees break 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:

faq 04 PNG

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:

faq 03 PNG

Cùng chuyên mục:

Why do we use arrays instead of other data structures?

Why do we use arrays instead of other data structures?

Which is better while or do while loop?

Which is better while or do while loop?

How to stop while loop in C / C++?

How to stop while loop in C / C++?

Why for loop in C / C++ not stopping?

Why for loop in C / C++ not stopping?

Top