Switch Statement in C

 1. Program  to implement

i.  Addition
ii. Subtraction
iii. Multiplication

*source code*

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int x,y,z,choice;
while(1)
{
printf("Enter the number or data\n");
scanf("%d %d",&x,&y);
printf("title\n 1 Addition\n 2 Subtraction\n 3 Multiplication\n 4Exit\n");
printf("Enter your choice among which calculation you want to perform\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
z=x+y;
printf("sum is %d\n",z);
break;

case 2:
z=x-y;
printf("subtraction is %d\n",z);
break;

case 3:
z=x*y;
printf("Multiplication is %d\n",z);
break;

case 4: exit(0);
default:printf("Enter valid number\n");
}
}
getch();
}

Output:



































2. Program using break statement
 Break statement:When it is encountered in loop,the loop terminates immediately and program control passes to another statement.


#include<stdio.h>
#include<conio.h>
void main()
{
int i;
for(i=1;i<=8;i++)
{
printf("%d\t",i);
if(i==5)
{
break;   //// Here break statement is used in loop of if(i==5) and the output result out as
}
}
getch();
}
Output:

3.Program using continue statement

Continue statement: When it is encountered to the loop,the statement is skipped to the following loop and passes to update expression after loop.

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
for(i=1;i<=8;i++)
{
if(i==5)
{
continue;
}
printf("%d\t",i);
}
getch();
}

Output:





Here as said in definition, the continue statement is used in loop if(i==5),and the  statement skipped and passes to other  ie 5 is skipped and passes to update expression and results out following output.

Comments