The else if
statement
is used along with the
if
and else
statements.
It is used to execute
blocks of code conditionally
when there are more than
two conditions.
In the example given below:
-
The if
statement checks
if the value of number
is greater than 0
.
-
The else if
statement checks
if the value of number
is equal to 0
.
-
The else
code block executes
when none of the previous
conditions are true
.
Since the value of the number
variable
is 0
:
-
The if
condition results
in false
, so its code block
is not executed.
-
The else if
condition results
in true
, so its code block
gets executed.
Hence the output will be "The number is 0.".
If we change the value of the number
variable
to -1
:
-
The if
condition results
in false
, so its code block
is not executed.
-
The else if
condition results
in false
, so its code block
is not executed.
-
Since all the above conditions
resulted in false
,
the else
code block gets executed.
Hence the output will be "The number is negative.".
If we change the value of the
number
variable to 7
,
the if
condition results in true
,
so its code block gets executed.
Hence the output will be "The number is positive.".
Depending upon the number of conditions,
we can add as many else if
statements as needed.
They must be added between the if
and else
statements.