我的 if 语句有什么问题?

编程入门 行业动态 更新时间:2024-10-25 12:24:13
本文介绍了我的 if 语句有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我现在正在尝试探索 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 语句有什么问题?

本文发布于:2023-10-22 22:59:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1518986.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有什么   语句

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!