HOME        BACK

Tutorial 4: Example 1

if Statements

<SCRIPT LANGUAGE="JavaScript">
<!-- HIDE FROM INCOMPATIBLE BROWSERS

 if (d != 5) {
    document.writeln("This is a command block. One or more statements in the brace brackets ({}). <BR>");
    document.writeln("d is not equal to 5. <BR>");
}
else {
    document.writeln("d is equal to 5. <BR>");
}

var e = 5;
if (e > 5) {
    document.writeln("d is greater than 5. <BR>");
}
else if (e < 5) {
    document.writeln("d is less than 5. <BR>");
}
else {
    document.writeln("d is equal to 5. <BR>");
}

var f = 5;
if (f > 5) {
    document.writeln("f is greater than 5. <BR>");
}
else {
    if (e < 5) {
        document.writeln("f is less than 5. <BR>");
    }
    else {
        document.writeln("f is equal to 5. <BR>");
    }
}

// STOP HIDING FROM INCOMPATIBLE BROWSERS -->
</SCRIPT>

Outputs:


switch Statements

<SCRIPT LANGUAGE="JavaScript">
<!-- HIDE FROM INCOMPATIBLE BROWSERS

a = 5;
switch (a) {
    case 1:
        document.writeln("a is equal to 1. <BR>");
        break;
    case "2":
        document.writeln("a is equal to \"2\". <BR>");
        break;
    case 3:
        document.writeln("a is equal to 3. <BR>");
        break;
    case "four":
        document.writeln("a is equal to \"four\". <BR>");
        break;
    case "5":
        document.writeln("a is equal to \"5\". <BR>");
        break;
    default:
        document.writeln("a is not equal to 1, \"2\", 3, \"four\", or \"5\". <BR>");
}

document.writeln("<BR>switch with no break. <BR>");
b = 2;
switch (b) {
    case 1:
        document.writeln("b is equal to 1. <BR>");
    case 2:
        document.writeln("b is equal to 2. <BR>");
    case 3:
        document.writeln("b is equal to 3. <BR>");
    case "four":
        document.writeln("b is equal to \"four\". <BR>");
    case "5":
        document.writeln("b is equal to \"5\". <BR>");
    default:
        document.writeln("b is not equal to 1, 2, 3, \"four\", or \"5\". <BR>");
}

// STOP HIDING FROM INCOMPATIBLE BROWSERS -->
</SCRIPT>

Outputs:


while Statements

<SCRIPT LANGUAGE="JavaScript">
<!-- HIDE FROM INCOMPATIBLE BROWSERS

var count = 1;
while (count <= 5) {
    document.writeln("count = " + count + "<BR>");
    count++;
}

// STOP HIDING FROM INCOMPATIBLE BROWSERS -->
</SCRIPT>

Outputs:


do...while Statements

<SCRIPT LANGUAGE="JavaScript">
<!-- HIDE FROM INCOMPATIBLE BROWSERS

var count = 5;
do {
    document.writeln("count = " + count + "<BR>");
    count--;
} while (count > 0);

// STOP HIDING FROM INCOMPATIBLE BROWSERS -->
</SCRIPT>

Outputs:


for Statements

<SCRIPT LANGUAGE="JavaScript">
<!-- HIDE FROM INCOMPATIBLE BROWSERS

var fastfood = new Array("pizza", "burger", "french fries", "tacos", "fried chicken");
for (var count = 0); count < fastfood.length; count++) {
    document.writeln("fastfood[" + count + "] = " + fastfood[count] + "<BR>");
}

// STOP HIDING FROM INCOMPATIBLE BROWSERS -->
</SCRIPT>

Outputs:


for...in Statements

<SCRIPT LANGUAGE="JavaScript">
<!-- HIDE FROM INCOMPATIBLE BROWSERS

document.writeln ("<BR>for...in statement with function object. <BR>");
function Animal (type, sound, transport_mode) {
    this.animal_type = type; // object property
    this.animal_sound = sound; // object property
    this.animal_transport_mode = transport_mode; // object property
}

livestock = new Animal("cow", "move", "walk"); // instantiate object

for (prop in livestock) { // this for loop prints
    document.writeln ("prop = " + prop + "<BR>");
    document.writeln ("livestock[" + prop + "] = " + livestock[prop] + "<BR>");
}

document.writeln ("<BR>Can not reference function property by integer index: <BR>");
document.writeln ("livestock[0] = " + livestock[0] + "<BR>");

document.writeln ("<BR>for...in statement with Array object. <BR>");
var fastfood = new Array("pizza", "burger", "french fries", "tacos", "fried chicken");
for (count in fastfood) {
    document.writeln("fastfood[" + count + "] = " + fastfood[count] + "<BR>");
}

// STOP HIDING FROM INCOMPATIBLE BROWSERS -->
</SCRIPT>

Outputs:


with Statements

<SCRIPT LANGUAGE="JavaScript">
<!-- HIDE FROM INCOMPATIBLE BROWSERS

function Animal (type, sound, transport_mode) {
    this.animal_type = type; // object property
    this.animal_sound = sound; // object property
    this.animal_transport_mode = transport_mode; // object property
}

livestock = new Animal(); // instantiate object

with (livestock) {
    animal_type = "wolf";
    animal_sound = "growl";
    animal_transport_mode = "run";
}

for (prop in livestock) { // this for loop prints
    document.writeln ("prop = " + prop + "<BR>");
    document.writeln ("livestock[" + prop + "] = " + livestock[prop] + "<BR>");
}

// STOP HIDING FROM INCOMPATIBLE BROWSERS -->
</SCRIPT>

Outputs:


continue Statements

<SCRIPT LANGUAGE="JavaScript">
<!-- HIDE FROM INCOMPATIBLE BROWSERS

for (var count = 0; count <= 5; count++) {
    if (count == 3)
        continue;
    document.writeln("count = " + count + "<BR>");
}

// STOP HIDING FROM INCOMPATIBLE BROWSERS -->
</SCRIPT>

Outputs:


break Statements

<SCRIPT LANGUAGE="JavaScript">
<!-- HIDE FROM INCOMPATIBLE BROWSERS

for (var count = 0; count <= 5; count++) {
    if (count == 3)
        break;
    document.writeln("count = " + count + "<BR>");
}

// STOP HIDING FROM INCOMPATIBLE BROWSERS -->
</SCRIPT>

Outputs:


HOME        BACK
Hosted by www.Geocities.ws

1