The relational operators are for comparisons. Please note that the single equal sign (=) defines the contents of a variable. For example, the statement "x = 1" populates the variable x with the number 1. The statement "x = 'elephant' " puts an elephant (or at least the string 'elephant') into the variable x. So a single equal sign does not perform a comparison, it is used for setting a variable's value. Instead, to perform a comparison we use the double equal sign (==).
So the relational operators are:
<, <=, >, >=, !=, ==
These are 'less than', 'less than or equal to', 'greater than', 'greater than or equal to', not equal to', (more about that one in a moment), and 'equal to', respectively. Look again at 'not equal to' (!=) — the exclamation point means "not," so technically, '!>' would mean "not greater than'. The ! is useful in the following multiple-condition scenarios.
What if a particular action needs to be taken only if multiple conditions are true? You can use logic operators to handle multiple conditions. There are three logic operators: &&, || and !.
'&&' is logical 'and' — && combines two Boolean values and returns a Boolean which is true if and only if both of its operands are true. For instance:
b = 3 > 2 && 5 < 7; // b is true
b = 2 > 3 && 5 < 7; // b is now false
'||' is logical 'or' — || combines two Boolean variables or expressions and returns a result that is true if either one or both of its operands are true. For instance:
b = 3 > 2 || 5 < 7; // b is true
b = 2 > 3 || 5 < 7; // b is still true
b = 2 > 3 || 5 > 7; // now b is false
The last logic operator is ! which means 'not' (see above). It reverses the value of a Boolean expression. Thus if b is true, !b is false. If b is false, !b is true.
b = !(3 > 2); // b is false
b = !(2 > 3); // b is true
These operators allow you to test multiple conditions with ease. For example, we can write an if tag as follows:
A real-world example might use the global time stamp to define an "after business hours" action.
In the above example, '< 7' means 'less than (earlier than) 7:00 AM' and '> 18' means 'greater than (later than) 6:00 PM.'