---Advertisement---
Learn C

Learn C Language : Switch Case Statement (Case Control Structure)

By Smart Answer

Updated on:

---Advertisement---

Switch Case Statement

The control statement which 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 statement:

switch(expression)
{
case constant 1:
do this;
case constant 2:
do this;
case constant 3:
do this;
default:
do this;
}

 

The integer expression following the keyword switch is any C expression that will yield an integer value.It could be an integer constant like 1, 2 or 3, or an expression that evaluates to an integer.The keyword case is followed by an integer or a character constant.Each constant in each case must be different from all the others.The ‘do this’ lines in the above form of switch represent any valid C statement.

A simple program which demonstrates the use of continue statement:

main()
{
int i = 2;
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”);
}
}

Output :
I am in case 2
I am in case 3
I am in default

Switch statement using break:

main()
{
int i = 2;
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”);
}
}

Output :
I am in case 2

 

Smart Answer

---Advertisement---

Related Post

Learn C Language : String

‹ previous What Is String The way a group of integer can be stored in an integer array, similarly a group of character can be stored in a ...

Learn C Language : Array

‹ previous Next › What are Arrays For understanding the arrays properly, consider the following program: main(){     int x;      x = 5;      x ...

Learn C Language : Data Type

‹ previous Next › Integer, long and short Sometimes, we know in advance that the value stored in a given integer variable will always be positive.When it is ...

Learn C Language : Functions

‹ previous Next › What is a Function A function is a self-contained block of statements that perform a coherent task of some kind.Every c program can be ...

Leave a Comment