Udostępnij za pośrednictwem


break and continue Statements

The break statement in JScript stops the execution of a loop if some condition is met. (Note that break is also used to exit a switch block). The continue statement can be used to jump immediately to the next iteration, skipping the rest of the code block while updating the counter variable if the loop is a for or for...in loop.

Using break and continue Statements

The following example illustrates the use of the break and continue statements to control a loop.

for(var i = 0;i <=10 ;i++) {
   if (i > 7) {
      print("i is greater than 7.");
      break; // Break out of the for loop.
   }
   else {
      print("i = " + i);
      continue; // Start the next iteration of the loop.
      print("This never gets printed.");
   }
}

The output of this program is

i = 0
i = 1
i = 2
i = 3
i = 4
i = 5
i = 6
i = 7
i is greater than 7.

See Also

Reference

break Statement

continue Statement

Other Resources

Loops in JScript

JScript Conditional Structures

JScript Reference