Switch Case Statement in C
Syntax
switch(Variable or Value) { case Value1: Statement Block For Value1; break; case Value2: Statement Block For Value2; break; ............ ............ case ValueN: Statement Block For ValueN; break; default: Default Statement Block For No Match; } switch...case
statement is useful for providing list of options to the user from which user can
select any one option.
In
switch...case statement value or value of the variable is compared with each case
value that is specified inside switch statement. If any case value match with the
value provided in switch statement the Statement block associated with that case
is executed. Example
switch (ch) { case 1: printf("a + b = %d", a + b); break; case 2: printf("a - b = %d", a-b); break; case 3: printf("a * b = %d ",a*b); break; case 4: printf("a / b = %d", a/b); break; default: printf("Wrong Choice"); } |