{"id":5206,"date":"2021-03-23T12:24:51","date_gmt":"2021-03-23T12:24:51","guid":{"rendered":"https:\/\/ded9.com\/?p=5206"},"modified":"2025-11-05T10:52:25","modified_gmt":"2025-11-05T10:52:25","slug":"learn-how-to-break-in-java-in-very-simple-language","status":"publish","type":"post","link":"https:\/\/ded9.com\/tr\/learn-how-to-break-in-java-in-very-simple-language\/","title":{"rendered":"Learn the break Statement in Java \u2014 Explained in Very Simple Language"},"content":{"rendered":"<p><span style=\"font-size: 12pt;\">In this tutorial, you will learn to use a break in Java to end a loop. Suppose you are working with circles. Sometimes,<\/span><span style=\"font-size: 12pt;\">\u00a0you need to skip some commands inside the loop or end the loop immediately without checking the condition.<\/span><\/p>\n<p>In such cases, break and continue commands are used. The break statement terminates the loop immediately, and program control follows the loop. This is often used with decision statements (if\u2026 else).<\/p>\n<p>The break structure is:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">break;<\/pre>\n<\/div>\n<h2><span style=\"font-size: 18pt;\">How does the <a href=\"https:\/\/www.w3schools.com\/java\/java_break.asp\" target=\"_blank\" rel=\"noopener\">break<\/a> Command work?<\/span><\/h2>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"wp-image-50092 aligncenter lazyloaded\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2025\/02\/c-users-mr-desktop-java-break-statement-works-jpg-1.jpeg\" alt=\"C: \\ Users \\ Mr \\ Desktop \\ java-break-statement-works.jpg\" width=\"560\" height=\"354\" data-ll-status=\"loaded\" \/><\/p>\n<h3><strong>Example 1: break in Java<\/strong><\/h3>\n<h3><strong><img decoding=\"async\" class=\"size-full wp-image-258196 aligncenter\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/java-break-statement.png\" alt=\"break in Java\" width=\"550\" height=\"302\" srcset=\"https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/java-break-statement.png 550w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/java-break-statement-300x165.png 300w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/> <\/strong><\/h3>\n<blockquote>\n<ol>\n<li dir=\"ltr\">class Test {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">for (int i = 1; i &lt;= 10; ++ i) {<\/li>\n<li dir=\"ltr\">if (i == 5) {<\/li>\n<li dir=\"ltr\">break;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">System.out.println (i);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p><strong>Output<\/strong><\/p>\n<blockquote>\n<p dir=\"ltr\">1<\/p>\n<p dir=\"ltr\">\u06f2<\/p>\n<p dir=\"ltr\">3<\/p>\n<p dir=\"ltr\">4<\/p>\n<\/blockquote>\n<p>In the above program, when the vi value equals 5, we put the expression i == 5 in parentheses. Then, the break Command is executed, terminating the for loop.<\/p>\n<p><strong>Example 2: The phrase break in Java<\/strong><\/p>\n<p>The following program calculates the sum of the numbers the user enters until they enter a negative number.<\/p>\n<p>The Scanner object is used to get input from the user.<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">import java.util.Scanner;<\/li>\n<li dir=\"ltr\">class UserInputSum {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">Double number, sum = 0.0;<\/li>\n<li dir=\"ltr\">Scanner input = new Scanner (System.in);<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">while (true) {<\/li>\n<li dir=\"ltr\">System.out.print (&#8220;Enter a number:&#8221;);<\/li>\n<li dir=\"ltr\">number = input.nextDouble ();<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">if (number &lt;0.0) {<\/li>\n<li dir=\"ltr\">break;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">sum + = number;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">System.out.println (\u201cSum =\u201d + sum);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p><strong>Output<\/strong><\/p>\n<blockquote>\n<p dir=\"ltr\">Enter a number: 3.2<\/p>\n<p dir=\"ltr\">Enter a number: 5<\/p>\n<p dir=\"ltr\">Enter a number: 2.3<\/p>\n<p dir=\"ltr\">Enter a number: 0<\/p>\n<p dir=\"ltr\">Enter a number: -4.5<\/p>\n<p dir=\"ltr\">Sum = 10.5<\/p>\n<\/blockquote>\n<p>In the above program, the while loop condition is always proper and runs until the user enters a negative number. If the user enters a negative number, the Command is executed inside the if body, which terminates the while <a href=\"https:\/\/ded9.com\/for-while-and-do-while-loops-in-java-in-simple-language\/\">loop<\/a>.<\/p>\n<p>If there are nested loops, the break terminates the innermost loop.<\/p>\n<p><img decoding=\"async\" class=\"wp-image-50093 aligncenter lazyloaded\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2025\/02\/c-users-mr-desktop-nested-while-loop-break-1-jp.jpeg\" alt=\"C: \\ Users \\ Mr \\ Desktop \\ nested-while-loop-break (1) .jpg\" width=\"300\" height=\"238\" data-ll-status=\"loaded\" \/><\/p>\n<p>The term break terminates the inner while loop, and the program control jumps to the outer loop.<\/p>\n<h2><span style=\"font-size: 18pt;\">Labeled break Command<\/span><\/h2>\n<p>The break statement we have discussed so far is the unmarked form break, which terminates the innermost loops for while, do\u2026 while, and switch. Another form of break command has a break tag and can be used to terminate the outer loop.<\/p>\n<h2>How does the labeled break Command work?<\/h2>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-50094 aligncenter lazyloaded\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2025\/02\/c-users-mr-desktop-labeled-break-statement-java-j.jpeg\" alt=\"C: \\ Users \\ Mr \\ Desktop \\ labeled-break-statement-Java.jpg\" width=\"350\" height=\"276\" data-ll-status=\"loaded\" \/><\/strong><\/p>\n<p>Here, the label is an identifier. When you run break, the labeled Command ends, and the program control jumps to after the labeled Command.<\/p>\n<p>Another example:<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">while (testExpression) {<\/li>\n<li dir=\"ltr\">\/\/ codes<\/li>\n<li dir=\"ltr\">second:<\/li>\n<li dir=\"ltr\">while (testExpression) {<\/li>\n<li dir=\"ltr\">\/\/ codes<\/li>\n<li dir=\"ltr\">while (testExpression) {<\/li>\n<li dir=\"ltr\">\/\/ codes<\/li>\n<li dir=\"ltr\">break second;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">\/\/ control jumps here<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p>When running break, the program control jumps to the phrase labeled second.<\/p>\n<h3><strong>Example 3: Labeled break in Java structure<\/strong><\/h3>\n<blockquote>\n<ol>\n<li dir=\"ltr\">class LabeledBreak {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">first:<\/li>\n<li dir=\"ltr\">for (int i = 1; i &lt;5; i ++) {<\/li>\n<li dir=\"ltr\">second:<\/li>\n<li dir=\"ltr\">for (int j = 1; j &lt;3; j ++) {<\/li>\n<li dir=\"ltr\">System.out.println (\u201ci =\u201d + i + \u201c; j =\u201d + j);<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">if (i == 2)<\/li>\n<li dir=\"ltr\">break first;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p><strong>Output<\/strong><\/p>\n<blockquote>\n<p dir=\"ltr\">i = 1;\u00a0j = 1<\/p>\n<p dir=\"ltr\">i = 1;\u00a0j = 2<\/p>\n<p dir=\"ltr\">i = 2;\u00a0j = 1<\/p>\n<\/blockquote>\n<p>Here is another method of the above program: break. In the following program, the program control jumps to the second tagged phrase after terminating the first tagged phrase.<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">class LabeledBreak {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">first:<\/li>\n<li dir=\"ltr\">for (int i = 1; i &lt;5; i ++) {<\/li>\n<li dir=\"ltr\">second:<\/li>\n<li dir=\"ltr\">for (int j = 1; j &lt;3; j ++) {<\/li>\n<li dir=\"ltr\">System.out.println (\u201ci =\u201d + i + \u201c; j =\u201d + j);<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">if (i == 2)<\/li>\n<li dir=\"ltr\">break second;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p><strong>Output<\/strong><\/p>\n<blockquote>\n<p dir=\"ltr\">i = 1;\u00a0j = 1<\/p>\n<p dir=\"ltr\">i = 1;\u00a0j = 2<\/p>\n<p dir=\"ltr\">i = 2;\u00a0j = 1<\/p>\n<p dir=\"ltr\">i = 3;\u00a0j = 1<\/p>\n<p dir=\"ltr\">i = 3;\u00a0j = 2<\/p>\n<p dir=\"ltr\">i = 4;\u00a0j = 1<\/p>\n<p dir=\"ltr\">i = 4;\u00a0j = 2<\/p>\n<\/blockquote>\n<p>The break Command is also used to terminate the switch.<\/p>\n<h2>FAQ<\/h2>\n<div id=\"rank-math-rich-snippet-wrapper\"><div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-1\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What does the break statement do in Java?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>It immediately exits the nearest enclosing loop or switch block, skipping any remaining iterations or cases.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-2\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">When should I use break inside loops?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Use it to stop looping when a condition is met (e.g., you found a match) instead of waiting for the loop to finish all iterations.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-3\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">How is break different from continue and return?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>break exits the loop\/switch; continue skips to the next loop iteration; return exits the entire method.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn to use a break in Java to end a loop. Suppose you are working with circles. Sometimes,\u00a0you need to skip some commands inside the loop or end the loop immediately without checking the condition. In such cases, break and continue commands are used. The break statement terminates the loop [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":5207,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[11513],"tags":[840],"class_list":["post-5206","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-2","tag-java"],"acf":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts\/5206","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/comments?post=5206"}],"version-history":[{"count":6,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts\/5206\/revisions"}],"predecessor-version":[{"id":265028,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts\/5206\/revisions\/265028"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/media\/5207"}],"wp:attachment":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/media?parent=5206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/categories?post=5206"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/tags?post=5206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}