我们为什么要使用"End If"语句?



为什么我们在这个程序中写END IF语句?无需编写它,我们可以轻松获得结果。有什么例子可以解释我END IF语句的使用吗?

我试过这个:

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

那么我也得到了预期的结果。

真正的代码是:

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

预期和期望
例如:
X=5则输出将为"X 不大于10"。

需要END IF来指示用多行编写的IF, ELSE IF, ELSE结构的结尾。如果没有END IF(没有将导致错误),则IF, ELSE IF, ELSE结构下的语句将被视为IF, ELSE IF, ELSE结构的一部分,直到出现END IF。如果没有END IFIF, ELSE IF, ELSE结构的使用将被限制为仅在程序结束时使用

考虑这样一种情况:如果条件为真,则要运行 100 行特定行,如果条件为假,则要运行 100 行其他行,并且必须始终在IF-ELSE结构之后执行 100 多行。 现在显然你不能把这200IF ELSE相关的行都写在一行里。如果没有END IF那么就没有办法运行接下来的 100 行。


END IF对于一行中编写的IF, ELSE IF, ELSE语句无效。下一行中的任何语句都将被视为超出IF, ELSE IF, ELSE结构。并非总是可以在一行中对所有必需的功能进行编码。所以,只有当一个可以写成一行的小功能要根据某些条件触发时,才能使用它。所以这是这种单行方法的缺点。

多个缩进的if/then/else语句可以组合成一个比单行语句更容易的结构,例如:

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

这与

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

非常困难的是测试单行中 3 个数字的值,如果/那么,例如:

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

确定 3 个输入值的另一个示例:

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

另一种方法是使用选择大小写:

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

相关内容

  • 没有找到相关文章

最新更新