Break vs continue

Break vs continue

Both of these reserved words are used in loops to end the current iteration of the loop often before getting to the end of the statements in the loop. They are also most often used inside of a if statement.

Break is used to end the loop before the loop would consider the condition false. So when the break statement is used it will end the loop and give control of the program to the statement right after the end of the loop. Break is also common to see inside of switch statements.

Continue is used to end the current iteration of the loop. So when it’s used it will disregard the rest of the loop that comes after it and go back to the conditional statement, and run the loop again as normal.

Ex:

for (int I =0; I<5; I ++)

{

if (j=3 && I=<2)

continue;

else if (j=3)// I >2

break;

else

j-1;

}

system.out.print(j);

So in this loop if j meets the conditions of the if statement then it will print out a 1. While if the conditions of the else if statement are met it will print out a 3. While if neither of them are met the answer it will print out will just be j(before the loop started)-5.