Break Example in java

package Loops;
import java.util.Scanner;
public class factorial {
    public static void main(String[] args) {

        Scanner sc=new Scanner(System.in);
        for(;;) {        //infiniteloop
            int n=sc.nextInt();
            if(n<0) {
                break;
            }
        }
    }
}
   
   
output:-
2
3
4
5
-4 //when we input negative number its break the loop and come outside


Comments