Anurag A. Anurag A. - 1 month ago 16
Java Question

how do i tell the computer to stop the execution of for loop if certain condition is true within the loop?

public static void main(String[] args)
{
for (i=1,i>=1,i++)
{
int A;
Scanner input=new Scanner(System.in);
A= input.nextInt();
if (A==3)


How to Stop the execution of for loop in this case to get a valid output?

Answer

You can use break operator without any parameters, or, if you want to skip some iterations you can use continue the same way.

For example:

for (int i = 0; i < 10; i++) {
    if (i > 5 + 1) break;
    System.out.println(i);
}