| Keywords: |
|---|
When a break statement is encountered in a block, control is passed to the statement (if any) following the end of the nearest enclosing block that it appears in; unless the break statement is followed by the name of a label, in which case control passes to the statement governed by the label.
for( var i = 0; i < limit; i++ ){ if( condition ) break; // ...statements } // ...statements
label:
for( var i = 0; i < limit; i++ ){
for( var j = 0; j < limit2; j++ ){
if( condition )
break label;
// ...statements
}
// ...statements
}
// ...statement governed by label
In a switch block, a break statement is typically placed at the end of each case clause to prevent the interpreter from "falling through" (unless, of course, "falling through" is the desired effect). When the interpreter encounters a break statement, control is passed to the statement following the nearest enclosing switch block. If every clause has a corresponding break statement, at most only one clause's statements will be executed. If the break statement is followed by a label name (e.g. label), when the break is encountered, control is passed to the statement marked with that label.
while block and labelswitch( expression ){ case 1: // ...statements break; case 2: label: while( condition ){ if( expression ){ break label; } // ...statements } // *falls through* default: // ...statements break; } // ...statements
switch statement's expression can evalute to, a case is used to identify which statements within the block are to be executed. If the literal value of a case clause matches the value of the switch statements' expression, then that case is executed.
A break statement is typically placed at the end of each case clause to prevent the interpreter from "falling through" to the next clause. Omitting the break statement for a given clause, causes every statement in the case block to be executed, from the matching case label to the first break statement, or the end of the switch block, including the default clause, whichever is encountered first.
switch( expression ){ case Number: // ...statements break; case String: // ...statements // *falls through* case Object: // ...statements // *falls through* default: // ...statements break; } // ...statements
try block, the statements in the catch block are executed. catch blocks are available in two flavors, unqualified and qualified.
try{ // ...statements } catch( e ){ // ...statements }
try{ // ...statements } catch( e if e instanceOf EvalError ){ // ...statements }
| EvalError | The result of a failed eval() call | |
| RangeError | A result that is out of range (e.g. an array index to beyond the length of the array) | |
| TypeError | An attempt to perform an operation on an object of an inappropriate type | |
| UserDefined | Exceptions that are raised by a throw statement |
continue statement is encountered in a for block, execution immediately passes to the loop-expression and continues normally (e.g. the controlling-expression is tested, and if found to be true, the statements in the block are executed).
for loopfor( initializing-expression; controlling-expression; loop-expression ){ // ...statements if( condition ) continue; // ...statements } // ...statements
If a continue statement is encountered in a do...while or while loop, execution immeadiately passes to the controlling-expression, which is re-evaluated, and if true, passes control back to the block for further execution.
do loopdo{ // ...statements if( condition ) continue; // ...statements } while( controlling-expression ); // ...statements
while loopwhile( controlling-expression ){ // ...statements if( condition ) continue; // ...statements } // ...statements
switch statement's expression doesn't evaluate to a case clause that has been defined. If no default clause is defined and none of the case clause labels match, the switch will not execute any statements within the block and control will be passed to the statement following the switch block. Because each case is evaluated in ascending order, if a default clause is used, it should always be the last clause in the switch block. Because default is a "catch all" clause, if the interpreter reaches it, it will execute the associated statements.
It is customary to end a default clause with a break statement.
switch( expression ){ case Number: // ...statements break; case String: // ...statements break; case Object: // ...statements break; default: // ...statements break; } // ...statements
while condition evalutes true, control is passed back to the do statement and the block is executed again. If the while condition evaluates false, control is passed to the statement immediately following the while statement.
do{ // ...statements } while( condition ); // ...statements
if block, the else keyword is used alone. However, if an additional condition does exist, the else keyword is followed by the if keyword and the corresponding condition.
if( condition ){ // ...statements } else{ // ...statements } // ...statements
if( condition ){ // ...statements } else if( condition ){ // ...statements } else{ // ...statements } // ...statements
try block will be executed regardless of whether or not a return statement is encountered in the associated try and/or catch block(s).
try{ // ...statements } catch( e ){ // ...statements } finally{ // ...statements }
true, typically a fixed number of times.
A for statement has three expressions that are used to control how the loop is executed:
The first expression is the initializing-expression. This statement is typically used to initialize (and possibly declare) the variable used in the controlling-expression.
The second expression is the controlling-expression. This statement is typically a conditional expression that is evaluated before each iteration of the loop (including before the first iteration). If this expression evaluates false, the enclosed statements are not executed for that iteration and control is passed to the third expression. If the expression never evaluates true the enclosed statements are never executed and control is passed to the statement immediately following the for block.
The third part, the loop-expression, contains a statement that is executed at the end of every iteration. Typically, this part is used to increment the variable initialized in the first expression.
for( var i = 0; i < limit; i++ ){ // ...statements } // ...statements
// EXAMPLE NEEDED
false, control is passed to the statement immediately following the if block. When used in conjunction with an else and/or else if block, only the statements enclosed in one of the blocks is executed before passing control to the statement immediately following the block(s).It is generally considered good practice to enclose the sequence of statements governed by the condition in matching curly braces ({}), for the sake of clarity and to help avoid inadvertent errors. It is, however, valid (albeit not recommended) syntax to omit the curly braces if only one statement is governed by the condition.
if( condition ){ // ...statements } // ...statements
else blockif( condition ){ // ...statements } else{ // ...statements } // ...statements
if( condition ){ // ...statements } else if( condition ){ // ...statements } else{ // ...statements } // ...statements
OuterLabel:
for( var i = 0; i < limit; i++ ){
InnerLabel:
for( var j = 0; j < limit; j++ ){
// ...statements
if( condition ){
// ...statements
break OuterLabel;
}
else{
// ...statements
continue InnerLabel;
}
}
// ...statements
}
// ...statements
function. When a return statement is encountered, control is passed to the statement following the one that called the function. If the return keyword is followed by an expression, the value of the expression is returned to the calling statement.
function demo( sArg ){
return String( "%1 was passed in." ).arg( sArg );
}
var sTmp = demo( 5 );
// sTmp = "5 was passed in."
switch( expression ){ case Number: // ...statements break; case String: // ...statements break; case Object: // ...statements break; default: // ...statements break; } // ...statements
// ...statements if( i > limit ){ throw "RangeError : Index out of range."; }
try{ // ...statements } catch( e ){ // ...statements } finally{ // ...statements }
false. The expression is tested before each iteration of the loop, and if true, executes the enclosed statement(s). If the expression evaluates false, control is passed to the statement immediately following the enclosing block.
while( i < 100 ){ // ...statements i++; } // ...statements
// EXAMPLE NEEDED
// EXAMPLE NEEDED