If we have Multiple IF...ELSE IF
statements but we also want also want to execute some piece of code if none of expressions are evaluated to True , then we can simple add a final ELSE
block which only gets executed if none of the IF
or ELSE IF
expressions are evaluated to true.
In the example below none of the IF
or ELSE IF
expression are True hence only ELSE
block is executed and prints 'No other expression is true'
IF ( 1 = 1 + 1 )
BEGIN
PRINT 'First If Condition'
END
ELSE IF (1 = 2)
BEGIN
PRINT 'Second If Else Block'
END
ELSE IF (1 = 3)
BEGIN
PRINT 'Third If Else Block'
END
ELSE
BEGIN
PRINT 'No other expression is true' --<-- Only this statement will be printed
END