Break and Continue

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 27:- Break and Continue

In Java, break and continue are two control flow statements that allow you to modify the behavior of loops.

  1. break statement:
    • The break statement is used to terminate the enclosing loop prematurely. When encountered, the loop immediately exits, and the program continues with the first statement after the loop.
    • The break statement is commonly used when a certain condition is met, and there's no need to continue with the rest of the loop iterations.

Example of using break:

javaCopy code
public class BreakExample { public static void main(String[] args) { for (int i = 1; i <= 5; i++) { System.out.println("Iteration " + i); if (i == 3) { break; // Exit the loop when i equals 3 } } System.out.println("Loop terminated"); } }

Output:

vbnetCopy code
Iteration 1 Iteration 2 Iteration 3 Loop terminated

In the example above, the loop runs from 1 to 5, and when i becomes 3, the break statement is encountered, causing the loop to terminate prematurely.

  1. continue statement:
    • The continue statement is used to skip the current iteration of a loop and move on to the next iteration.
    • When the continue statement is encountered, the program jumps to the next iteration, and any code after the continue statement within the loop's body will be skipped for the current iteration.

Example of using continue:

javaCopy code
public class ContinueExample { public static void main(String[] args) { for (int i = 1; i <= 5; i++) { if (i == 3) { continue; // Skip the current iteration when i equals 3 } System.out.println("Iteration " + i); } System.out.println("Loop finished"); } }

Output:

vbnetCopy code
Iteration 1 Iteration 2 Iteration 4 Iteration 5 Loop finished

In the example above, the loop runs from 1 to 5, and when i becomes 3, the continue statement is encountered. This causes the program to skip the code inside the loop for that iteration and move on to the next iteration.

Both break and continue statements are useful tools to control the flow of loop execution in specific scenarios and make your code more efficient and readable. However, they should be used judiciously to avoid creating confusing or hard-to-read code.

 

Disclaimer:-

Under Section 107 of the copyright act 1976, allowance is made for fair use for purposes such as criticism, comment, news reporting, scholarship, and research. Fair use is a use permitted by copyright statute that might otherwise be infringing. Non-profit, educational, or personal use tips the balance in favor of fair use.

 

6. Loops

Comments: 0

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?