我的if语句有什么问题? [英] What is wrong with my if-statement?

查看:173
本文介绍了我的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发现

";" 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 *)

我试图删除开始结束但发生了同样的错误。这是编译器错误吗?

I tried to delete the begin and end but the same error occured. Is this a compiler bug?

P.S。我在一个案例陈述中这样做。但我认为这不重要。

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;

将编译。
但是...首选使用开始 ... 结束括号以避免误解复杂的代码 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?

当你删除开始结束时,代码不起作用,因为有 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

Appended on comment of @lurker

请参阅以下示例,不包含开始 ... 结束括号。

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?

如果 DoSmth3 在而不是(expr2)(expr2)和(不是(expr3))上调用。虽然我们可以预测此示例中的编译器行为,但是没有 begin 的更复杂的代码... end 变为subect to错误,很难读。请参阅以下代码:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆