Topic-11: Case control / switch case()

  • We will multiple conditions but we will execute one condition .
  • It is same as if else ladder but switch case is bit faster than if else ladder .

EXAMPLE:-1

#include<stdio.h>

int main()

{

int num=2;

switch(num){

case 1:

printf(“one\n”);

break;

 

case 2:

printf(“two\n”);

break;

 

case 3:

printf(“three\n”);

break;

}

 

return 0;

 

}

OUTPUT :-

two

EXAMPLE-2

#include<stdio.h>

int main()

{

int ch=’d’;

switch(ch){

case ‘a’:

printf(“apple\n”);

break;

 

case ‘b’:

printf(“ball\n”);

break;

 

case ‘c’:

printf(“cat\n”);

break;

 

case ‘d’:

printf(“don\n”);

break;

}

 

return 0;

 

}

OUTPUT :-

don

EXAMPLE-3

#include<stdio.h>

int main()

{

int num=4;

switch(num){

case 1:

printf(“hello\n”);

break;

 

case 2:

printf(“welcome\n”);

break;

 

case 3:

printf(“by\n”);

break;

 

case 4:

printf(“thank u\n”);

break;

}

 

return 0;

}

OUTPUT:-

thank u

  • If we will remove the “break” then the next statement is also execute .
  • We can use “default” in the middle of the code, but if we will use “default” in the middle of the codes then we must we ‘break’ after default .

Leave a Comment

Your email address will not be published. Required fields are marked *