khoob study

Tuesday, November 8, 2022

switch case statement in c language

                       switch case statement in c language 

#switch case statement:- Swich case is the alternative of else if statement  of C language . When we have many choices in our program & we want , user to choose one of them we may choose you 'switch case' to write the program for the same . 

The switch case statement :- 

switch (condition)
{
case 1:
-
-
-// set of statement 
-
-
break;
switch (condition)
{
case 2:
-
-
-// set of statement 
-
-
break;
switch (condition)
{
case 3:
-
-
-// set of statement 
-
-
break;
-
-
-
-
-
-
-
-
-
-
switch (condition)
{
case n:
-
-
-// set of statement 
-
-
break;

~~ In switch case the value of expression is pass than the value of expression is matched with each case one by one from starting to end . if value of expression is matched with any of the cases then statement or set of statement of that case will be executed and rest of the case will be bypass . If non of the case is matched then their may be default case wich will be executed . the break keyword is used  in switch case to send the control out side of switch.

EXAMPLE:-

Program:- wap to choose any number by user in c language.

#include<stdio.h>
#include<conio.h>

main()
[
int a;
clrscr();
printf("choose any number from 1 to 4");
scanf("%d",&a );
switch case (a)
{
case 1:
prinf(" u have enter one");
break;
case 2:
prinf(" u have enter two");
break;
case 3:
prinf(" u have enter three");
break;
case 4:
prinf(" u have enter four");
default ;
printf("worng entry try again ");
}
getch();
}

Output :-

choose the number 1
one
two
3
three
4
four
5
worng entry try again
....6,7,8,9,0
... worng enry try again

Read More: https://khoobstudy.blogspot.com/2022/11/program-for-hello-word-good-morning.html

2 comments: