Loop Control Statements in Java
Understanding break and continue
Preface
In Java, certain statements can alter the normal inflow of a circle. These are known as Loop Control Statements. They help in managing how and when a circle should terminate or skip duplications.
The two main circle control statements in Java are:
break
- continue
Let’s explore each in detail with exemplifications.
1. The break Statement
As we’ve seen in Python, the break
statement is used to exit a circle precociously when a certain condition is met. This means the circle stops running indeed if its normal terminating condition has n’t been reached yet.
Syntax
Explanation
In this illustration, the circle runs from i = 0
to i<5
, but when i
reaches 3, the break
statement gets executed. This incontinently terminates the circle, skipping the remaining duplications.
break in Nested circles
When break
is used inside a nested circle, it only stops the inmost circle in which it's placed, while the external circle continues prosecution.
illustration Using break in a Nested Loop
Explanation
Then, the break
statement stops the inner circle when j == 1
, but the external circle (i
circle) continues running typically.
break in an horizonless Loop
The break
statement is also useful in horizonless circles — circles that run indefinitely unless manually stopped.
illustration Breaking an horizonless Loop
Explanation
Since while( true)
creates an horizonless circle, it would typically noway stop. still, when num
reaches 4, the break
statement is touched off, and the circle terminates.
2. The continue Statement
The continue
statement works a little else than break
. rather of stopping the circle entirely, it skips the current replication and moves directly to the coming bone.
Syntax
Explanation
When i == 1
, the continue
statement is executed, which skips the remaining law inside the circle for that replication. The circle also moves to the coming replication (i = 2
) and continues as normal.
Summary
- Loop Control Statements help manage how circles bear in Java.
break
: Terminates a circle incontinently.continue
: Skips the current replication and moves to the coming bone.