Why do we use "End If" statement?
Asked Answered
B

4

6

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".

Brood answered 24/1, 2019 at 18:34 Comment(2)
That's like asking why a FOR needs a matching NEXT :)Amadavat
Just to end the if block :)Geryon
D
7

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.

Dola answered 24/1, 2019 at 18:53 Comment(1)
Debugging a one-line IF statement can also be difficult. A multi-line IF statement with separate lines for everything makes this much easier since the error is reported on the line containing only the problematic code, not on the same line as the IF, ELSE IF, ELSE, and actions as would occur with a one-line IF.Butterscotch
A
3

Multiple indented if/then/else statements can be combined into a structure easier than a single line statement, such as:

INPUT X
IF X > 10 THEN
    PRINT "X is greater than 10."
ELSE
    IF X < 10 THEN
        PRINT "X is less than 10."
    ELSE
        PRINT "X is equal to 10."
    END IF
END IF

Which is the same as:

INPUT X
IF X > 10 THEN PRINT "X is greater than 10." ELSE IF X < 10 THEN PRINT "X is less than 10." ELSE PRINT "X is equal to 10."

What would be extremely difficult is testing the value of 3 numbers in a single line if/then, such as:

INPUT X, Y, Z
IF X = 0 AND Y = 0 AND Z = 0 THEN PRINT "All zero." ELSE IF X = 0 AND Y <> 0 AND Z = 0 THEN PRINT "X and Z zero." ELSE IF X <> 0 AND Y = 0 AND Z = 0 THEN PRINT "Y and Z zero." ELSE IF X = 0 AND Y = 0 AND Z <> 0 THEN PRINT "X and Y zero." ELSE IF X <> 0 AND Y <> 0 AND Z = 0 THEN PRINT "X and Y non-zero." ELSE IF X <> 0 AND Y = 0 AND Z <> 0 THEN PRINT "X and Z non-zero." ELSE IF X = 0 AND Y <> 0 AND Z <> 0 THEN PRINT "Y and Z non-zero." ELSE PRINT "All non-zero."
Amadavat answered 26/1, 2019 at 4:9 Comment(0)
A
1

Another example of determining 3 input values:

COLOR 15
DO
    PRINT "Enter values(y/n)";: INPUT x$
    IF LCASE$(x$) = "n" THEN END
    PRINT "Enter X,Y,Z";: INPUT X, Y, Z
    GOSUB Calculate
LOOP
END
Calculate:
SELECT CASE X
    CASE IS > 0
        SELECT CASE Y
            CASE IS > 0
                SELECT CASE Z
                    CASE IS > 0
                        PRINT "X is positive, Y is positive, Z is positive."
                    CASE IS < 0
                        PRINT "X is positive, Y is positive, Z is negative."
                    CASE ELSE
                        PRINT "X is positive, Y is positive, Z is zero."
                END SELECT
            CASE IS < 0
                SELECT CASE Z
                    CASE IS > 0
                        PRINT "X is positive, Y is negative, Z is positive."
                    CASE IS < 0
                        PRINT "X is positive, Y is negative, Z is negative."
                    CASE ELSE
                        PRINT "X is positive, Y is negative, Z is zero."
                END SELECT
            CASE ELSE
                SELECT CASE Z
                    CASE IS > 0
                        PRINT "X is positive, Y is zero, Z is positive."
                    CASE IS < 0
                        PRINT "X is positive, Y is zero, Z is negative."
                    CASE ELSE
                        PRINT "X is positive, Y is zero, Z is zero."
                END SELECT
        END SELECT
    CASE IS < 0
        SELECT CASE Y
            CASE IS > 0
                SELECT CASE Z
                    CASE IS > 0
                        PRINT "X is negative, Y is positive, Z is positive."
                    CASE IS < 0
                        PRINT "X is negative, Y is positive, Z is negative."
                    CASE ELSE
                        PRINT "X is negative, Y is positive, Z is zero."
                END SELECT
            CASE IS < 0
                SELECT CASE Z
                    CASE IS > 0
                        PRINT "X is negative, Y is negative, Z is positive."
                    CASE IS < 0
                        PRINT "X is negative, Y is negative, Z is negative."
                    CASE ELSE
                        PRINT "X is negative, Y is negative, Z is zero."
                END SELECT
            CASE ELSE
                SELECT CASE Z
                    CASE IS > 0
                        PRINT "X is negative, Y is zero, Z is positive."
                    CASE IS < 0
                        PRINT "X is negative, Y is zero, Z is negative."
                    CASE ELSE
                        PRINT "X is negative, Y is zero, Z is zero."
                END SELECT
        END SELECT
    CASE ELSE
        SELECT CASE Y
            CASE IS > 0
                SELECT CASE Z
                    CASE IS > 0
                        PRINT "X is zero, Y is positive, Z is positive."
                    CASE IS < 0
                        PRINT "X is zero, Y is positive, Z is negative."
                     CASE ELSE
                        PRINT "X is zero, Y is positive, Z is zero."
                END SELECT
            CASE IS < 0
                SELECT CASE Z
                    CASE IS > 0
                        PRINT "X is zero, Y is negative, Z is positive."
                    CASE IS < 0
                        PRINT "X is zero, Y is negative, Z is negative."
                    CASE ELSE
                        PRINT "X is zero, Y is negative, Z is zero."
                 END SELECT
            CASE ELSE
                SELECT CASE Z
                     CASE IS > 0
                        PRINT "X is zero, Y is zero, Z is positive."
                    CASE IS < 0
                        PRINT "X is zero, Y is zero, Z is negative."
                    CASE ELSE
                         PRINT "X is zero, Y is zero, Z is zero."
                END SELECT
         END SELECT
END SELECT
RETURN
Amadavat answered 11/3, 2019 at 4:47 Comment(0)
A
0

An alternative is to use select case:

INPUT X
SELECT CASE X
    CASE IS > 10
        PRINT "X is greater than 10."
    CASE IS < 10
        PRINT "X is less than 10."
    CASE ELSE
        PRINT "X is equal to 10."
END SELECT
Amadavat answered 9/3, 2019 at 4:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.