Test your Programming knowledge

50% users failed to get the best score. It's your turn to test now.
Questions curated by the expert mentors at codzify.com

Start Quiz


Switch Statements in C++ Programming

2 min 2 sec read

What is Switch Statement ?

The control statement that allows us to make a decision from the number of choices is called a switch, or more correctly a switch- case-default, since these three keywords go together to make up the control statement.

Generic Syntax of switch-case Statements :-

switch expression
   case expression1  :
      break;     /* optional */
   case expression2  :
      break;     /* optional */
   default : /* Optional */

switch statements can accepts 'integer constants' or 'Character variables'.

What happens during the execution of switch statements ?

Firstly , expression under switch keyword inside parenthesis is evaluated .Then each case is evaluated one by one unless and until match is found. If no match is found , default case is being executed.


/* Copyright @codzify 2017 */
using namespace std;
int main ()
    int i = 1;
    switch ( i )
        case 1 :
            printf ( "I am in case 1 
" ) ;
        case 2 :
            printf ( "I am in case 2 
" ) ;
        case 3 :
            printf ( "I am in case 3 
" ) ;
        default :
            printf ( "I am in default 
" ) ;
    return 0;


I am in case 1

Recommended Articles

  1. Functions in C++
  2. Pointers in C++
  3. Array in C++

Did you found this article helpful?

Try to execute what you have learnt

Easy to use online data structure compiler where you can execute the programs in your favourite programming language.
(C, C++, Python)

Open Compiler

HTML, CSS and Javascript Real time Web Editor

Execute your HTML, CSS and javascript code in real time with the web editor
(HTML, CSS, Bootstrap, Javascript)

Open Web Editor