In this section, we will explore the break
and continue
statements in Java, which are used to control the flow of loops. These statements provide a way to exit a loop or skip the current iteration, respectively.
- Break Statement
The break
statement is used to exit from a loop or switch statement before it has completed its normal sequence. When a break
statement is encountered inside a loop, the loop is immediately terminated, and the program control resumes at the next statement following the loop.
Syntax
Example
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 is 5 } System.out.println(i); } System.out.println("Loop terminated."); } }
Explanation
- The loop starts with
i
equal to 1 and incrementsi
by 1 in each iteration. - When
i
becomes 5, thebreak
statement is executed, and the loop is terminated. - The program then prints "Loop terminated."
Output
- Continue Statement
The continue
statement is used to skip the current iteration of a loop and proceed to the next iteration. When a continue
statement is encountered, the remaining code inside the loop for the current iteration is skipped, and the loop proceeds with the next iteration.
Syntax
Example
public class ContinueExample { public static void main(String[] args) { for (int i = 1; i <= 10; i++) { if (i == 5) { continue; // Skip the iteration when i is 5 } System.out.println(i); } System.out.println("Loop completed."); } }
Explanation
- The loop starts with
i
equal to 1 and incrementsi
by 1 in each iteration. - When
i
becomes 5, thecontinue
statement is executed, and the current iteration is skipped. - The program then proceeds with the next iteration, printing the numbers except for 5.
Output
- Practical Exercises
Exercise 1: Using Break
Write a program that finds the first number divisible by 7 between 1 and 100 and exits the loop once it finds the number.
Solution
public class FindDivisibleBy7 { public static void main(String[] args) { for (int i = 1; i <= 100; i++) { if (i % 7 == 0) { System.out.println("First number divisible by 7 is: " + i); break; } } } }
Exercise 2: Using Continue
Write a program that prints all numbers from 1 to 20, but skips the numbers that are divisible by 3.
Solution
public class SkipDivisibleBy3 { public static void main(String[] args) { for (int i = 1; i <= 20; i++) { if (i % 3 == 0) { continue; } System.out.println(i); } } }
- Common Mistakes and Tips
Common Mistakes
- Using
break
andcontinue
incorrectly: Ensure you understand the difference betweenbreak
andcontinue
.break
exits the loop, whilecontinue
skips the current iteration. - Infinite loops: Be cautious when using
break
andcontinue
in loops to avoid creating infinite loops.
Tips
- Use
break
to exit a loop early when a certain condition is met. - Use
continue
to skip specific iterations of a loop based on a condition.
Conclusion
In this section, we learned about the break
and continue
statements in Java. The break
statement is used to exit a loop prematurely, while the continue
statement is used to skip the current iteration and proceed to the next one. Understanding these control flow statements is essential for writing efficient and effective loops in Java. In the next section, we will delve into the concepts of Object-Oriented Programming (OOP) in Java.
Java Programming Course
Module 1: Introduction to Java
- Introduction to Java
- Setting Up the Development Environment
- Basic Syntax and Structure
- Variables and Data Types
- Operators
Module 2: Control Flow
Module 3: Object-Oriented Programming
- Introduction to OOP
- Classes and Objects
- Methods
- Constructors
- Inheritance
- Polymorphism
- Encapsulation
- Abstraction
Module 4: Advanced Object-Oriented Programming
Module 5: Data Structures and Collections
Module 6: Exception Handling
Module 7: File I/O
Module 8: Multithreading and Concurrency
- Introduction to Multithreading
- Creating Threads
- Thread Lifecycle
- Synchronization
- Concurrency Utilities
Module 9: Networking
- Introduction to Networking
- Sockets
- ServerSocket
- DatagramSocket and DatagramPacket
- URL and HttpURLConnection