Loop Control Statements in Java

ZoneTechPro
By -
0

 


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


break;


illustration Breaking Out of a Loop


class Main {  
    public static void main( String() args) {  
        for( int i =  0; i< 5; i) {  
            if( i ==  3) {  
                break; // Exits the  circle when i is 3  
            }  
            System.out.println(i);  
        }  
        System.out.println(" END");  
    }  
}


Affair:


0  
1  
2  
END  


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


class Main {  
    public static void main( String() args) {  
        for( int i =  0; i< 3; i) {  
            System.out.println(" external " + i);  

            for( int j =  0; j< 3; j) {  
                if( j ==  1) {  
                    break; // Exits inner  circle when j is 1  
                }  
                System.out.println(" Inner " + j);  
            }  
        }  
    }  
}


Affair:


external 0  
 Inner 0  
external 1  
 Inner 0  
external 2  
 Inner 0  


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 circlescircles that run indefinitely unless manually stopped.

illustration Breaking an horizonless Loop



class Main {  
    public static void main( String() args) {  
        int num =  1;  

        while( true) { // horizonless  circle  
            if( num ==  4) {  
                break; // Stops the  circle when num reaches 4  
            }  
            System.out.println(num);  
            num++;  
        }  
    }  
}


Affair:


1  
2  
3  


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

continue;


illustration Skipping an replication


class Main {  
    public static void main( String() args) {  
        for( int i =  0; i< 3; i) {  
            if( i ==  1) {  
                continue; // Skips when i is 1  
            }  
            System.out.println(i);  
        }  
    }  
}


Affair:


0  
2  



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.


Related Posts :

        
Java Introduction & Basics

         Sequence of Instructions in Java

         Java Data Types

         Java Input and Output Basics

         Java Type Conversions

         Java String Methods

         Java Logical Operators

         Conditional Statements in Java

         Java Ternary Operator

         Java Switch Statement

         Loops in Java

      Java String Method

          Chracters of Methods



Tags:

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Learn more
Ok, Go it!