Sun Chili!Soft ASP Sun Chili!Soft
ASP Sun Microsystems

 

JScript if. . . else Statement

Conditionally executes a group of statements, depending on the value of an expression.

Syntax: JScript if. . . else Statement

if (condition)

statement1

[else

statement2]

Arguments: JScript if. . . else Statement

condition

A Boolean expression. If condition is null or undefined, condition is treated as false.

statement1

The statement to be executed if condition is True. Can be a compound statement.

statement2

The statement to be executed if condition is False. Can be a compound statement.

Remarks: JScript if. . . else Statement

It is generally good practice to enclose statement1 and statement2 in braces ({}) for clarity and to avoid inadvertent errors. In the following example, you may intend that the else be used with the first if statement, but it is used with the second one.

if (x == 5)

if (y == 6)

z = 17;

else

z = 20;

Changing the code in the following manner eliminates any ambiguities:

if (x == 5)

{

if (y == 6)

z = 17;

}

else

z = 20;

Similarly, if you want to add a statement to statement1, and you don't use braces, you can accidentally create an error:

if (x == 5)

z = 7;

q = 42;

else

z = 19;

In this case, there is a syntax error, as there is more than one statement between the if and else statements. Putting braces around the statements between the if and else is required.

Copyright 2002 Sun Microsystems, Inc. All rights reserved. Legal Notice.