Switch statement in C Language with Example
Автор: The Global Computer Society Nainital
Загружено: 2023-03-23
Просмотров: 34
What is Switch statement in C Language? Explain with Example.
A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case.
The switch statement in C is an alternate to if-else-if ladder statement which allows us to execute multiple operations for the different possible values of a single variable called switch variable. Here, We can define various statements in the multiple cases for the different values of a single variable.
What is switch and break in C?
The switch expression is evaluated once. The value of the expression is compared with the values of each case. If there is a match, the associated block of code is executed. The break statement breaks out of the switch block and stops the execution.
Syntax:
switch(expression){
case constant 1:
perform this;
case constant 2:
perform this;
case constant 3:
perform this;
case constant 4:
perform this;
.
.
.
default:
perform this;
}
Example:
int a = 2;
switch(a){
case 1:
printf(“1\n”);
break;
case 2:
printf(“2\n”);
break;
default:
printf(“No match”);
}
#SwitchStatement #CLanguage #ProgrammingTutorial #CodingTips #BeginnerFriendly
Доступные форматы для скачивания:
Скачать видео mp4
-
Информация по загрузке: