We have previously learned about if-else
statements, but
what if there was a more efficient way to handle multiple conditional
arguments?
Consider the following program:
#include <stdio.h>
int main(void)
{
int a = 3;
if (a == 1)
{
// Do something
}
else if (a == 2)
{
// Do something else
}
else if (a == 3)
{
// Do the thing
}
else
{
// Do this as a last resort
}
return 0;
}
This code works fine as it is, but as the number of conditions
increase, cycling through each if statement can become both very tedious
as well as computationally expensive. This is where switch
statements come in.
Consider the following program:
#include <stdio.h>
int main(void)
{
int a = 3;
switch(a)
{
case 1:
// Do something
break;
case 2:
// Do something else
break;
case 3:
// Do the thing
break;
default:
// Do this as a last resort
}
return 0;
}
Functionally, this executes the same functions as the previous example. Let’s break down each component of the statement:
switch(a)
This establishes the switch statement. It takes in a variable a to be compared.
case CONSTANT:
This compares the variable a that was previously taken in to a
constant. Functionally, this is the same as
if (a == CONSTANT)
break;
This breaks the switch statement, ceasing any more comparisons and
taking the user to the next line after the brackets. In the example
above, it would take you to the line return 0;
. This is not
required, and can even be omitted to continue iterating through the
switch statements. Here is an example:
#include <stdio.h>
int main(void)
{
int starRating = 4;
switch (starRating)
{
case 5:
("Your movie is perfect!\n");
printfbreak;
case 4:
case 3:
("Your movie is good!\n");
printfbreak;
case 2:
("Your movie is okay.\n");
printfbreak;
case 1:
case 0:
("Your movie is bad.\n")
printfbreak;
default:
("Invalid rating.\n");
printfbreak;
}
return 0;
}
The output above prints
Your movie is good!
Since the case for 4 is not broken, it executes the statement for the next case below. In this case it executes case 3’s statement and then breaks.
Overall, switch statements provide an efficient way of comparing a variable against multiple constants.