Hello Guys,
Switch statements are alternative for if
statements. It is usually used in specific situations.
The conditional operator and the if statements make it easy
to write a program that chooses between two alternatives.
However many times a program needs to choose one of
several alternatives.
You can
do this by using else if; else if ... else.
It is tedious and prone to error, but switch statements
are cleaner and efficient.
When the value of the variable is successively compared
against different values, use the switch statements, it is more
convenient and efficient.
Syntax:
switch (expression)
{
case value1:
statement1;
...
break;
case value2:
statement2;
...
break;
.
.
.
default:
statement;
...
break;
}
The expression enclosed within the
parameters is successively compared against the values: value1,
value2, ..., valueN.
The case must be a simple constant or constant
expression.
If the case is found whose value is equal to the value of
the expression then the statements that follow the case are
executed.
When more than one statement is included, they do not have
to be enclosed within the braces. (as shown in the syntax above)
The break statement signals the end of a
particular case and causes execution of the switch statement to be
terminated.
We must include the switch statement after every
case.
Forgetting to do so for a particular case causes
the program execution into the next case.
The spacial optional case called default is executed if the
value of expression does not match any of the case values. It is same as "fall through" else.
Example:
#include<stdio.h>
enum weekDay {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday}
enum weekDay today = Monday;
int maint()
{
switch (today)
{
case Sunday:
printf("Today is Sunday");
break;
case Monday:
printf("Today is Monday");
break;
case Tuesday:
printf("Today is Tuesday");
break;
default:
printf("whatever");
break;
}
return 0;
}
You can check that out by removing the break statement. and then by changing the value of today.
Output:
Today is Monday
Click here if you are unsure about enums
Another Example:
// Program to use the switch statement for calculation of two numbers...
#include<stdio.h>
int main(void)
{
float value1, value2;
char operator;
printf("Type in your expression.\n");
scanf("%f %c %f", &value1, &operator &value2);
switch(operator)
{
case '+':
pritnf("%.2f\n", value1 + value2);
break;
case '-':
pritnf("%.2f\n", value1 - value2);
break;
case '*':
pritnf("%.2f\n", value1 * value2);
break;
case '/':
if (value2 == 0)
printf("Division not possible \n");
else:
pritnf("%.2f\n", value1 / value2);
break;
default:
print("UNKNOWNOPERATORError"")
break;
}
return 0;
}
Comment down the output...
You can also get the video to understand the topic on my YouTube channel:
eat_sleep_ code_repeat (Subscribe)
Thanks, allow me to complete.
Comments what you feel about the article and suggest me the topic.
This is Harsh Jaiswal Signing off.
Take Care.
Subscribe by Email
Follow Updates Articles from This Blog via Email
No Comments