Table of Contents
Introduction
Break statement in C means terminating the loop at any stage in its ongoing process or jumping from one step to another. We will learn Break statement in C in this article with examples.
Hopefully, you all are aware of loops and the nesting of the loops that we have discussed in our previous chapters.
Switch, Break and continue in C:
When we want to choose one case among many choices, if-else is used. But using the same when we have several choices is not a good practice. It will give us the desired output without a doubt, but it will make the code complex and lengthy.
Switch in C
Using ‘switch…case’ can solve the problem discussed above that makes the code a little easier and less complex.
This part of programming is beneficial when we are given many choices, and the aim is to perform different tasks for each choice. Other than if-else statements can be executed and controlled by switch…case.
Syntax: switch (expression)
{
case constant1:
statement(s);
break;
case constant2:
statement(s);
break;
………….. // any number of cases can be
given.
default:
statement(s);
}
‘switch’, ‘case’, ‘break’, ‘default’ are the keywords and will be used as it is in the program. The ‘expression’ that we write enclosed in ‘( )’ brackets followed by switch will be checked. Suppose the value of the expression matches the value of any constant of the cases written in the code. In that case, the statement(s) corresponding to those cases will be executed. However, if there is no match, the default case will be evaluated, and the statement(s) corresponding to that case is executed.
Example
For example, a teacher asks for students’ grades as input and wants to give remarks as an output. See the code to get a better understanding of the concept.
#include<stdio.h> int main() { char grade; printf("Enter your grade\n"); scanf("%c",&grade); switch(grade) { case 'A': printf("Excellent\n"); break; case 'B': printf("Outstanding\n"); break; case 'C': printf("Good\n"); break; case 'D': printf("Can do better\n"); break; case 'E': printf("Pass\n"); break; case 'F': printf("Fail\n"); break; default: printf("invalid input\n"); } return 0; }
Output
Explanation
The teacher asks for the student’s grade and stores that input in the variable ‘grade’.
The variable ‘grade’ is passed in ‘( )’ braces as an expression to check whether the value of ‘grade’ matches any of the cases written in the body of the switch. Since the grade entered by the user, i.e. ‘C’ matches with case ‘C’:, the statement corresponding to this case is printed on the output screen as shown.
Break in C
break; is terminating the further execution of the code without checking the rest of the cases. As stated already, ‘break’ is used to terminate the loop whenever we want and can also be used with ‘switch’. That is the only reason for writing ‘break’ after every case to avoid the extra outputs.
Example
If we don’t use ‘break’ after every case, the cases written after that will also get executed after the execution of the correct case.
#include<stdio.h> int main() { char grade; printf("Enter your grade\n"); scanf("%c",&grade); switch(grade) { case 'A': printf("Excellent\n"); case 'B': printf("Outstanding\n"); case 'C': printf("Good\n"); case 'D': printf("Can do better\n"); case 'E': printf("Pass\n"); case 'F': printf("Fail\n"); default: printf("invalid input\n"); } return 0; }
Output
Explanation
When the student enters their grade value ‘C’, the remarks (or statement) corresponding to that grade gets printed. But all the statements are written, you after ‘case ‘C’:’ also executed because there is no ‘break’ statement used after any case to terminate the loop.
So, it is better to use the ‘break’ statement to execute only that particular case whose constant value matches with the value of expression of the switch statement.
Note: character values are always written in ‘’.
Let’s make a program having expression value as an integer.
#include<stdio.h> int main() { int amount; printf("Enter the amount of goods purchased in multiples of 1000\n"); scanf("%d",&amount); switch(amount) { case 1000: printf("You get 10 percent discount\n"); break; case 2000: printf("You get 15 percent discount\n"); break; case 3000: printf("You get 20 percent discount\n"); break; case 4000: printf("You get 25 percent discount\n"); break; case 5000: printf("You get 30 percent discount\n"); break; default: printf("invalid amount entered\n"); } return 0; }
Output:
Loops containing break statement in C
We can also terminate the ongoing process of a loop by using the ‘break’ statement. Just write a break; after the statement after which you want to stop further execution of the loop.
Example
#include<stdio.h> int main() { int m; for(m=1;m<=6;m++) { printf("Hello to user %d\n",m); if(m==3) { break; } } return 0; }
Output
Explanation
The program runs fine with the first and the second iteration without going into the inner ‘if’ block. After that, ‘m’ is updated to 3. Since the condition (m<=6) is true, the statements inside the ‘for’ loop are executed. ‘Hello to user 3’ is printed, and the control moves to the inner ‘if’ statement, i.e. if(m==3), which is true. Thus, the execution of the body of the ‘if’ block takes place. This terminates the loop (since the break statement is used), and control comes out of the ‘for’ loop, thus ending the output.
Continue in C
The continue statement is used to pass the control to the conditional test. The conditional test is where the condition is checked, skipping the rest of the loop written after the continue statement for that particular iteration.
Example
Let us first write a program containing ‘continue’.
#include<stdio.h> int main() { int m; for(m=1;m<=6;m++) { printf("whats up programmers\n"); if(m==3) { continue; } printf("m is not 3\n"); } return 0; }
Output
Explanation
In the program written above, what happens is:
The statement printf(“what’s up programmers\n”); is printed on the screen for the first iteration. Since (m==3) is not true, the control moves to the next statement, and printf(“m is not 3\n”); is executed. The same process takes place in the second iteration. When ‘m’ is updated to 3, the first printf() statement is executed, and then the ‘if’ condition is verified, which results in true. Thus, the control enters the ‘if’ block.
The continue; statement allows the control to pass to the conditional part of the ‘for’ loop by skipping the rest of the statements for the 3rd iteration. Thus, ‘m is not 3’ is not printed in the 3rd iteration.
After that, every iteration works properly, and the code runs until the ‘m<=6’ condition becomes false.
0 Comments