Conditional and logical operators

Operator Type Description
> Conditional Evaluates whether the value on the left is greater than the value on the right
< Conditional Evaluates whether the value on the right is grater than the value on the left
>=, <= Conditional Evaluates the same as > or < bur with the additional logic that the values can also be equal
!= Conditional Evaluates whether the values aren't equal
== Conditional Evaluates whether the values are qual independent of the underlying data type
=== Conditional Evaluates whether the values are equal both in value and underlying data type
&& Logical The AND logical operator, in which the expressions on both sides must evaluate to true
|| Logical The OR logical operator, in which at least one expression on either side must evaluate to true

We will use logical operators to combine conditional operators.

Example:

if(age >= 12)
{
    if(age <= 65)
    {
        // logic goes here
    }
}

In the above example first age >= 12 will be evaluated, if it is true then only age <= 65 is evaluated. So, we can combine these two statements into one statement by using Logical operators.

if(age >= 12 && age <= 65)
{
    // logic goes here
}
  • Updated
    Jan 07, 2015
  • Views
    1,940