contents   index   previous   next



Logical operators and conditional expressions

 

Logical operators compare two values and evaluate whether the resulting expression is false or true. The value false is zero, and true is not false, that is, anything not zero. A variable or any other expression may be false or true, that is, zero or non-zero. An expression that does a comparison is called a conditional expression.

 

Many values are evaluated as true, in fact, everything except 0. It is often safer to make comparisons based on false, which is only one value, rather than to true, which can be many. Expressions can be combined with logic operators to make complex true/false decisions.

 

Logical operators are used to make decisions about which statements in a script will be executed, based on how a conditional expression evaluates. As an example, suppose that you are designing a simple guessing game. The computer thinks of a number between 1 and 100, and you guess what it is. The computer tells you if you are right or not and whether your guess is higher or lower than the target number. This procedure uses the if statement, which is introduced in the next section. Basically, if the conditional expression in the parenthesis following an if statement is true, the statement block following the if statement is executed. If false, the statement block is ignored, and the computer continues executing the script at the next statement after the ignored block. The script might have a structure similar to the one below in which GetTheGuess() is a function that gets your guess.

 

var guess = GetTheGuess(); //get the user input

if (guess > target_number)

{

   ...guess is too high...

}

 

if (guess < target_number)

{

   ...guess is too low...

}

 

if (guess == target_number)

{

   ...you guessed the number!...

}

 

This example is simple, but it illustrates how logical operators can be used to make decisions in ScriptEase.

 

The logical operators are:

 

!

not

reverses an expression. If (a+b) is true, then !(a+b) is false.

&&

and

true if, and only if, both expressions are true. Since both expressions must be true for the statement as a whole to be true, if the first expression is false, there is no need to evaluate the second expression, since the whole expression is false.

||

or

true if either expression is true. Since only one of the expressions in the or statement needs to be true for the expression to evaluate as true, if the first expression evaluates as true, the interpreter returns true and does not bother with evaluating the second.

==

equality

true if the values are equal, else false. Do not confuse the equality operator, ==, with the assignment operator, =.

!=

inequality

true if the values are not equal, else false.

===

identity

true if the values are identical or strictly equal, else false. No type conversions are performed as with the equality operator.

!==

non-identity

true if the values are not identical or not strictly equal, else false. No type conversions are performed as with the inequality operator.

<

less than

a < b is true if a is less than b.

>

greater than

a > b is true if a is greater than b.

<=

less than or equal to

a <= b is true if a is less than or equal to b.

>=

greater than or equal to

a >= b is true if a is greater than b.

 

Remember, the assignment operator, =, is different than the equality operator, ==. If you use one equal sign when you intend two, your script will not function the way you want it to. This is a common pitfall, even among experienced programmers. The two meanings of equal signs must be kept separate, since there are times when you have to use them both in the same statement, and there is no way the computer can differentiate them by context.

 


Concatenation operator