HOME        NEXT        PREV

Tutorial 4: Decision Making with Control Structures and Statements


HOME        TOP

Section A: Decision Making

if Statements

if (conditional expression)
    statement;

if (conditional expression) {
    statement(s);
}

if (conditional expression)
    statement;
else 
    statement;

if (conditional expression) {
    statement(s);
}
else {
    statement(s);
}

if (conditional expression) {
    statement(s);
}
else if (conditional expression) {
    statement(s);
}
else if (conditional expression) {
    statement(s);
}
else {
    statement(s);
}

if (conditional expression) {
    statement(s);
}
else
    if (conditional expression) {
        statement(s);
    }
    else {
        statement(s);
    }
}

    Example: Tutorial4Ex1.html


HOME        TOP

switch Statements

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

The labels within a switch statement are called case labels and the identify the specific code segments. A case label consists of the keyword case, followed by a literal value or variable name, followed by a colon. JavaScript compares the value returned from the switch statement expression to the literal value or variable name following the case keyword. If a match is found, the case label statements execute. You can use a variety of data types as case labels within the same switch statement. The default label contains statements that execute when the value returned by the switch statement conditional expression does not match a case label. A switch statement ends automatically after the JavaScript interpreter encounters its closing bracket (}) or when a break statement is found. A break statement is used to exist switch statements and other program control statements such as while, do...while, for, and for...in looping statements.

    Example: Tutorial4Ex1.html


HOME        TOP

Section B: Repetition

while Statements

while (conditional expression) {
    statement(s);
}

The while statement is used for repeating a statement or series of statements as long as a given condition expression evaluates to true.

    Example: Tutorial4Ex1.html


HOME        TOP

do...while Statements

do {
    statement(s);
}while (conditional expression)

 The do...while statement executes a statement or statements once, then repeats the execution as long as a given condition expression evaluates to true.

    Example: Tutorial4Ex1.html


HOME        TOP

for Statements

for (initialization expression; condition; update statement) {
    statement(s);
}

 The for statement is used for repeating a statement or series of statements as long as a given condition expression evaluates to true. You can include code in the for statement constructor to initialize a counter and change its value with each iteration.
When the JavaScript interpreter encounters a for loop, the following steps occur:
    1. The initialization expression is started.
    2. The for loop condition is evaluated.
    3. If the condition in Step 2 returns a value of true, then the for loop statements execute, Step 4 occurs, and the process starts over again with Step 2. If the condition in step 2 returns a value of false, then the for statement ends and the next statement following the for statement executes.
    4. The update statement in the for statement constructor is executed.

    Example: Tutorial4Ex1.html


HOME        TOP

for...in Statements

for (variable in object) {
    statement(s);
}

The for...in statement is a looping statement that executes the same statement or command block for all the properties within an object. The variable name in the for...in statement constructor holds an individual object property. The object name in the constructor represents the name of an object that has been instantiated in a program. The for...in statement automatically assigns each property in an object to the variable name, performs the necessary statements on the property, then moves to the next property and starts over. The for...in statement ends automatically once it reaches the last property in an object.
There is no set order or way to control how the properties in an object are assigned to the for...in statement variable.

    Example: Tutorial4Ex1.html


HOME        TOP

with Statements

with (object) {
    statement(s);
}

The with statement eliminates the need to retype the name of an object when properties of the same object are being referenced in a series.

    Example: Tutorial4Ex1.html


HOME        TOP

continue Statements

The continue statement halts a looping statement and restarts the loop with a new iteration. You use the continue statement when you want to stop the loop for the current iteration, but want the loop to continue with a new iteration.

    Example: Tutorial4Ex1.html


HOME        TOP

break Statements

A break statement is used to exist switch statements and other program control statements such as while, do...while, for, and for...in looping statements.

    Example: Tutorial4Ex1.html


HOME        TOP

 

 

Hosted by www.Geocities.ws

1