我的 if 语句有什么问题? [英] What is wrong with my if-statement?
问题描述
我现在正在尝试探索 pascal.我遇到了一些编译器错误.我写了一个 if else if 语句,如下所示:
I am now trying to explore pascal. And I ran into some compiler errors. I wrote a if else if statement like this:
if ((input = 'y') or (input = 'Y')) then
begin
writeln ('blah blah');
end;
else if ((input = 'n') or (input = 'N')) then
begin
writeln ('blah');
end;
else
begin
writeln ('Input invalid!');
end;
它在第一个 else
处给了我一个错误:
And it gives me an error at the first else
:
";"预期但找到ELSE"
";" expected but "ELSE" found
我找了很多关于 if 语句的教程,他们就像我一样:
I looked for a lot of tutorials about if statements and they just do it like me:
if(boolean_expression 1)then
S1 (* Executes when the boolean expression 1 is true *)
else if( boolean_expression 2) then
S2 (* Executes when the boolean expression 2 is true *)
else if( boolean_expression 3) then
S3 (* Executes when the boolean expression 3 is true *)
else
S4; ( * executes when the none of the above condition is true *)
我试图删除 begin
和 end
但发生了同样的错误.这是编译器错误吗?
I tried to delete the begin
and end
but the same error occured. Is this a compiler bug?
附言我在案例陈述中这样做.但我认为这不重要.
P.S. I am doing this in a case statement. But I don't think it matters.
推荐答案
;
在大多数情况下不允许在 else
之前.
;
is not allowed before else
in the majority of cases.
if ((input = 'y') or (input = 'Y')) then
begin
writeln ('blah blah');
end
else if ((input = 'n') or (input = 'N')) then
begin
writeln ('blah');
end
else
begin
writeln ('Input invalid!');
end;
会编译.但是... 更喜欢使用 begin
... end
括号以避免误解复杂的 if then else
语句中的代码.这样的事情会更好:
will compile.
But... Prefer using begin
... end
brackets to avoid misunderstanding of code in complicated if then else
statements.
something like this will be better:
if ((input = 'y') or (input = 'Y')) then
begin
writeln('blah blah');
end
else
begin
if ((input = 'n') or (input = 'N')) then
begin
writeln('blah');
end
else
begin
writeln('Input invalid!');
end;
end;
第二个示例更容易阅读和理解,不是吗?
The second sample is much easier to read and understand, isn't it?
当您删除begin
和end
时,代码不起作用,因为else
之前有一个分号.这将编译没有错误:
The code does not work when you remove begin
and end
because there is a semicolon before else
. This will compile without errors:
if ((input = 'y') or (input = 'Y')) then
writeln('blah blah')
else
begin
end;
附加@lurker的评论
请看下面没有begin
... end
括号的例子.
Please, see the following example without begin
... end
brackets.
if expr1 then
DoSmth1
else if expr2 then
if expr3 then
DoSmth2
else
DoSmth3;//Under what conditions is it called?
如果在not (expr2)
或 (expr2) and (not (expr3))
上调用了 DoSmth3
,这里看的不是很清楚>.虽然我们可以在这个示例中预测编译器的行为,但是没有 begin
... end
的更复杂的代码变得容易出错并且难以阅读.看下面的代码:
It is not clearly seen here, if DoSmth3
is called on not (expr2)
or (expr2) and (not (expr3))
. Though we can predict the compiler behaviour in this sample, the more complicated code without begin
... end
becomes subect to mistakes and is difficult to read. See the following code:
//behaviour 1
if expr1 then
DoSmth
else if expr2 then
begin
if expr3 then
DoSmth
end
else
DoSmth;
//behaviour 2
if expr1 then
DoSmth
else if expr2 then
begin
if expr3 then
DoSmth
else
DoSmth;
end;
现在代码行为很明显.
这篇关于我的 if 语句有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!