The break and continue can be used in JavaScript with loop instrucxtions: for() and while() to stop or "jump over" loops.


The break statement

The break statement stops the execution of a repetitive instruction.
- Example, a for() loop is stoped with 'break':
<script>
//defined to run till x is 10
for(x=0; x<10; x++){
 document.write('<br> X is '+x);

 //stops completely the loop when x is 3
 if(x==3) break;
} 
</script>
In the same way you can use it with while():
<script>
var x =0;

//defined to run till x is 10
while(x <10){
 document.write('<br> X este '+x);

 //stops completely the loop when x is 3
 if(x==3) break;
 x++;
} 
</script>

The continue statement

The continue statement 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 1 and 3:
<script>
//defined to run from 0 to 5
for(x=0; x<5; x++){
 //skips the execution when x is 1 or 3
 if(x ==1 || x ==3) continue;
 document.write('<br> X ise '+x);
} 
</script>
In the same way you can use it with while():
<script>
var x =0;

//defined to run from 0 to 5
while(x <5){
 //skips the execution when x is 1 or 3
 if(x ==1 || x ==3){
 x++; //to increment when it jumps over iteration
 continue;
 }

 document.write('<br> X este '+x);
 x++;
} 
</script>

JS label statements

label statements can be used in JavaScript with 'break' or 'continue'. It labels a group of nested repetitive instructions, allowing control over that group inside the nested instructions.

Syntax:
label_name:
for(...){
 //other instructions with for() or while()
}
- 'label_name' can be any name (except the reserved JavaScript keywords), followed by : and the nested group of for() or while().

Studying the following example, you can better understand how the 'label' it works:
<script>
loopX: //labels the next set of nested instructions
for(var x=0; x<5; x++){
 document.write('<h4>X - '+x+'</h4>');
 for(var y=0; y<3; y++){
 //stops execution of the entire group when x is 2
 if(x ==2) break loopX;

 document.write('<br>Y = '+y);
 }
}
</script>

- Testing the code, you will see that though 'break' is inside the second 'for()', by specifying 'loopX', which is the label of the entire set of nested instructions, it will stops the execution of entire group.

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which HTML element can be used to embed a SWF flash content?
<object> <div> <script>
<object type="application/x-shockwave-flash" data="file.swf" width="500" height="250">
 <param name="src" value="file.swf" />
 Your browser not support SWF.
</object>
Which CSS pseudo-class adds a style to an input form field that has keyboard input focus?
:active :focus :hover
input:focus {
  background-color: #88fe88;
}
Click on the instruction which converts a JSON string into a JavaScript object.
JSON.stringify(javascript_object) object.toString() JSON.parse(json_string)
var jsnstr = '{"url": "http://coursesweb.net/", "title": "Web Development Courses"}';
var obj = JSON.parse(jsnstr);
alert(obj.url);
Indicate the PHP function which can be used to create or write a file on server.
fopen() file_put_contents() file_get_contents()
if (file_put_contents("file.txt", "content")) echo "The file was created";
else echo "The file can not be created";
break, continue, and label

Last accessed pages

  1. Convert BBCode to HTML and HTML to BBCode with JavaScript (4226)
  2. Read Excel file data in PHP - PhpExcelReader (61137)
  3. Convert XML to JSON in PHP (8234)
  4. Register and show online users and visitors (25584)
  5. Integer and Float value in Select with PDO from string to numeric (5104)

Popular pages this month

  1. Insert, Select and Update NULL value in MySQL (561)
  2. Read Excel file data in PHP - PhpExcelReader (469)
  3. PhpSpreadsheet - Read, Write Excel and LibreOffice Calc files (344)
  4. PHP getElementById and getElementsByTagName (267)
  5. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (254)