C Switch case statement


It is the simplest form of if else if statement in which many if else if conditions takes place. When we want to write a program in which there is need to include more if else if conditions then it becomes more confusion and  tough to understand. So we can use switch case statement that makes to write a program easy and simple to understand.

Syntax of switch case statement:-

switch(variable or condition)
{
case value 1;
statement block 1;
break;
case value 2;
statement block 2;
break;
case value 3;
….……………………..
….……………………..
case value n;
statement block n;
default:
statement block x;
break;
}
statement y;


In case of switch case statement when value written in switch match with case value then statement of  that block will be executed. When case value be character then will be written in single inverted comma like case ‘a’: and when integer then will be written simple like case 1: .There is default keyword written in syntax. Its  use when value in  switch not matched with case value then statement written under default keyword gets executed.And there is a keyword break also written. As we know that statements become executed one by one in a c programs so there are used break keyword . If there are not break keyword used then all the cases will be executed after that case  in which case value matched with value of switch. Break keyword tell the compiler to jump out of case statement to execute following statement after cases statements.One things keep in mind when unexpected value given by the user then value of switch does not match with case value in this case  statement under the defaults keyword gets executed.

Let us see example:-

Program to c merge the both operation addition and summation with help of switch case  statement;
#include<stdio.h>
int main()
{
int a,b,c,sum,multi;
printf("select your operation:\n ");
printf("1.sum of two numbers:\n");
printf("2.multiplication of two numbers:\n");
scanf("%d",&c);
switch(c)
{
case 1:
printf("Enter any two numbers:\n");
scanf("%d%d",&a,&b);
sum=a+b;
printf("%d",sum);
break;
case 2:
printf("Enter any two numbers:");
scanf("%d%d",&a,&b);
multi=a*b;
printf("%d",multi);
break;
default:
printf("choose again your correct operation!");
}
return 0;
}
Output :
select your operation
1. sum of two numbers:
2. multiplication of two numbers:
 2
 Enter any two numbers: 5
 7

Iterative(looping) statements

Iterative statement is used to execute  the particular statements until the specified condition is true. When condition is false then programs goes to end.These  are three types of looping statement.
1.  while loop
2. do while loop
3. for loop

while loop

Syntax of while loop:-

while(condition)

{
statement 1;
statement 2;….……………..
Statement n;
}
Statement x;

In the while loop condition is check before execution of any statement in while body.When condition is checked then statement in the while body executed and value gets updated. Updated value check in the condition  and statement get executed again.These process of execution will continue until the updated value  becomes false in condition.when condition is false control goes out of while body and  jump to the statement  x. Keep in mind what happened when there is no value get updated.In this case value not updated so condition always will be true and statement in while body executed infinite times.Let us understand with program example -

Program to c print the first 10 natural numbers.

#include<stdio.h>
int main()
{
int i=1;
while(i<=10)
{
printf("%d",i);
i++;
}
return 0;
Output:-
12345678910
Run this code

In given example value started to check from 1 to 10.Value 1 checked in condition is true so statements of while body executed and further value get updated from 1 to 2 .In this way statements in while body executed to print numbers up to 10 but when value updated 11 checked in condition is false control jump out of while body. After that statement return 0 executed.

On the basis of above rules and conditions try to give your answer  to these questions -
1.) Merge the area of circle and rectangle with help of switch case.
2.) write a program to print vowel with help of switch case.
3.) write a program to sum of first 10 natural numbers.

<<Prev                                Next>>