Java-Control Infinite Loop

Control Infinite Loop Using Break

If you can force immediate termination of a loop, bypassing the conditional expression and any remaining code in the body of the loop. When a break statement is encountered inside a loop, the loop is terminated and program control resumes at the next statement following the loop. The break statement can be used with any of Java’s loops, including intentionally infinite loops.

Program

1

Program Source

class Javaapp {

    public static void main(String[] args) {
        
        int i = 0;
        
        for( ; ; )
        {
            System.out.println("Value of i = "+i);
            
            if(i==5)
            {
                break;
            }
            i++;
        }  
    }
}

Leave a Comment