position: fixed; top: auto !important; margin-left: 112px;

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.

Syntax of switch-case Statements :-

switch (expression) {
case expression :
      do this ;
      break;
 case expression : 
     do this ; break;
 case expression :
     do this ;
     break;
default :
      this ;
}

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

What happens during the execution of switch statements ?

Firstly , expresssion under switch keyword inside paranthesis 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.

But why break statement ?

Once matched case is found why to execute rest cases. It is better to write break inside each case so that once needed case is found, program execution goes outside of switch brackets.

Example (Program without break statement)

/* Copyright @codzify 2017 */
#import <stdio.h>
#import <conio.h>

void main( ) {
int i = 2 ;
switch ( i ) {
case 1 :
   printf ( "I am in case 1 \n" ) ;
case 2 :
    printf ( "I am in case 2 \n" ) ;
case 3 :
    printf ( "I am in case 3 \n" ) ;
default :
   printf ( "I am in default \n" ) ;
} 
}

Output

I am in case 2
I am in case 3
I am in default

As you see , 'i' gets assigned with value 2. Then expression under switch statement is being evaluated. Then, switch will search for case 2 and executes printf statement then it will execute next case (case 3) also because we did not provided break statement in case 2. Then in case 3 it will executes "I am in case 3" statement and as there is also no break statement default case will gets executed.

Example (Program with break statement)

/* Copyright @codzify 2017 */
#import <stdio.h>
#import <conio.h>

void main( ) {
int i = 2 ;
switch ( i ) {
case 1 :
   printf ( "I am in case 1 \n" ) ;
   break;
case 2 :
    printf ( "I am in case 2 \n" ) ;
    break;
case 3 :
    printf ( "I am in case 3 \n" ) ;
    break;
default :
   printf ( "I am in default \n" ) ;
   break;
} 
}

Output

I am in case 2

Switch Statements in case of character variables

/* Copyright @codzify 2017 */
#import <stdio.h>
#import <conio.h>

void main( ) {
char ch ;
printf ("Enter any of the alphabet a, b, or c ") ; 
scanf ("%c", &ch ) ;

switch ( ch ) 
{
case 'a' :
case 'A' :
   printf ( "print a or A" ) ;
   break ; 

case 'b' :
case 'B' :
    printf ( "b or B" ) ; 
    break ;
case 'c' : 
case 'C' :
    printf ( "c or C" ) ;
    break ; 

default :
    printf ( "Print Defualt Statements" ) ; }
}

Here we will take input from user . Suppose user entered b Then b or B statements gets printed

Input :-

Enter any of the alphabet a, b, or c
 b

Output :-

b or B

Hope you got better understanding of the switch statement in C. In next tutorial, you will learn about pointers in C language. Happy Learning !



Previous Next Article







codzify.com


Largest collection of up-to-date tutorials to learn programming languages. We are focused on easy learning. Massive collection of interview questions one may need for preparation.

Social Profile


Linkedin
Twitter
Facebook

Copyright 2018. All rights reserved.