为什么我们在这个程序中写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 IF
,IF, 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