[Contents] [Previous] [Next] [Last]

Chapter 14
Statements

Statements are core to the JavaScript language. This section includes new statements and changed statements.

break

The break statement can now include an optional label that allows the program to break out of a labeled statement. This type of break must be in a statement identified by the label used by break.

The statements in a labeled statement can be of any type.

Syntax

break label

Argument

label is the identifier associated with the label of the statement.

Example

In the following example, a statement labeled checkiandj contains a statement labeled checkj. If break is encountered, the program breaks out of the checkj statement and continues with the remainder of the checkiandj statement. If break had a label of checkiandj, the program would break out of the checkiandj statement and continue at the statement following checkiandj.

checkiandj : 
   if (4==i) { 
      document.write("You've entered " + i + ".<BR>");
      checkj : 
         if (2==j) {
            document.write("You've entered " + j + ".<BR>"); 
            break checkj; 
            document.write("The sum is " + (i+j) + ".<BR>"); 
         } 
      document.write(i + "-" + j + "=" + (i-j) + ".<BR>"); 
   }

See Also

labeled, switch

continue

The continue statement can now include an optional label that allows the program to terminate execution of a labeled statement and continue to the specified labeled statement. This type of continue must be in a looping statement identified by the label used by continue.

Syntax

continue label

Argument

label is the identifier associated with the label of the statement.

Example

In the following example, a statement labeled checkiandj contains a statement labeled checkj. If continue is encountered, the program continues at the top of the checkj statement. Each time continue is encountered, checkj re-iterates until its condition returns false. When false is returned, the remainder of the checkiandj statement is completed. checkiandj re-iterates until its condition returns false. When false is returned, the program continues at the statement following checkiandj.

If continue had a label of checkiandj, the program would continue at the top of the checkiandj statement.

checkiandj : 
   while (i<4) {
      document.write(i + "<BR>"); 
      i+=1; 
      checkj : 
         while (j>4) {
            document.write(j + "<BR>"); 
            j-=1; 
            if ((j%2)==0);
               continue checkj; 
            document.write(j + " is odd.<BR>");
         } 
      document.write("i = " + i + "<br>");
      document.write("j = " + j + "<br>");  
   }

See Also

labeled

do while

The do while statement executes its statements until the test condition evaluates to false. Statement is executed at least once.

Syntax

do 
   statement
while (condition);

Arguments

statement is a block of statements that is executed at least once and is re-executed each time the condition evaluates to true.

condition is evaluated after each pass through the loop. If condition evaluates to true, the statements in the preceding block are re-executed. When condition evaluates to false, control passes to the statement following do while.

Example

In the following example, the do loop iterates at least once and re-iterates until i is no longer less than 5.

do {
   i+=1
   document.write(i);
   } while (i<5);

export

The export statement allows a signed script to provide functions objects to other signed or unsigned scripts.

Syntax

export name1, name2, ..., nameN
export *

Parameters

nameN is a list of functions to be exported.

* exports all functions from the script.

Description

Typically, information in a signed script is available only to scripts signed by the same principals. By exporting functions, a signed script makes this information available to any script (signed or unsigned). The receiving script uses the companion import statement to access the information.

See Also

import

import

The import statement allows a script to import functions from a signed script which has exported the information.

Syntax

import objectName.name1, objectName.name2, ..., objectName.nameN
import objectName.*

Parameters

nameN is a list of functions to import from the export file.

objectName is the name of the object that will receive the imported names. For example, if f and p have been exported, and if obj is an object from the importing script, then

import obj.f, obj.p
will make f and p accessible in the importing script as properties of obj.

* imports all functions from the export script.

Description

Typically, information in a signed script is available only to scripts signed by the same principals. By exporting (using the export statement) properties, functions, or objects, a signed script makes this information available to any script (signed or unsigned). The receiving script uses the import statement to access the information.

The script must load the export script into a window or layer before it can import and use any exported functions.

See Also

export

labeled

A labeled statement provides an identifier that can be used with break or continue to indicate where the program should continue execution.

In a labeled statement, break or continue must be followed with a label, and the label must be the identifier of the labeled statement containing break or continue.

Syntax

label :
   statement

Arguments

statement is a block of statements. break can be used with any labeled statement, and continue can be used with looping labeled statements.

Example

For an example of a labeled statement using break, see break.

For an example of a labeled statement using continue, see continue.

See Also

break, continue

switch

A switch statement allows a program to evaluate an expression and attempt to match the expression's value to a case label. If a match is found, the program executes the associated statement.

The program first looks for a label matching the value of expression and then executes the associated statement. If no matching label is found, the program looks for the optional default statement, and if found, executes the associated statement. If no default statement is found, the program continues execution at the statement following the end of switch.

The optional break statement associated with each case label ensures that the program breaks out of switch once the matched statement is executed and continues execution at the statement following switch. If break is omitted, the program continues execution at the next statement in the switch statement.

Syntax

switch (expression){
         case label : 
              statement;
              break;
         case label : 
              statement;
              break;
         ...
         default : statement;
}

Arguments

expression is the value matched against label.

label is an identifier used to match against expression.

statement is any statement.

Example

In the following example, if expression evaluates to "Bananas", the program matches the value with case "Bananas" and executes the associated statement. When break is encountered, the program breaks out of switch and executes the statement following switch. If break were omitted, the statement for case "Cherries" would also be executed.

switch (i) { 
   case "Oranges" : 
      document.write("Oranges are $0.59 a pound.<BR>"); 
      break; 
   case "Apples" : 
      document.write("Apples are $0.32 a pound.<BR>"); 
      break; 
   case "Bananas" : 
      document.write("Bananas are $0.48 a pound.<BR>"); 
      break; 
   case "Cherries" : 
      document.write("Cherries are $3.00 a pound.<BR>"); 
      break; 
   default : 
      document.write("Sorry, we are out of " + i + ".<BR>"); 
   } 
document.write("Is there anything else you'd like?<BR>");


[Contents] [Previous] [Next] [Last]

Last Updated: 10/22/97 11:48:17


Copyright 1997 Netscape Communications Corporation