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 possibles 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.

First, the integer expression specified in the switch statement is evaluated. This value is then matched one by one with the constant values given in the different cases. If a match is found, then all the statements specified in that case are executed along with the all the cases present after that case including the default statement. No two cases can have similar values. If the matched case contains a break statement, then all the cases present after that will be skipped, and the control comes out of the switch. Otherwise, all the cases following the matched case executed.

When a case value matches with the switch value, the execution starts from that particular case. This execution flow continues with next case statements also. To avoid this, we use "break" statement at the end of each case. That means the break statement is used to terminate the switch statement. However it is optional.

Writing a basic program using switch case statement

#include <stdio.h>
void main()
{
int n ;
printf("Enter any digit: ") ;
scanf("%d", &n) ;
switch( n )
{
case 0:
printf("ZERO") ;
break ;
case 1:
printf("ONE") ;
break ;
case 2:
printf("TWO") ;
break ;
case 3:
printf("THREE") ;
break ;
case 4:
printf("FOUR") ;
break ;
case 5:
printf("FIVE") ;
break ;
case 6:
printf("SIX") ;
break ;
case 7:
printf("SEVEN") ;
break ;
case 8:
printf("EIGHT") ;
break ;
case 9:
printf("NINE") ;
break ;
default:
printf("Not a Digit") ;
}
}
While Do while
It checks the condition first and then executes statements. This loop will execute the statements at least once, then the condition is checked.
While loop allows initialization of counter variables before starting the body of a loop. Do while loop allows initialization of counter variables before and after starting the bodyt of a loop.
It is an entry controlled loop. It is an exit controlled loop
We do not need to add a semicolon (;) at the end of a while condition. We need to add a semicolon(;) at the end of the while condition.
In case of a single statement, we do need to add brackets. Brackets are always needed.
In this loop, the condition is mentioned at the starting of the loop. The loop conditon is specified after the block is executed.
Statements can be executed zero times if the condition is false. Statemnent is executed at least once.
Generally while loop is written as
while (condition) {
statements; //loop body
}
Generally do while loop is written as:
do {
statements; //loop body
}
Parameters Break Statement Continue Statement
Construct a loop This statement allows a user to exit a loop's overall structure. It does not allow a user to exit an overall loop structure.
Statements that switch and loop The break statement can be used in conjunction with the switch statement. It can also be used inside the while loop, so-while loop, and for loop. It means that both the loop and the switch can easily break. The switch statement and the continue statement are incompatible. You can still use it in for loops, do-while loops,and while loops. This means that continue can only happen in a loop, not in a switch.
Control When the control reaches the break statement in a loop construct, it exists immediately. The control passes form the beginning of a loop statement to the continue statement as soon as it encounters it.
Function The break statement causes a loop or a switch to stop running in the middle of a case's execution. It means that if a switch or a loop encounters a break, it will end abruptly. The continue statement does not end the loop, but rather leads it to the next iteration. It means that if a loop encounters a continue statement, it will complete all of its iterations. The continue statement is used to skip the statements that follow the continue in a loop.
Syntax It can be written as:
break;
It can be written as
continue;