User Tools

Site Tools


Control Statements and Exceptions

break

This statement is used in do...while, for, for...in and while loops, as well as in switch blocks, to terminate the execution of the nearest enclosing block.

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.

Examples

In a for loop.

for( var i = 0; i < limit; i++ ){
	if( condition )
		break;
	// ...statements
}
// ...statements

In a for loop, nested with a label.

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.

switch( expression ){
	case 1:
		// ...statements
		break;
	case 2:
		label:
		while( condition ){
			if( expression ){
				break label;
			}
			// ...statements
		}
		// *falls through*
	default:
		// ...statements
		break;
}
// ...statements

See also:

case

This statement serves as a condition label for switch blocks. For each possible condition a 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.

Examples

switch( expression ){
	case Number:
		// ...statements
		break;
	case String:
		// ...statements
		// *falls through*
	case Object:
		// ...statements
		// *falls through*
	default:
		// ...statements
		break;
}
// ...statements

See also:

catch

This statement is used as a counterpart to try statements for handling exceptions that may occur. If an exception does occur in the statements within the try block, the statements in the catch block are executed. catch blocks are available in two flavors, unqualified and qualified.

Qualified Exceptions:

  • 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.

Examples

Unqualified.

try{
	// ...statements
}
catch( e ){
	// ...statements
}

Qualified.

try{
	// ...statements
}
catch( e if e instanceOf EvalError ){
	// ...statements
}

See also:

continue

This statement is used in do...while, for, for...in and while blocks to transfer control to the controlling-expression of the nearest enclosing block. All remaining statements in the current iteration are not executed. If a 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).

Examples

In a for loop.

for( 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 immediately passes to the controlling-expression, which is re-evaluated, and if true, passes control back to the block for further execution.

do{
	// ...statements
	if( condition )
		continue;
	// ...statements
} while( controlling-expression );
// ...statements
while( controlling-expression ){
	// ...statements
	if( condition )
		continue;
	// ...statements
}
// ...statements

See also:

default

This statement is used in switch blocks to define the default statements to be executed in the event that the 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.

Examples

switch( expression ){
	case Number:
		// ...statements
		break;
	case String:
		// ...statements
		break;
	case Object:
		// ...statements
		break;
	default:
		// ...statements
		break;
}
// ...statements

See also:

do...while

This statement is used to create a loop that is guaranteed to execute the enclosed statements at least once. If the 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.

Examples

do{
	// ...statements
} while( condition );
// ...statements

See also:

else

This statement is used in conjunction with the if statement to provide optional statements for execution. If no additional conditions exist, other than that used in the 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.

Examples

Single condition.

if( condition ){
	// ...statements
}
else{
	// ...statements
}
// ...statements

Multiple conditions.

if( condition ){
	// ...statements
}
else if( condition ){
	// ...statements
}
else{
	// ...statements
}
// ...statements

See also:

finally

This statement is used in conjunction with the try and catch statements to handle errors and provide a sequence of statements to be executed, unconditionally, in the event that an error does occur. The statements enclosed within a try block will be executed regardless of whether or not a return statement is encountered in the associated try and/or catch block(s).

Examples

try{
	// ...statements
}
catch( e ){
	// ...statements
}
finally{
	// ...statements
}

for

This statement is used to create a loop that executes for as long as an expression is 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.

Examples

for( var i = 0; i < limit; i++ ){
	// ...statements
}
// ...statements

See also:

for...in

This statement is used to create a loop that executes for each element of an Array.

Examples

// EXAMPLE NEEDED

See also:

if

This statement is used to conditionally execute a sequence of statements, depending on the value of an expression. If the condition evaluates 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.

Examples

Single condition.

if( condition ){
	// ...statements
}
// ...statements

Single condition with optional else block.

if( condition ){
	// ...statements
}
else{
	// ...statements
}
// ...statements

Multiple conditions.

if( condition ){
	// ...statements
}
else if( condition ){
	// ...statements
}
else{
	// ...statements
}
// ...statements

See also:

label

Statements can be labelled. Labels are used to pass control from break and continue statements to a specific statement outside of the enclosing block.

Examples

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

See also:

return

This statement is used to return from a function (with the exception of constructors), optionally returning a value from that 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.

Examples

function demo( sArg ){
	return String( "%1 was passed in." ).arg( sArg );
}
var sTmp = demo( 5 );
// sTmp = "5 was passed in."

See also:

switch

This statement is used to provide a multi-way branch by evaluating an expression (only once). It is used in conjunction with the case and default clauses to determine a statement, or sequence of statements, to execute based on whether the expression evaluates to one of the labels of the defined cases.

Examples

switch( expression ){
	case Number:
		// ...statements
		break;
	case String:
		// ...statements
		break;
	case Object:
		// ...statements
		break;
	default:
		// ...statements
		break;
}
// ...statements

See also:

throw

This statement is used to generate an error condition to be handled by a catch block. The exception thrown can be any expression.

Examples

// ...statements
if( i > limit ){
	throw "RangeError : Index out of range.";
}

See also:

try

This statement is used to signify a statement, or sequence of statements, to attempt execution. It is used in conjunction with catch and finally blocks to handle errors that may occur.

Examples

try{
	// ...statements
}
catch( e ){
	// ...statements
}
finally{
	// ...statements
}

See also:

while

This statement is used to execute a statement, or sequence of statements, until a boolean expression evaluates 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.

Examples

while( i < 100 ){
	// ...statements
	i++;
}
// ...statements

See also:

with

This statement is used to establish the default object for a block of code, to indicate that any unqualified names should be qualified with the provided object. It is commonly used to shorten the length of code that has to be written for a given segment of code.

Examples

More code.

// EXAMPLE NEEDED

Same result, less code.

// EXAMPLE NEEDED

Note: Leaving the interpreter to do the lookup for you may be slower than providing the fully qualified names.