Break

break is an instruction that allows you to stop processing a loop (or "switch" conditional).
If you want to end a loop before its condition is false, you can use the break instruction after a conditional "if()".

Example:
var total:int = 1;

// create a for() loop
for(var i:int=0; i<4; i++)
{
  total *= 2;
  if(total==8) break;         // ends completely this loop
  trace('i='+ i+ ' - total='+ total);
}

// Output:  i=0 - total=2  i=1 - total=4
When the conditional "if(total==8)" returns true, the code after the "break" will not be executed at all, and the execution of this "for()" loop ends.
The Output panel displays:
i=0 - total=2
i=1 - total=4
- The condition (i<4) is still true, becouse the value of "i" is 1, but the "break;" instruction ends completely this loop.

Continue

The continue instruction skips the remaining statements in the current loop and begins the next iteration at the top of the loop.
The difference between "break" and "continue" is that the "break" statement ends completely the loop, while "continue" ends the current iteration only, and lets the loop to continue with the next iteration.

Example:
var total:int = 1;

// create a for() loop
for(var i:int=0; i<4; i++)
{
  total *= 2;

  // skip other actions in the current iteration when total==4 or total==8
  if(total==4 || total==8) continue;

  trace('i='+ i+ ' - total='+ total);
}
The Output is:
i=0 - total=2
i=3 - total=16
- Notice that when the value of "total" is 4 or 8, the trace() statement after the "continue" instruction is not executed, but the for() loop continue with the next iterations until its condition is false.

"break and "continue" can be used with any loop statement: while(), do..while, for(), for..in, for each..in.

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which attribute is used in <img> tag for the address of the image?
href src rel
<img src="http://coursesweb.net/imgs/webcourses.gif" width="191" height="63" alt="Courses-Web" />
Which CSS code hides the element on page?
display: none; display: inline; position: relative;
#id {
  display: none;
}
What instruction stops the execution of a while() or for() statement?
continue prompt() break
for(var i = 0; i< 8; i++) {
  if(i > 1) break;
  alert(i);
}
Indicate the function that can create a constant.
define() include() defined()
define("CONSTANT_NAME", "value");
echo CONSTANT_NAME;
Break and Continue

Last accessed pages

  1. sPBM - Simple PHP Backup Manager (719)
  2. PHP PDO - Select query, fetch (14291)
  3. Creating Mask Layers in Flash (3019)
  4. DirectoryIterator to get file and directory info (1489)
  5. Mystic India Pop (4345)

Top accessed pages

  1. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (49620)
  2. Read Excel file data in PHP - PhpExcelReader (38033)
  3. PHP-MySQL free course, online tutorials PHP MySQL code (36948)
  4. Get Attribute (ID, Class, Name, Title, Src) with jQuery (33324)
  5. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (30696)