LATEST

How to use switch...case statement in C / C++

In this tutorial, we will learn about "How to use switch...case statement in C / C++" and its working with the help of some examples. This is one of the basic knowledge in C / C++ programming language, so please learn it carefully.

The switch...case statement allows us to execute a block of code among many alternatives.

For example: Suppose we have a program to create a simple calculator with simple math operations. Here, we have options to user select.

See the syntax and flowchart of the switch...case statement below.

Syntax and Flowchart of the switch...case statement in C / C++

The switch...case statement is similar to the if...else...if ladder statement that we learned in the previous tutorial. The switch...case statement allows us to execute a block of code among many alternatives.

The program will execute the commands from top to bottom. If it satisfies any of the conditions then the execution of the code block under that condition will proceed.

Syntax of the switch...case statement in C / C++:

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
}

Here,

  • expression: This is the value passed in to compare with constant1 and constant2.
  • constant1, constant2: This is the value to compare with the passed expression value.
  • break: This is the statement to exit switch...case.
  • default: If no case is executed, the code block inside default will be executed.

How does the switch...case statement work?

When expression value is passed to switch...case. It will compare with the constants of the cases. If the condition of any case is satisfied, then the code blocks of that case are executed.

If no case is executed. The code block inside default will be executed.

Flowchart of the switch...case in C / C++:

case 02 PNG

Some notes with switch...case statement in C / C++

When using the switch...case statement, we need to keep a few things in mind as follows.

  1. The constant value must be of the same data type as the expression value.
  2. We should use break statement in switch...case. Without it, all cases in switch...case are executed.
  3. We should use default in switch...case. If none of these occur, the code block inside default will be executed.
  4. Do not use two cases with the same constant value.
  5. We can do the same thing with the if...else..if ladder. However, the syntax of the switch...case statement is cleaner and much easier to read and write.

Switch...case statement in C / C++ with examples

Here, we have two basic examples of switch...case in C / C++. Each example is written in two different languages C and C++.

Example 1: Create menus with switch...case statement in C / C++.

C++ programming:

#include <iostream>
using namespace std;
int main() {
  int number;
  cout<<"-------------Menu-------------\n\n";
  cout<<"\t1. Option 1.\n";
  cout<<"\t2. Option 2.\n";
  cout<<"\t3. Option 3.\n";
  cout<<"\t4. Option 4.\n";
  cout<<"\t5. Option 5.\n";
  cout<<"\t6. Option 6.\n";
  cout<<"\t-> Enter 0 to exit.\n\n";
  cout<<"Enter your choose: ";
  cin>>number;
  switch(number){
    case 1:
      cout<<"You have selected option 1.";
      break;
    case 2:
      cout<<"You have selected option 2.";
      break;
    case 3:
      cout<<"You have selected option 3.";
      break;
    case 4:
      cout<<"You have selected option 4.";
      break;
    case 5:
      cout<<"You have selected option 5.";
      break;
    case 6:
      cout<<"You have selected option 6.";
      break;
    case 0:
      cout<<"Goood byeeee !!!";
      break;
    default:
      cout<<"Your selection is not valid..";
  }
  
  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>
#include <stdbool.h>
int main(void) {
  int number;
  printf("-------------Menu-------------\n\n");
  printf("\t1. Option 1.\n");
  printf("\t2. Option 2.\n");
  printf("\t3. Option 3.\n");
  printf("\t4. Option 4.\n");
  printf("\t5. Option 5.\n");
  printf("\t6. Option 6.\n");
  printf("\t-> Enter 0 to exit.\n\n");
  printf("Enter your choose: ");
  scanf("%d", &number);
  switch(number){
    case 1:
      printf("You have selected option 1.");
      break;
    case 2:
      printf("You have selected option 2.");
      break;
    case 3:
      printf("You have selected option 3.");
      break;
    case 4:
      printf("You have selected option 4.");
      break;
    case 5:
      printf("You have selected option 5.");
      break;
    case 6:
      printf("You have selected option 6.");
      break;
    case 0:
      printf("Goood byeeee !!!");
      break;
    default:
      printf("Your selection is not valid..");
  }
  
  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

case 03 PNG

Example 2: Create a Calculator using the switch...case statement in C / C++.

C++ programming:

#include <iostream>
using namespace std;

int main() {
  char oper;
    float num1, num2;
    cout << "Enter an operator (+, -, *, /): ";
    cin >> oper;
    cout << "Enter two numbers: " << endl;
    cin >> num1 >> num2;

    switch (oper) {
        case '+':
            cout << num1 << " + " << num2 << " = " << num1 + num2;
            break;
        case '-':
            cout << num1 << " - " << num2 << " = " << num1 - num2;
            break;
        case '*':
            cout << num1 << " * " << num2 << " = " << num1 * num2;
            break;
        case '/':
            cout << num1 << " / " << num2 << " = " << num1 / num2;
            break;
        default:
            // operator is doesn't match any case constant (+, -, *, /)
            cout << "Error! The operator is not correct";
            break;
    }

  
  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>
#include <stdbool.h>

int main(void) {
  char oper;
    float num1, num2;
    printf("Enter an operator (+, -, *, /): ");
    scanf("%c", &oper);
    printf("Enter two numbers: \n");
    scanf("%f%f",&num1,&num2);

    switch (oper) {
        case '+':
            printf("%f + %f = %f",num1,num2,num1+num2);
            break;
        case '-':
            printf("%f - %f = %f",num1,num2,num1-num2);
            break;
        case '*':
            printf("%f * %f = %f",num1,num2,num1*num2);
            break;
        case '/':
            printf("%f / %f = %f",num1,num2,num1/num2);
            break;
        default:
            // operator is doesn't match any case constant (+, -, *, /)
            printf("Error! The operator is not correct");
            break;
    }
  
  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

case 04 PNG

We have many more switch...case statement examples. You can access it here.

Cùng chuyên mục:

How to use multidimensional Arrays in C / C++

How to use multidimensional Arrays in C / C++

Searching and sorting arrays in C / C++

Searching and sorting arrays in C / C++

How to use arrays in C / C++

How to use arrays in C / C++

How to use goto statement in C / C++

How to use goto statement in C / C++

How to use continue statement in C / C++

How to use continue statement in C / C++

How to use break statement in C / C++

How to use break statement in C / C++

How to use if - else statement in C / C++

How to use if - else statement in C / C++

How to use Binary Search Tree (BST) in C / C++

How to use Binary Search Tree (BST) in C / C++

How to use do while loop in C / C++

How to use do while loop in C / C++

How to use while loop in C / C++

How to use while loop in C / C++

How to use for loop in C / C++

How to use for loop in C / C++

Top