Greater Than or Equal

advertisement
Conditional Operators
Introduction
Conditional statements allow you to control the flow of execution of a script or one of its
sections. To do this, you use some keywords and associate them with expressions to check.
Depending on the outcome of the checking process and other comparison operations, you can
take appropriate actions.
Most of the conditional statements perform comparisons and act depending on the outcome of
such comparisons. To assist you in making such comparisons, the JavaScript language is
equipped with special operators that can act on natural numbers, decimal numbers, or strings.
Equality ==
We previously used the assignment operator = to give a value to a variable. Although the
assignment operator works on two operands, one on the left and another on the right of the
operator, it doesn't mean that both operands are equal. It is only used to give a new value,
the right value, to the left variable. Because of that, the left operand must never be a numeric
value, although both operands can be variables.
If you want to find out whether two variables hold the same value, you should use the
JavaScript equality operator. This is performed with == and its syntax is:
Variable1 == Variable2
To perform this operation, the browser (actually the interpreter) compares the operands on
both sides of the operator. If both operands hold the same value, the comparison renders a
value of true. Otherwise, the comparison renders false.
Here is an example:
<script language="JavaScript">
document.write("Comparison of 15 == 32 produces ", 15 == 32);
</script>
The Logical Not Operator !
When a variable is not being used or is not available for processing, to make a variable
(temporarily) unusable, you can nullify its value. To render a variable unavailable during the
evolution of a program, apply the logical not operator which is !. Its syntax is:
!Value
There are two main ways you can use the logical not operator. As we will learn when studying
conditional statements, the most classic way of using the logical not operator is to check the
state of a variable.
To nullify a variable, you can write the exclamation point to its left. When used like that, you
can display its value using the cout extractor. You can even assign it to another variable.
When a variable holds a value, it is "alive". To make it not available, you can "not" it. When a
variable has been "notted", its logical value has changed. If the logical value was true, which is
1, it would be changed to false, which is 0. Therefore, you can inverse the logical value of a
variable by "notting" or not "notting" it.
Inequality !=
To compare two variables in order to find out whether they are different, you can use the
inequality operator != whose syntax is:
Variable1 != Variable2
When performing this operation, the interpreter compares the values of Variable1 and
Variable2. If their values are different, which means that they are not equal, the comparison
results in a true value (very important to understand). If they are equal, the result of the
comparison is false (observe the contrast with the equality operator ==).
Here is an example:
<script language="JavaScript">
document.write("Comparison of 15 != 32 produces ", 15 != 32);
</script>
Less Than <
If you want to find out whether one value is less than another, use the "less than" operator <.
Its syntax is:
Variable1 < Variable2
The interpreter compares the values held by Variable1 and Variable2. If the value held by
Variable1 is less than that of Variable2, the comparison would produce a true value.
Otherwise, the result is rendered false.
Here is an example:
<script language="JavaScript">
document.write("Comparison of 15 < 32 produces ", 15 < 32);
</script>
Less Than Or Equal <=
The previous two operations can be combined to compare two values. This allows you to know
if two values are the same or if the first is less than the second. The operator used is <= and
its syntax is:
Value1 <= Value2
The <= operation performs a comparison as any of the last two. If both Value1 and Value2
hold the same value, the result is true or positive. If the left operand, in this case Value1,
holds a value lower than the second operand, in this case Value2, the result is still true.
Here is an example:
<script language="JavaScript">
document.write("Comparison of 15 <= 32 produces ", 15 <= 32);
</script>
Greater Than >
When two variables of the same type are distinct, one of them is usually higher than the
other. You can use a logical operator that allows you to find out if one of two values is greater
than the other. The operator used for this operation uses the > symbol. Its syntax is:
Value1 > Value2
Both operands, in this case Value1 and Value2, can be variables or the left operand can be a
variable while the right operand is a constant. If the value on the left of the > operator is
greater than the value on the right side or a constant, the comparison produces a true or
positive value . Otherwise, the comparison renders false or null.
Greater Than or Equal >=
The greater than or the equality operators can be combined to produce an operator as follows:
>=. This is the "greater than or equal to" operator. Its syntax is:
Value1 >= Value2
A comparison is performed on both operands: Value1 and Value2. If the value of Value1 and
that of Value2 are the same, the comparison produces true. If the value of the left operand is
greater than that of the right operand, the comparison produces true also. If the value of the
left operand is strictly less than the value of the right operand, the comparison produces a
false or null result.
Here is an example:
<script language="JavaScript">
document.write("Comparison of 15 >= 32 produces ", 15 >= 32);
</script>
Here is a summary table of the logical operators we have studied:
Operator
Meaning
Example
Opposite
==
Equality to
a == b
!=
!=
Not equal to
12 != 7
==
<
Less than
25 < 84
>=
<=
Less than or equal to Cab <= Tab
>
>
Greater than
248 > 55
<=
Greater than or equal
>=
Val1 >= Val2
<
to
Download