Switch Statements in C++ Programming

2 min 2 sec read Basic


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  :
      statement(s)
      break;     /* optional */
   case expression2  :
      statement(s)
      break;     /* optional */
  
   default : /* Optional */
      statement(s);
}

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.

Example

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

Output

I am in case 1

How was the tutorial? Nice. Right?



Deep Concept videos to crack the highly-piad interviews.

Chekout out our Youtube Channel to get detailed video content on important topics in interviews.



What is your Interview Score?

Test your skillset with the curated questions created by experts around the globe.

Book a free test slot. Now !.

It usually takes 30 mins for an online test and this test will be MCQ based. Get detailed analytics based on your test and recommedations with personalised roadmaps.

Book a test slot. Now!

Have questions about the trial coding class?

Chat with our experts to discuss

Connect on WhatsApp


Recommended tutorials

#online compiler for c     #python for programming    

#dfs and bfs algorithm     #programming with c language

#storageclass in C    #listcomprehension in python