8) Conditional Statement

While writing code, when you want to perform different actions depending on conditions you will use a conditional statement in your code

Example:

If the condition is true the block will execute otherwise whole block will be ignored. If you have some alternative condition then we use else

You can chain condition also

If the first condition is true first is executed and if second is true second is executed but if both are true only first is executed.

Operator Meaning Operator Meaning
=== Identical !== Not Identical
< Less than > Greater than
<= Less than or equal >= Greater than or equal to

Note:

  • Always use a double equal sign for if statement using single equal will always evaluate to true as it assigns a value instead of comparing it.
  • Difference between equal and identical refers to data type. For example

Here both are equal, not identical.

Example of if/else if/else

Other syntax for if else

We can avoid braces and use the following syntax for if else condition also

Note: when using braces elseif can be broken into two words however here elseif is written as one word only.