{"id":1170,"date":"2022-02-24T19:28:48","date_gmt":"2022-02-24T19:28:48","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1170"},"modified":"2022-02-24T19:28:48","modified_gmt":"2022-02-24T19:28:48","slug":"java-break-statement","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/24\/java-break-statement\/","title":{"rendered":"Java break Statement"},"content":{"rendered":"\n<p>In this tutorial, you will learn about the break statement, labeled break statement in Java with the help of examples.<\/p>\n\n\n\n<p id=\"introduction\">While working with loops, it is sometimes desirable to skip some statements inside the loop or terminate the loop immediately without checking the test expression.<\/p>\n\n\n\n<p>In such cases,\u00a0<code>break<\/code>\u00a0and\u00a0<code>continue<\/code>\u00a0statements are used. You will learn about the\u00a0Java continue statement\u00a0in the next tutorial.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>The&nbsp;<code>break<\/code>&nbsp;statement in Java terminates the loop immediately, and the control of the program moves to the next statement following the loop.<\/p>\n\n\n\n<p>It is almost always used with decision-making statements (Java if&#8230;else Statement).<\/p>\n\n\n\n<p>Here is the syntax of the break statement in Java:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>break;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"working-of-break\">How break statement works?<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/java-break-statement-works.jpg\" alt=\"How break statement works in Java programming?\" title=\"Working of Java break Statement\"\/><figcaption>Working of Java break Statement<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"break-example\">Example 1: Java break statement<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Test {\n    public static void main(String&#91;] args) {\n      \n        \/\/ for loop\n        for (int i = 1; i &lt;= 10; ++i) {\n\n            \/\/ if the value of i is 5 the loop terminates  \n            if (i == 5) {\n                break;\n            }      \n            System.out.println(i);\n        }   \n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>1\n2\n3\n4<\/samp><\/code><\/pre>\n\n\n\n<p>In the above program, we are using the\u00a0<code>for<\/code>\u00a0loop to print the value of\u00a0<var>i<\/var>\u00a0in each iteration. To know how\u00a0<code>for<\/code>\u00a0loop works, visit the\u00a0Java for loop. Here, notice the statement,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (i == 5) {\n    break;\n}<\/code><\/pre>\n\n\n\n<p>This means when the value of&nbsp;<var>i<\/var>&nbsp;is equal to 5, the loop terminates. Hence we get the output with values less than 5 only.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Example 2: Java break statement<\/h3>\n\n\n\n<p>The program below calculates the sum of numbers entered by the user until user enters a negative number.<\/p>\n\n\n\n<p>To take input from the user, we have used the\u00a0<code>Scanner<\/code>\u00a0object.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Scanner;\n\nclass UserInputSum {\n    public static void main(String&#91;] args) {\n      \n        Double number, sum = 0.0;\n\n        \/\/ create an object of Scanner\n        Scanner input = new Scanner(System.in);\n      \n        while (true) {\n            System.out.print(\"Enter a number: \");\n\n            \/\/ takes double input from user\n            number = input.nextDouble();\n         \n            \/\/ if number is negative the loop terminates\n            if (number &lt; 0.0) {\n                break;\n            }\n         \n           sum += number;\n        }\n        System.out.println(\"Sum = \" + sum);\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Enter a number: 3.2\nEnter a number: 5\nEnter a number: 2.3\nEnter a number: 0\nEnter a number: -4.5\nSum = 10.5<\/samp><\/code><\/pre>\n\n\n\n<p>In the above program, the test expression of the&nbsp;<code>while<\/code>&nbsp;loop is always&nbsp;<code>true<\/code>. Here, notice the line,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (number &lt; 0.0) {\n    break;\n}<\/code><\/pre>\n\n\n\n<p>This means when the user input negative numbers, the while loop is terminated.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"break-nested-loops\">Java break and Nested Loop<\/h2>\n\n\n\n<p>In the case of\u00a0nested loops, the\u00a0<code>break<\/code>\u00a0statement terminates the innermost loop.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/nested-while-loop-break.jpg\" alt=\"The break statement terminates the innermost while loop in case of nested loops.\" title=\"Working of break Statement with Nested Loops\"\/><figcaption>Working of break Statement with Nested Loops<\/figcaption><\/figure>\n\n\n\n<p>Here, the break statement terminates the innermost&nbsp;<code>while<\/code>&nbsp;loop, and control jumps to the outer loop.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"labeled-break\">Labeled break Statement<\/h2>\n\n\n\n<p>Till now, we have used the unlabeled break statement. It terminates the innermost loop and switch statement. However, there is another form of break statement in Java known as the labeled break.<\/p>\n\n\n\n<p>We can use the labeled break statement to terminate the outermost loop as well.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/labeled-break-statement-Java.jpg\" alt=\"The labeled break statement is used to break the outermost loop.\" title=\"Working of the labeled break statement in Java\"\/><figcaption>Working of the labeled break statement in Java<\/figcaption><\/figure>\n\n\n\n<p>As you can see in the above image, we have used the&nbsp;<var>label<\/var>&nbsp;identifier to specify the outer loop. Now, notice how the&nbsp;<code>break<\/code>&nbsp;statement is used (<code>break label;<\/code>).<\/p>\n\n\n\n<p>Here, the&nbsp;<code>break<\/code>&nbsp;statement is terminating the labeled statement (i.e. outer loop). Then, the control of the program jumps to the statement after the labeled statement.<\/p>\n\n\n\n<p>Here&#8217;s another example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>while (testExpression) {\n   \/\/ codes\n   second:\n   while (testExpression) {\n      \/\/ codes\n      while(testExpression) {\n         \/\/ codes\n         break second;\n      }\n   }\n   \/\/ control jumps here\n}<\/code><\/pre>\n\n\n\n<p>In the above example, when the statement&nbsp;<code>break second;<\/code>&nbsp;is executed, the&nbsp;<code>while<\/code>&nbsp;loop labeled as&nbsp;<var>second<\/var>&nbsp;is terminated. And, the control of the program moves to the statement after the second&nbsp;<code>while<\/code>&nbsp;loop.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-labeled-break\">Example 3: labeled break Statement<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class LabeledBreak {\n    public static void main(String&#91;] args) {\n   \n        \/\/ the for loop is labeled as first   \n        first:\n        for( int i = 1; i &lt; 5; i++) {\n\n            \/\/ the for loop is labeled as second\n            second:\n            for(int j = 1; j &lt; 3; j ++ ) {\n                System.out.println(\"i = \" + i + \"; j = \" +j);\n             \n                \/\/ the break statement breaks the first for loop\n                if ( i == 2)\n                    break first;\n            }\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>i = 1; j = 1\ni = 1; j = 2\ni = 2; j = 1<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, the&nbsp;<code>labeled break<\/code>&nbsp;statement is used to terminate the loop labeled as first. That is,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>first:\nfor(int i = 1; i &lt; 5; i++) {...}<\/code><\/pre>\n\n\n\n<p>Here, if we change the statement&nbsp;<code>break first;<\/code>&nbsp;to&nbsp;<code>break second;<\/code>&nbsp;the program will behave differently. In this case,&nbsp;<code>for<\/code>&nbsp;loop labeled as second will be terminated. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class LabeledBreak {\n    public static void main(String&#91;] args) {\n      \n        \/\/ the for loop is labeled as first\n        first:\n        for( int i = 1; i &lt; 5; i++) {\n\n            \/\/ the for loop is labeled as second\n            second:\n            for(int j = 1; j &lt; 3; j ++ ) {\n\n                System.out.println(\"i = \" + i + \"; j = \" +j);\n       \n                \/\/ the break statement terminates the loop labeled as second   \n                if ( i == 2)\n                    break second;\n            }\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>i = 1; j = 1\ni = 1; j = 2\ni = 2; j = 1\ni = 3; j = 1\ni = 3; j = 2\ni = 4; j = 1\ni = 4; j = 2<\/samp><\/code><\/pre>\n\n\n\n<p><strong>Note<\/strong>: The&nbsp;<code>break<\/code>&nbsp;statement is also used to terminate cases inside the&nbsp;<code>switch<\/code>&nbsp;statement.&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn about the break statement, labeled break statement in Java with the help of examples. While working with loops, it is sometimes desirable to skip some statements inside the loop or terminate the loop immediately without checking the test expression. In such cases,\u00a0break\u00a0and\u00a0continue\u00a0statements are used. You will learn about the\u00a0Java [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[363],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1170"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1170"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1170\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}