Break and Continue in Java ?
Break is a reserved keyword in java which we can use in our code when we want to jump out of the loop or switch statement immediately.
Break in for loop:
public class BreakExample {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break; // exit the loop when i equals 5
}
System.out.println("i = " + i);
}
System.out.println("Loop ended.");
}
}
Output:
i = 1
i = 2
i = 3
i = 4
Loop ended.
Break in while loop:
public class BreakWhileExample {
public static void main(String[] args) {
int i = 1;
while (i <= 10) {
if (i == 5) {
break; // exit the loop when i equals 5
}
System.out.println("i = " + i);
i++;
}
System.out.println("Loop ended.");
}
}
Output:
i = 1
i = 2
i = 3
i = 4
Loop ended.
Break in do while loop:
public class BreakDoWhileExample {
public static void main(String[] args) {
int i = 1;
do {
if (i == 5) {
break; // exit the loop when i equals 5
}
System.out.println("i = " + i);
i++;
} while (i <= 10);
System.out.println("Loop ended.");
}
}
Output:
i = 1
i = 2
i = 3
i = 4
Loop ended.
now as you have gone through the code completely regarding the break statement let me explain you how the code is working in the first example, "break in for loop" a for loop is running from 1 to 10 when i = 1 it will print the i value when i = 2 it will print till i =4 it will continue printing as long as i = 5, it will break out of the loop and then print "Loop ended". which is written outside the for loop, hope you got the clarity. In the similar fashion code is working in while loop and do while loop now for that explanation I recommend you to analyze the code from yourself and copy paste it in your local editor everything is same except the while and do while loop functionalities for that we have a specific article on that have a look loops in java.
What is Continue Statement ?
It is also a reserved keyword in java, continue statement is used to stop the current iteration in the loop and continue from the next iteration in the loop have a look at the code for better understanding of how continue actually works in java:
public class ContinueExample {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
continue; // skip this iteration when i equals 5
}
System.out.println("i = " + i);
}
}
}
Output:i = 1
i = 2
i = 3
i = 4
i = 6
i = 7
i = 8
i = 9
i = 10
let me explain you what actually is happening in this continue code example a loop is running from 1 to 10 from i= 1 to i = 4 it will print the i value simply when i = 5 it will skip this iteration and continue printing next values in this way it will now print 6 to 10 hope it makes sense.
Now let's see Break and Continue together in a code snippet for more clarity:
i = 1
i = 2
i = 3
i = 4
i = 6
i = 7
i = 8
i = 9
i = 10
let me explain you what actually is happening in this continue code example a loop is running from 1 to 10 from i= 1 to i = 4 it will print the i value simply when i = 5 it will skip this iteration and continue printing next values in this way it will now print 6 to 10 hope it makes sense.
Now let's see Break and Continue together in a code snippet for more clarity:
public class BreakContinueExample {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i == 3) {
continue; // skip printing when i equals 3
}
if (i == 7) {
break; // exit the loop when i equals 7
}
System.out.println("i = " + i);
}
System.out.println("Loop ended.");
}
}
Output:i = 1
i = 2
i = 4
i = 5
i = 6
Loop ended.
Now I hope you got the complete clarity around break and continue statements in java and how to use it when to use it everything around break and continue now let's wrap up this article with the conclusion and let's see a summary what we have actually learned in this article.
Conclusion:
Suppose we are traversing through a loop of 10 elements (1 to 10). If we want to print only specific elements based on certain conditions—for example:
-
Print only odd numbers between 1 to 5, and
-
Print only even numbers from 6 to 10—
In this case, we can use the continue
statement to skip elements we don’t want to print, and we can use the break
statement if we want to stop the loop immediately in the middle.
So, these two reserved keywords in Java help us control the flow of loops effectively.
👉 I recommend practicing by writing different variations of code in your local editor. Run them, analyze the outputs, and experiment with different conditions. That way, you will get a crystal-clear understanding of how break
and continue
work in Java till then bye and we will see you in some another article 😊.
i = 1 i = 2 i = 4 i = 5 i = 6Loop ended.
Now I hope you got the complete clarity around break and continue statements in java and how to use it when to use it everything around break and continue now let's wrap up this article with the conclusion and let's see a summary what we have actually learned in this article.
Conclusion:
Suppose we are traversing through a loop of 10 elements (1 to 10). If we want to print only specific elements based on certain conditions—for example:
-
Print only odd numbers between 1 to 5, and
-
Print only even numbers from 6 to 10—
Print only odd numbers between 1 to 5, and
Print only even numbers from 6 to 10—
In this case, we can use the continue
statement to skip elements we don’t want to print, and we can use the break
statement if we want to stop the loop immediately in the middle.
So, these two reserved keywords in Java help us control the flow of loops effectively.
👉 I recommend practicing by writing different variations of code in your local editor. Run them, analyze the outputs, and experiment with different conditions. That way, you will get a crystal-clear understanding of how break
and continue
work in Java till then bye and we will see you in some another article 😊.
Comments
Post a Comment
Hello, visitor your comments are so helpful for us so do not hesitate just write the comment we read all your comments so don't think your comment goes waste.