Udostępnij za pośrednictwem


Conditional Statement Use

JScript supports if and if...else conditional statements. An if statement tests a condition. It executes the relevant JScript code if the condition evaluates as true. An if...else statement tests a condition and executes one of two blocks of code depending on the result of the condition statement. The simplest form of an if statement can be written on one line, but multiline if and if...else statements are more common.

Conditional Statement Examples

The following examples demonstrate syntaxes you can use with if and if...else statements. The first example shows the simplest kind of Boolean test. If (and only if) the item between the parentheses evaluates to (or can be coerced to) true, the statement or block of statements after the if is executed.

In the following example, the registerUser function is called if the value of newUser converts to true.

if (newUser)
   registerUser();

In this example, the test fails unless both conditions are true.

if (rind.color == "deep yellow " && rind.texture == "wrinkled") {
   theResponse = ("Is it a Crenshaw melon?");
}

In this example, the code in the body of the do...while loop is executed until the variable quit is true.

var quit;
do {
   // ...
   quit = getResponse()
}
while (!quit)

See Also

Reference

if...else Statement

Other Resources

JScript Conditional Structures

JScript Reference