{"id":150617,"date":"2021-01-29T09:29:56","date_gmt":"2021-01-29T09:29:56","guid":{"rendered":"https:\/\/www.softwaretestinghelp.com\/?page_id=150617"},"modified":"2025-04-01T08:37:18","modified_gmt":"2025-04-01T08:37:18","slug":"java-break-statement","status":"publish","type":"page","link":"https:\/\/www.softwaretestinghelp.com\/java-break-statement\/","title":{"rendered":"Java Break Statement &#8211; Tutorial With Examples"},"content":{"rendered":"<p><strong>Learn about the Java Break statement with the help of practical programming examples of how to use Break in Java programs:<\/strong><\/p>\n<p>In this tutorial, we will discuss another Jump statement of Java i.e. break statements.<\/p>\n<p>We will explain the break statement with description, programming examples, and see how to use break inside various loops and Switch statements, etc.<\/p>\n<p><strong>=&gt; <a href=\"https:\/\/www.softwaretestinghelp.com\/java\/\">Explore The Simple Java Training Series Here<\/a><\/strong><\/p>\n<p><strong><em> <\/em><\/strong><\/p>\n<h3><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/12\/Java-break-statement.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-151346\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/12\/Java-break-statement.png\" alt=\"Java break statement\" width=\"700\" height=\"394\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/12\/Java-break-statement.png 700w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/12\/Java-break-statement-300x169.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/a><\/h3>\n<h2>Java Break Statement<\/h2>\n<p>Unlike <a href=\"https:\/\/www.softwaretestinghelp.com\/java-continue-statement\/\">Java continue<\/a>, the break statement in Java is used to immediately terminate the loop without executing the remaining part of the body of the loop. Even though it is not mandatory, it is mostly used inside a Switch statement.<\/p>\n<h3>Exiting A Loop Using A Break In Java<\/h3>\n<p>In this example, we have taken a \u201cfor loop\u201d and demonstrated how to jump out of the loop using a break statement.<\/p>\n<p>This is the most simple and common example of a break statement.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">public class example {\r\n\r\n\tpublic static void main(String&#x5B;] args) {\r\n\r\n\t\tfor (int i = 0; i &lt; 20; i++) {\r\n\t\t\tif (i == 6) {\r\n\t\t\t\tbreak;\r\n\t\t\t}\r\n\t\t\tSystem.out.println(i);\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/12\/Exiting-a-loop.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-150621\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/12\/Exiting-a-loop.png\" alt=\"Exiting a loop\" width=\"441\" height=\"177\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/12\/Exiting-a-loop.png 441w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/12\/Exiting-a-loop-300x120.png 300w\" sizes=\"(max-width: 441px) 100vw, 441px\" \/><\/a><\/p>\n<h3>Labeled Break With A Nested Loop<\/h3>\n<p>In this type of break, we provide the name of the label that identifies the code block.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre>break label;<\/pre>\n<p>Whenever this kind of break executes, the control is transferred out of the block i.e. it bypasses the execution of any following statement.<\/p>\n<p>Here, we are demonstrating how the labeled break statement behaves in a nested loop. A nested loop is nothing but the loop inside a loop. The first loop that appears is known as the outer loop and the loops that are inside the first loop are called the inner loop.<\/p>\n<p>In this example, when the inner loop &#8216;j&#8217; breaks to the outer loop &#8216;i&#8217;, both the loops are terminated. This means that &#8216;i&#8217; is iterated only once whereas &#8216;j&#8217; is iterated 5 times i.e. from 0 to 4.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">class example {\r\n\r\n\tpublic static void main(String args&#x5B;]) {\r\n\r\n\t\touter: for (int i = 0; i &lt; 3; i++) {\r\n\r\n\t\t\tSystem.out.print(&quot;For i = &quot; + i + &quot;: &quot;);\r\n\t\t\tSystem.out.println(&quot;j value is: &quot;);\r\n\t\t\t\r\n\t\t\t\/*\r\n\t\t\t * When the inner loop 'j' breaks to the outer loop\r\n\t\t\t * 'i', both the loops are terminated. This means\r\n\t\t\t * that the 'i' is iterated only once whereas the 'j'\r\n\t\t\t * is iterated 5 times i.e. from 0 to 4.\r\n\t\t\t *\/\r\n\t\t\t\r\n\t\t\tfor (int j = 0; j &lt; 100; j++) {\r\n\t\t\t\tif (j == 5)\r\n\t\t\t\t\tbreak outer;\r\n\t\t\r\n\t\t\t\tSystem.out.println(&quot; &quot; + j);\r\n\t\t\t}\r\n\t\t\t\r\n\t\t}\r\n\t\tSystem.out.println(&quot;Loops terminated&quot;);\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/12\/Labeled-break-with-a-nested-loop.png\"><img decoding=\"async\" class=\"alignnone wp-image-150622 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/12\/Labeled-break-with-a-nested-loop.png\" alt=\"with a nested loop\" width=\"468\" height=\"196\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/12\/Labeled-break-with-a-nested-loop.png 468w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/12\/Labeled-break-with-a-nested-loop-300x126.png 300w\" sizes=\"(max-width: 468px) 100vw, 468px\" \/><\/a><\/p>\n<h3>Break Java With A While Loop<\/h3>\n<p>In the below example, we have been incrementing the value of \u2018i\u2019 that is initialized with the value 5. As soon as the value becomes 10, the control goes inside the if statement inside the while loop and the loop gets terminated.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">class example {\r\n\r\n\tpublic static void main(String args&#x5B;]) {\r\n\r\n\t\tint i = 5;\r\n\r\n\t\t\/\/ while loop starts here\r\n\t\twhile (i &lt;= 25) {\r\n\t\t\tSystem.out.println(i);\r\n\t\t\ti = i + 1;\r\n\r\n\t\t\t\/\/ as soon as 'i' becomes 10, the loop ends.\r\n\r\n\t\t\tif (i == 10) {\r\n\t\t\t\tbreak;\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/12\/Break-with-a-while-loop.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-150623\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/12\/Break-with-a-while-loop.png\" alt=\"Break with a while loop\" width=\"547\" height=\"151\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/12\/Break-with-a-while-loop.png 547w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/12\/Break-with-a-while-loop-300x83.png 300w\" sizes=\"(max-width: 547px) 100vw, 547px\" \/><\/a><\/p>\n<h3>Break Java With A Switch<\/h3>\n<p>In this example, we will demonstrate how to use a break statement inside a \u201cSwitch statement\u201d. As we know, when a break statement is encountered the Switch gets terminated. The control of the program moves to the code\/statement following the Switch statement.<\/p>\n<p>Even though it is optional, we can also use a break inside every Switch case. In the below program, see the switch (a) -&gt; case 2. If we remove the break then case 3 will also be executed. So, the break helps in maintaining the isolation of different Switch cases from each other.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">import java.util.Scanner;\r\n\r\npublic class example {\r\n\tpublic static void main(String&#x5B;] args) {\r\n\t\tint a, b;\r\n\t\tSystem.out.println(&quot;Enter a and b&quot;);\r\n\t\tScanner in = new Scanner(System.in);\r\n\t\ta = in.nextInt();\r\n\t\tb = in.nextInt();\r\n\r\n\t\t\/\/ Outer Switch starts here\r\n\t\tswitch (a) {\r\n\t\t\/\/ If a = 1\r\n\t\tcase 1:\r\n\r\n\t\t\t\/\/ Inner Switch starts here\r\n\t\t\tswitch (b) {\r\n\t\t\t\/\/ for condition b = 1\r\n\t\t\tcase 1:\r\n\t\t\t\tSystem.out.println(&quot;b is 1&quot;);\r\n\t\t\t\tbreak;\r\n\t\t\t\/\/ for condition b = 2\r\n\t\t\tcase 2:\r\n\t\t\t\tSystem.out.println(&quot;b is 2&quot;);\r\n\t\t\t\tbreak;\r\n\t\t\t\/\/ for condition b = 3\r\n\t\t\tcase 3:\r\n\t\t\t\tSystem.out.println(&quot;b is 3&quot;);\r\n\t\t\t\tbreak;\r\n\t\t\t}\r\n\t\t\tbreak;\r\n\t\t\/\/ for condition a = 2\r\n\t\tcase 2:\r\n\t\t\tSystem.out.println(&quot;a is 2&quot;);\r\n\r\n\t\t\t\/*\r\n\t\t\t *  if we remove this break then case 3 will also be executed\r\n\t\t\t *  for a = 2\r\n\t\t\t *\/\r\n\t\t\t\r\n\t\t\tbreak;\r\n\t\t\/\/ for condition a == 3\r\n\t\tcase 3:\r\n\t\t\tSystem.out.println(&quot;a is 3&quot;);\r\n\t\t\tbreak;\r\n\t\tdefault:\r\n\t\t\tSystem.out.println(&quot;default statement here&quot;);\r\n\t\t\tbreak;\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/12\/Break-with-a-Switch.png\"><img decoding=\"async\" class=\"alignnone wp-image-150624 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/12\/Break-with-a-Switch.png\" alt=\"with a Switch\" width=\"542\" height=\"131\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/12\/Break-with-a-Switch.png 542w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/12\/Break-with-a-Switch-300x73.png 300w\" sizes=\"(max-width: 542px) 100vw, 542px\" \/><\/a><\/p>\n<p><strong>Also read =&gt;&gt;<a href=\"https:\/\/www.softwaretestinghelp.com\/mysql-case-statement\/\"> MySQL CASE Statement Tutorial<\/a><\/strong><\/p>\n<h3>Frequently Asked Questions<\/h3>\n<p><span style=\"color: #ff6600;\"><strong>Q #1) Is it bad to use break Java?<\/strong><\/span><\/p>\n<p><strong>Answer:<\/strong> Not at all. There are many usages of a Java break. The best use can be seen in a Switch statement to maintain the isolation of different Switch cases from each other. Another use of break is to come out of the loop as soon as it meets any specified\/predefined condition.<\/p>\n<p><span style=\"color: #ff6600;\"><strong>Q #2) How do you break a Java program?<\/strong><\/span><\/p>\n<p><strong>Answer:<\/strong> The simplest way of using a break inside a Java program is to initialize a loop followed by specifying certain conditions using the if statement and then the break statement inside the if condition.<\/p>\n<p>However, if you explicitly want your Java program to break then you can use \u201c<strong>System. exit(0);<\/strong>\u201d<\/p>\n<p><span style=\"color: #ff6600;\"><strong>Q #3) Does Break Break Out of all loops Java?<\/strong><\/span><\/p>\n<p><strong>Answer:<\/strong> No. It only breaks the current loop in which the break statement is specified.<\/p>\n<p><span style=\"color: #ff6600;\"><strong>Q #4) Can Break be used in if statement?<\/strong><\/span><\/p>\n<p><strong>Answer:<\/strong> Yes, you can specify the break statement inside the if statement with some conditions.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>For example:<\/strong><\/span><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">for (int i = 0; i &lt; 10; i++) {\r\nif (i == 6) {\r\nbreak;\r\n<\/pre>\n<h2>Conclusion<\/h2>\n<p>In this tutorial, we have explained the Java break statement i.e. the most convenient jump statement. We have properly described the statement along with examples and programs wherever required. Some FAQ\u2019s have been provided for better understanding.<\/p>\n<p>Moreover, we have tried listing the examples of a break statement with different types of loops and conditional statements that will provide you a better understanding of this jump statement.<\/p>\n<p><strong>=&gt; <a href=\"https:\/\/www.softwaretestinghelp.com\/java\/\">Visit Here To Learn Java From Scratch<\/a><\/strong><\/p>\n\r\n\t\t\t<div id=\"daexthefup-container\"\r\n\t\t\t\tclass=\"daexthefup-container daexthefup-layout-stacked daexthefup-alignment-center\"\r\n\t\t\t\tdata-post-id=\"150617\">\r\n\r\n\t\t\t\t<div class=\"daexthefup-feedback\">\r\n\t\t\t\t\t<div class=\"daexthefup-text\">\r\n\t\t\t\t\t\t<h3 class=\"daexthefup-title\">Was this helpful?<\/h3>\r\n\t\t\t\t\t<\/div>\r\n\t\t\t\t\t<div class=\"daexthefup-buttons-container\">\r\n\t\t\t\t\t\t<div class=\"daexthefup-buttons\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t<div class=\"daexthefup-yes daexthefup-button daexthefup-button-type-icon\" data-value=\"1\">\r\n\t\t\t\t\r\n                <svg>\r\n                    <defs>\r\n                        <style>.thumb-up-cls-1{fill:#c9c9c9;}.thumb-up-cls-2{fill:#e1e1e1;}.thumb-up-cls-3{fill:#676767;}<\/style>\r\n                    <\/defs>\r\n                    <g id=\"thumb_up\">\r\n                        <path class=\"thumb-up-cls-2 daexthefup-icon-circle\" d=\"m24,3c11.58,0,21,9.42,21,21s-9.42,21-21,21S3,35.58,3,24,12.42,3,24,3m0-1C11.85,2,2,11.85,2,24s9.85,22,22,22,22-9.85,22-22S36.15,2,24,2h0Z\" \/>\r\n                        <g>\r\n                            <rect class=\"thumb-up-cls-3 daexthefup-icon-secondary-color\" x=\"10\" y=\"20\" width=\"6\" height=\"15\" rx=\"1.5\" ry=\"1.5\" \/>\r\n                            <path class=\"thumb-up-cls-1 daexthefup-icon-primary-color\" d=\"m30.57,9.06l-.49-.1c-.81-.17-1.61.35-1.78,1.16l-5.3,11.74c-.17.81,3.16,1.61,3.97,1.78l1.96.41c.81.17,1.61-.35,1.78-1.16l2.18-10.27c.34-1.61-.7-3.21-2.31-3.56Z\" \/>\r\n                            <path class=\"thumb-up-cls-1 daexthefup-icon-primary-color\" d=\"m38.17,20h-18.67c-.83,0-1.5.67-1.5,1.5v12c0,.83.67,1.5,1.5,1.5h16.27c.71,0,1.33-.5,1.47-1.21l2.4-12c.19-.93-.53-1.8-1.47-1.8Z\" \/>\r\n                        <\/g>\r\n                    <\/g>\r\n                <\/svg>\t\t\t<\/div>\r\n\r\n\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t<div class=\"daexthefup-no daexthefup-button daexthefup-button-type-icon\" data-value=\"0\">\r\n\t\t\t\t\r\n                <svg>\r\n                    <defs>\r\n                        <style>.thumb-down-cls-1{fill:#c9c9c9;}.thumb-down-cls-2{fill:#e1e1e1;}.thumb-down-cls-3{fill:#676767;}<\/style>\r\n                    <\/defs>\r\n                    <g id=\"thumb_down\">\r\n                        <path class=\"thumb-down-cls-2 daexthefup-icon-circle\" d=\"m24,3c11.58,0,21,9.42,21,21s-9.42,21-21,21S3,35.58,3,24,12.42,3,24,3m0-1C11.85,2,2,11.85,2,24s9.85,22,22,22,22-9.85,22-22S36.15,2,24,2h0Z\" \/>\r\n                        <g>\r\n                            <rect class=\"thumb-down-cls-3 daexthefup-icon-secondary-color\" x=\"10\" y=\"13\" width=\"6\" height=\"15\" rx=\"1.5\" ry=\"1.5\" \/>\r\n                            <path class=\"thumb-down-cls-1 daexthefup-icon-primary-color\" d=\"m30.57,38.94l-.49.1c-.81.17-1.61-.35-1.78-1.16l-5.3-11.74c-.17-.81,3.16-1.61,3.97-1.78l1.96-.41c.81-.17,1.61.35,1.78,1.16l2.18,10.27c.34,1.61-.7,3.21-2.31,3.56Z\" \/>\r\n                            <path class=\"thumb-down-cls-1 daexthefup-icon-primary-color\" d=\"m38.17,28h-18.67c-.83,0-1.5-.67-1.5-1.5v-12c0-.83.67-1.5,1.5-1.5h16.27c.71,0,1.33.5,1.47,1.21l2.4,12c.19.93-.53,1.8-1.47,1.8Z\" \/>\r\n                        <\/g>\r\n                    <\/g>\r\n                <\/svg>\t\t\t<\/div>\r\n\r\n\t\t\t\t\t\t\t\t\t<\/div>\r\n\t\t\t\t\t<\/div>\r\n\t\t\t\t<\/div>\r\n\r\n\t\t\t\t<div class=\"daexthefup-comment\">\r\n\t\t\t\t\t<div class=\"daexthefup-comment-top-container\">\r\n\t\t\t\t\t\t<label id=\"daexthefup-comment-label\" class=\"daexthefup-comment-label\"><\/label>\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t<div class=\"daexthefup-comment-character-counter-container\">\r\n\t\t\t\t\t\t\t\t<div id=\"daexthefup-comment-character-counter-number\"\r\n\t\t\t\t\t\t\t\t\tclass=\"daexthefup-comment-character-counter-number\"><\/div>\r\n\t\t\t\t\t\t\t\t<div class=\"daexthefup-comment-character-counter-text\"><\/div>\r\n\t\t\t\t\t\t\t<\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t<\/div>\r\n\t\t\t\t\t<textarea id=\"daexthefup-comment-textarea\" class=\"daexthefup-comment-textarea\"\r\n\t\t\t\t\t\t\t\tplaceholder=\"Type your message\"\r\n\t\t\t\t\t\t\t\tmaxlength=\"\r\n\t\t\t\t\t\t\t\t400\t\t\t\t\t\t\t\t\t\"><\/textarea>\r\n\t\t\t\t\t<div class=\"daexthefup-comment-buttons-container\">\r\n\t\t\t\t\t\t<button class=\"daexthefup-comment-submit daexthefup-button\">Submit<\/button>\r\n\t\t\t\t\t\t<button class=\"daexthefup-comment-cancel daexthefup-button\">Cancel<\/button>\r\n\t\t\t\t\t<\/div>\r\n\t\t\t\t<\/div>\r\n\r\n\t\t\t\t<div class=\"daexthefup-successful-submission-text\">Thanks for your feedback!<\/div>\r\n\r\n\t\t\t<\/div>\r\n\r\n\t\t\t","protected":false},"excerpt":{"rendered":"<p>Learn about the Java Break statement with the help of practical programming examples of how to use Break in Java programs: In this tutorial, we will discuss another Jump statement of Java i.e. break statements. We will explain the break statement with description, programming examples, and see how to use &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Java Break Statement &#8211; Tutorial With Examples\" class=\"read-more button\" href=\"https:\/\/www.softwaretestinghelp.com\/java-break-statement\/#more-150617\" aria-label=\"Read more about Java Break Statement &#8211; Tutorial With Examples\">Read more<\/a><\/p>\n","protected":false},"author":9,"featured_media":151346,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"_acf_changed":false,"_helpful_pro_status":1,"footnotes":""},"categories":[408],"tags":[],"class_list":{"0":"post-150617","1":"page","2":"type-page","3":"status-publish","4":"has-post-thumbnail","6":"category-java"},"acf":[],"_links":{"self":[{"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/pages\/150617","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/comments?post=150617"}],"version-history":[{"count":0,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/pages\/150617\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/media\/151346"}],"wp:attachment":[{"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/media?parent=150617"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/categories?post=150617"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/tags?post=150617"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}