Linux C Programming Tutorial Part 16: Switch, Break, and Continue statements

We've already discussed some of the basic loops used in the C programming language, including for, while, and do...while. These loops, as most of you'd agree, basically help you when instructions are to be executed repeatedly under one or more conditions. So you write less code while making it more readable in the process. 

Just like these loops, there are some statements that help you write better code in C. Some of those statements are Switch, Break, Continue, and Goto. Let's discuss them one by one.

Switch statement

Switch is basically a statement that accepts a value, based on which it executes a piece of code. It's a control statement as in it changes the control of execution based on the input value.

Here's the structure of switch:

...
...
...
switch (expression)
{
case const_val_1: //some code
break;
case const_val_2: //some code
break;
case const_val_3: //some code
break;
default: //some code
break;
}
...
...
...

So here, 'expression' is a value that's passed as input to 'switch' statement, and based on the case which matches 'expression' value, the corresponding code is executed. Following is a working example where-in switch makes the code less complex and improves readability.

#include <stdio.h>

int main()
{
int roll;
printf("Enter a roll number in range of 1 - 5: ");

scanf("%d", &roll);

switch(roll)
{
case 1: printf("name of student is RON");
break;
case 2: printf("name of student is ROGER");
break;
case 3: printf("name of student is SAM");
break;
case 4: printf("name of student is LUCIE");
break;
case 5: printf("name of student is DORN");
break;
default: printf("Entered roll number didn't match any candidate");
break;
}
return 0;
}

So here, the roll number that's entered by user in input is fed to the switch statement, and the code corresponding to the case that matches is executed.

Note that the 'break' statement at the end of each case makes sure the switch statement is exited after the matched case is executed. If no 'break' statement is present, the execution flow enters the next case. 

The 'default' case is there to make sure user gets notified if no case matches the value in switch. Having a 'default' case is not mandatory, but it is always useful.

Also, since the default case is usually the last case in the switch statement, having 'break' at the end of it is also not mandatory, but is encouraged as it is considered defensive programming (just in case more cases get added in future after the 'default' case).

Break and Continue

Note that in addition to 'switch', the 'break' statement can also be used to perform an early exit from loops like for, while, and do...while. 

...
...
...
for(i=0; i<10; i++)
{
//statements

if(condition)
break;

//statements
}
...
...
...

Now coming to 'continue', just like 'break' causes an early exit from a loop, 'continue' forces the next iteration of the loop, which in case of while and do...while starts with condition check and in case of a 'for' loop is the increment step. 

Here's an example that uses 'continue' to skip odd numbers inside a for loop:

...
...
...
for(i=0; i<100; i++)
{
if(i%2)
continue;

//statements
}
...
...
...

Note that you can't use 'continue' inside switch, except for the cases when 'switch' itself is part of a loop.

Conclusion

In this tutorial, we discussed break, continue, and switch statements. Do try out the examples or demo code we shared in this tutorial on your system to get a better hang of these concepts. In case of any doubt or query, let us know in comments below.

Share this page:

0 Comment(s)