Understanding the switch-case Statement in C
											
											
										
										
											 
												 switch case statement: 
												Switch-case statements in C make multiway branching a powerful tool and are sometimes referred to as selection control statements. An expression or variable is tested for equality against a list of values which are predefined in switch-case. When a match is found, the code associated with that specific case is executed.
 
											Its working is somewhat parallel to an if-else if ladder, but it is only designed for situations where one expression is being evaluated against many possible constant values. In C, a switch statement can take as many case options as possible. The switch statement evaluates an expression and matches the result with the values laid down in its case labels. If the value of the expression matches a case value, the code associated with that case is executed.
											So, let's now check the syntax of switch-case for clarity.
																							
										
										
										
										
										
											 
									
											
										
										
											
											
										
											
											
									
										
												  Syntax : switch case statement
												
												/* syntax for switch case statement in C Programming */
													switch (expression)
														{
														    case constant1:
														         /*statements for case constant1:*/
														         break;
														    case constant2:
														         /* statements for case constant2 */
														         break;
														    .
														    .
														    .
														     case constant-n:
														         /* statements for case constant-n:*/
														         break;
														    default:
														         /*  statements for default case*/
														}
													 
														 Explanation:
															1. For a switch statement, it first evaluates the expression provided to switch(expression).
					2. If it achieves value of a case (like case constant1), then the statements inside that case block will execute and keep executing until it encounters a break statement. The duty of the break statement is to stop executing the switch statement immediately and shall transfer the control to the code that comes right after the switch block.
					3. If no match is found after checking all the case conditions, then the default case, if you specify one, will execute. The moment it exits, the control exits automatically from the switch statement.
					
Important Notes:
					i. Fall-Through: If you do not use a break statement to terminate the execution of a matching case block, then the program will be allowed to "fall through" all the succeeding case blocks for execution of statements irrespective of whether their temporary values match, until it encounters a break or until it reaches the very end of the switch block. This is a major source of errors if not intended!
					ii. default is Optional: A default clause need not be necessarily written in a switch statement; it is left to the program's discretion. Once the matching case is over, if there are no matches, and if default has not been declared in the switch statement, then it simply comes out of switch without executing any switch codes internally, and the program flow continues.
												
																			
													
											
										
								
							
								
									
									
								
									 This Diagram is about the flowchart for a switch-case statement. And now let's study how the switch-case statement works in a stepwise manner by just following the logic in the figure.
						In the very beginning, the exp (expression) that has been mentioned in the switch-case is evaluated. This expression will be evaluated just once before comparing its resultant value against the constant values of all the case labels (like case 1, case 2, and so forth or with default label).
						If a match is found (for example if the expression matches case constant1), such statements will become executed inside the respective case block. Execution will continue until a break statement is traversed. The activity of a break statement is to stop immediately the execution of the switch block and transfer the control to the code directly after the switch block.
						In case no match is found within all these case options, the default case (if you have included it) will execute. At the end of the default case, the control automatically exits the switch statement.
								
									
										
									
										
											
											Example 1: C program using switch case  to create a simple calculator.
											
											
												// Program to create a simple Choice base calculator in C Language using switch case
							#include <stdio.h>
							int main()
 {
							    char operation;
							    double a, b;
							    printf("\n Addition: +");
							    printf("\n Substraction: -");
							    printf("\n Multiplication: *");
							    printf("\n Division: /");
							   
							    printf("Enter Your choice or an operator (+, -, *, /): ");
							    scanf("%c", &operation);
							    printf("Enter two Values: ");
							    scanf("%lf %lf",&a, &b);
							    switch(operation)
							    {
							         case '+':
							                printf("\nAddition=>%.1lf + %.1lf = %.1lf",a, b, (a+b));
							                break;
							          case '-':
							                printf("\nSubstaction=>%.1lf - %.1lf = %.1lf",a, b, (a-b));
							                break;
							          case '*':
							                printf("\n Multiplication=>%.1lf * %.1lf = %.1lf",a, b, (a*b));
							                break;
							          case '/':
							                printf("\nDivision=>%.1lf / %.1lf = %.1lf",a, b, (a/b));
							                break;
							             
							          /* operator doesn't match any case constant +, -, *, /        */
							          default:
							                printf("Error! Wrong choice or operator try again....");
							    }
							    return 0;
							}
								
											 
											  Output:
										     Addition: +
										    Substraction: -
										    Multiplication: *
										    Division: /
										    
											Enter Your choice or an operator  (+, -, *, /): 
											-
											Enter two Values: 52.5
											12.3
											Substaction=>52.5 - 12.3 = 40.2
															
											
										 
										
									
								
												
													
												
												
										
										 
									
									
										
									
											
									Other Topics 
SQL Interview Questions  Java Interview Questions Top SQL Interview Questions Python Interview Questions Banking Case Study in SQL