You have already seen the break statement used in an earlier chapter of this tutorial. It was used to "jump out" of a switch statement.The break statement can also be used to jump out of a loop.This example jumps out of the for loop when i is equal to 4:...
Remove break statement boolean run = true;do { x = v0 * t * Math.cos(inRadians) + x0; y = v0 * t * Math.sin(inRadians) - (g * t * t) / 2 + y0; System.out.printf("%3.2f\t%7.3f\t%7.3f\n", t, x, y); hitTarget = x >= 17 && x <= 20 && y <= 3 &&...
Thebreakstatement can also be used to jump out of a loop: Example for(leti =0; i <10; i++) { if(i ===3) {break; } text +="The number is "+ i +""; } Try it Yourself » In the example above, thebreakstatement ends the loop ("breaks" the loop) when the loop counter...
if(i ==4) { i++; continue; } cout << i <<"\n"; i++; } Try it Yourself » Exercise? What does thebreakstatement do in a loop? Exits the loop immediately Skips the current iteration Repeats the current iteration Submit Answer »...
Thecontinuestatement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop. This example skips the value of 4: Example for(inti=0;i<10;i++){if(i==4){continue;}System.out.println(i);} ...