Why do we use "End If" statement?

Bhavesh Sood picture Bhavesh Sood · Jan 24, 2019 · Viewed 7.1k times · Source

Why do we write END IF statement in this program? Without writing it, we can easily get our result. Is there any example through which you can explain me the use of END IF statement?

I have tried this:

INPUT X
IF X>10 THEN PRINT "X IS GREATER THAN 10" ELSE PRINT "X IS NOT GREATER THAN 10"
END

then I am also getting expected result.

The real code is:

INPUT X
IF X>10 THEN 
    PRINT "X IS GREATER THAN 10"
ELSE
    PRINT "X IS NOT GREATER THAN 10"
END IF
END

EXPECTED AND DESIRED
FOR EXAMPLE:
When X=5 Then Output will be "X IS NOT GREATER THAN 10".

Answer

Sardar Usama picture Sardar Usama · Jan 24, 2019

END IF is needed to indicate the ending of an IF, ELSE IF, ELSE structure written in multiple lines. If there is no END IF (the absence of which will lead to an error) then the statements under IF, ELSE IF, ELSE structure will be considered as a part of the IF, ELSE IF, ELSE structure until there comes an END IF. If there were no END IF, the use of IF, ELSE IF, ELSE structure would have been limited to be used only at the end of the program

Consider a situation where you would want to run 100 particular lines if the condition is true and 100 other lines if the condition is false and 100 more lines which must always execute after the IF-ELSE structure. Now obviously you cannot write all of these 200 IF ELSE related lines in a single line. And if there were no END IF then there would have been no way to run the next 100 lines.


END IF is invalid for the IF, ELSE IF, ELSE statements written in one line. Any statement in the next line will be considered out of the IF, ELSE IF, ELSE structure. It is not always possible to code all of the required functionality in one line. So, it can only be used when a small functionality that can be written in one line is to be triggered on the basis of some conditions. So this is the short coming of this one-liner approach.