{"id":2014,"date":"2024-05-10T07:06:29","date_gmt":"2024-05-10T07:06:29","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=2014"},"modified":"2024-05-10T07:06:29","modified_gmt":"2024-05-10T07:06:29","slug":"break-statement-in-c","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/break-statement-in-c\/","title":{"rendered":"Break Statement in C"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#break-statement-in-c\">Break Statement in C<\/a><\/li><li ><a href=\"#what-is-break-in-c\">What is Break in C<\/a><\/li><li ><a href=\"#syntax-of-break-in-c\">Syntax of Break in C<\/a><\/li><li ><a href=\"#use-of-break-in-c\">Use of Break in C<\/a><\/li><li ><a href=\"#break-in-c-switch-case\">Break In C Switch Case <\/a><\/li><li ><a href=\"#faq-break-statement-in-c\">FAQ- Break Statement in C<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"break-statement-in-c\">Break Statement in C<\/h2>\n\n\n\n<p>In C programming, the &#8220;break&#8221; statement is a handy tool that allows you to control how your code behaves. It&#8217;s particularly useful for stopping loops and switching statements before they would normally finish. This gives you more control over your program and helps you respond to specific conditions as needed. In simpler terms, the &#8220;break&#8221; statement is like a stop button for loops and switch statements in C.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-break-in-c\">What is Break in C<\/h2>\n\n\n\n<p>In C programming, the &#8220;break&#8221; statement serves as a way to control loops. When encountered, it exits the loop immediately, allowing you to control the flow of your program. You can use it inside loops or switch statements to exit the block of code. It&#8217;s important to note that the &#8220;break&#8221; statement can only exit one loop at a time, not multiple nested loops simultaneously.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"syntax-of-break-in-c\">Syntax of Break in C<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>break;\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"use-of-break-in-c\">Use of Break in C<\/h2>\n\n\n\n<p>The &#8220;break&#8221; statement in C is a tool used to exit a loop prematurely. It can be employed with various types of loops, such as &#8220;for&#8221; loops, &#8220;while&#8221; loops, or &#8220;do-while&#8221; loops, to transfer program control out of the loop at a specific point. This allows you to terminate the loop before it naturally reaches its end based on certain conditions or requirements in your program.<\/p>\n\n\n\n<p>In C, we can use the break statement in the following ways::<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Simple Loops<\/strong><\/li>\n\n\n\n<li><strong>Nested Loops<\/strong><\/li>\n\n\n\n<li><strong>Infinite Loops<\/strong><\/li>\n\n\n\n<li><strong>Switch case<\/strong><\/li>\n<\/ul>\n\n\n\n<p><strong>Example 1: C Program to use break Statement with Simple Loops<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\/\/ C Program to demonstrate break statement with for loop\n#include &lt;stdio.h&gt;\n \nint main()\n{\n \n    \/\/ using break inside for loop to terminate after 2\n    \/\/ iteration\n    printf(\"break in for loop\\n\");\n    for (int i = 1; i &lt; 5; i++) {\n        if (i == 3) {\n            break;\n        }\n        else {\n            printf(\"%d \", i);\n        }\n    }\n \n    \/\/ using break inside while loop to terminate after 2\n    \/\/ iteration\n    printf(\"\\nbreak in while loop\\n\");\n    int i = 1;\n    while (i &lt; 20) {\n        if (i == 3)\n            break;\n        else\n            printf(\"%d \", i);\n        i++;\n    }\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p><strong>Example 2: C Program to use break Statement with Nested Loops<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\/\/ C program to illustrate\n\/\/ using break statement\n\/\/ in Nested loops\n#include &lt;stdio.h&gt;\n \nint main()\n{\n    \/\/ nested for loops with break statement\n    \/\/ at inner loop\n    for (int i = 1; i &lt;= 6; ++i) {\n        for (int j = 1; j &lt;= i; ++j) {\n            if (i &lt;= 4) {\n                printf(\"%d \", j);\n            }\n            else {\n                \/\/ if i &gt; 4 then this innermost loop will\n                \/\/ break\n                break;\n            }\n        }\n        printf(\"\\n\");\n    }\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1 \n1 2 \n1 2 3 \n1 2 3 4<\/code><\/pre>\n\n\n\n<p><strong>Example 3: C Program to use break Statement with Infinite Loops<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C Program to demonstrate infinite loop without using\n\/\/ break statement\n#include &lt;stdio.h&gt;\n \nint main()\n{\n \n    int i = 0;\n \n    \/\/ while loop which will always be true\n    while (1) {\n        printf(\"%d \", i);\n        i++;\n        if (i == 5) {\n            break;\n        }\n    }\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>0 1 2 3 4 \n<\/code><\/pre>\n\n\n\n<p>In the given program, there is a potential issue where the loop condition is always set to true, which would result in the loop running indefinitely, causing an infinite loop. However, this problem is addressed by incorporating the &#8220;break&#8221; statement. By using &#8220;break,&#8221; the loop is controlled, and its iteration is limited to 8 iterations. This way, you prevent the loop from running indefinitely and ensure it terminates after a specific number of iterations, enhancing the program&#8217;s stability and predictability.<\/p>\n\n\n\n<p>The working of the break statement in C is described below:<\/p>\n\n\n\n<p><strong>STEP 1:<\/strong> The loop begins executing after evaluating the test condition.<\/p>\n\n\n\n<p><strong>STEP 2:<\/strong> If there&#8217;s a &#8220;break&#8221; condition in the loop, it&#8217;s checked.<\/p>\n\n\n\n<p><strong>STEP 3A:<\/strong> If the &#8220;break&#8221; condition is true, the program immediately jumps to the statements below the loop, effectively ending the loop prematurely.<\/p>\n\n\n\n<p><strong>STEP 3B:<\/strong> If the &#8220;break&#8221; condition is false, the program continues executing as usual within the loop.<\/p>\n\n\n\n<p>This mechanism allows you to control and exit the loop based on specific conditions, providing flexibility and control over your code&#8217;s behavior.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"break-in-c-switch-case\">Break In C Switch Case <\/h2>\n\n\n\n<p>The &#8220;switch&#8221; case statement in C evaluates an expression and, based on the value of the expression, executes the statement associated with the matching value. Importantly, if there are multiple cases after the matching one, all of them will be executed sequentially unless stopped. To halt this sequential execution, the &#8220;break&#8221; statement is used within the &#8220;switch&#8221; case. When &#8220;break&#8221; is encountered, it exits the &#8220;switch&#8221; block, preventing further case statements from being executed. This helps control the flow of the program within the &#8220;switch&#8221; construct.<\/p>\n\n\n\n<p>Syntax of break in Switch Case<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>switch(expression)\n{    \ncase value1:\n    statement_1;\n    break;\n    \ncase value2:\n    statement_2;\n    break;\n.....\n.....\n\ncase value_n:\n    statement_n;\n    break;\n    \ndefault:\n    default statement;\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"example-of-break-in-switch-case\">Example of break in switch case<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C Program to demonstrate working of break with switch\n\/\/ case\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n \nint main()\n{\n    char c;\n    float x, y;\n \n    while (1) {\n        printf(\"Enter an operator (+, -), if want to exit \"\n               \"press x: \");\n        scanf(\" %c\", &amp;c);\n        \/\/ to exit\n        if (c == 'x')\n            exit(0);\n \n        printf(\"Enter Two Values:\\n \");\n        scanf(\"%f %f\", &amp;x, &amp;y);\n \n        switch (c) {\n        \/\/ For Addition\n        case '+':\n            printf(\"%.1f + %.1f = %.1f\\n\", x, y, x + y);\n            break;\n        \/\/ For Subtraction\n        case '-':\n            printf(\"%.1f - %.1f = %.1f\\n\", x, y, x - y);\n            break;\n        default:\n            printf(\n                \"Error! please write a valid operator\\n\");\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter an operator (+, -), if want to exit press x: +\nEnter Two Values:\n10\n20\n10.0 + 20.0 = 30.0<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-break-statement-in-c\">FAQ- Break Statement in C<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1695210415112\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. What is break vs exit statement in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. The key difference between &#8220;break&#8221; and &#8220;exit()&#8221; in C is that &#8220;break&#8221; is a keyword used to immediately exit a loop or switch statement, while &#8220;exit()&#8221; is a standard library function that terminates the entire program&#8217;s execution when called. &#8220;break&#8221; controls the flow within constructs, while &#8220;exit()&#8221; ends the program entirely.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1695210424872\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. What is break in C with example?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. The &#8220;break&#8221; command in C enables you to exit a loop (such as &#8220;do,&#8221; &#8220;for,&#8221; or &#8220;while&#8221;) or a switch statement prematurely, bypassing its normal completion. It can only be used within the body of a loop or a switch statement. It&#8217;s important to note that the &#8220;break&#8221; keyword must be in lowercase and cannot be abbreviated.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1695210433955\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. Why is break statement used?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. The &#8220;break&#8221; statement in C is a loop control statement designed to terminate a loop. When the program encounters a &#8220;break&#8221; statement within a loop, it immediately stops the loop iterations and transfers control to the first statement after the loop, effectively exiting the loop prematurely.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Break Statement in C In C programming, the &#8220;break&#8221; statement is a handy tool that allows you to control how your code behaves. It&#8217;s particularly useful for stopping loops and switching statements before they would normally finish. This gives you more control over your program and helps you respond to specific conditions as needed. In &#8230; <a title=\"Break Statement in C\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/break-statement-in-c\/\" aria-label=\"More on Break Statement in C\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":5302,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[341],"class_list":["post-2014","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-break-statement-in-c","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2014","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=2014"}],"version-history":[{"count":13,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2014\/revisions"}],"predecessor-version":[{"id":10620,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2014\/revisions\/10620"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/5302"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=2014"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=2014"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=2014"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}