简体   繁体   English

'如果'声明和冒号

[英]'If' statement and the colon

Here is an interesting piece of code that my fellow team members were just having a slightly heated discussion about... 这是一段有趣的代码,我的团队成员正在就......进行一次稍微热烈的讨论。

  Dim fred As Integer

  If True Then fred = 5 : fred = 3 : fred = 6 Else fred = 4 : fred = 2 : fred = 1

After executing the above code snippet, what is the value of fred ? 执行上面的代码片段后, fred的值是多少?

Try not to cheat and debug the code. 尽量不要欺骗和调试代码。

This is a highly contrived code example that started out as an example of using the colon with an If statement, but then someone decided to take it upon themselves to proffer a result for fred . 这是一个非常人为的代码示例,最初是作为使用带有If语句的冒号的示例,但后来有人决定自己为fred提供结果。

UPDATE : I would not normally write code like this and this snippet only serves as an example. 更新 :我通常不会编写这样的代码,这个代码片段仅作为示例。 As it so happens, this question originated from a discussion involving the creation of a coding standards document for our team. 碰巧的是,这个问题起源于一个涉及为我们团队创建编码标准文档的讨论。

I'm assuming you mean VB.Net. 我假设你的意思是VB.Net。

According to the grammar in the VB Language spec, which you can read here: 根据VB语言规范中的语法,您可以在这里阅读:

http://www.microsoft.com/Downloads/thankyou.aspx?familyId=39de1dd0-f775-40bf-a191-09f5a95ef500&displayLang=en http://www.microsoft.com/Downloads/thankyou.aspx?familyId=39de1dd0-f775-40bf-a191-09f5a95ef500&displayLang=en

The result should be "6". 结果应为“6”。

This is because the grammar for a "line if statement" is: 这是因为“行if语句”的语法是:

If  BooleanExpression  Then  Statements  [  Else  Statements  ]  StatementTerminator

and "statements" is defined to be 并且“陈述”被定义为

Statements  ::=
[  Statement  ]  |
Statements  :  [  Statement  ]

Edit: I would like to note that debugging the code is not "cheating". 编辑:我想注意调试代码不是“作弊”。

I used to work on the VB compiler team at Microsoft. 我曾经在微软的VB编译团队工作。

There were times where the spec was ambiguous, or didn't match what we had actually shipped. 有时候规范是模棱两可的,或者与我们实际发货时的情况不符。 In several of those cases the solution (what we did to fix it) was always based on "well... what does the compiler do now". 在其中一些案例中,解决方案(我们修复它的方法)始终基于“嗯......编译器现在做了什么”。

Sometimes we would change the compiler, sometimes we would change the spec. 有时我们会更改编译器,有时我们会更改规范。

However,we would always run the compiler to see what it actually did before we made a decision. 但是,在做出决定之前,我们总是运行编译器来查看它实际上做了什么。

So... debugging the code is a big part of figuring out what it does... 所以...调试代码是弄清楚它的作用的重要部分......

I haven't used BASIC that extensively in a while, so this is just a guess, but I think that fred is 6 . 我有一段时间没有广泛使用BASIC,所以这只是猜测,但我认为fred6

Frankly, the code is not very readable. 坦率地说,代码不是很易读。 I feel that by not having everything in one line and using indentation the code would be more readable: 我觉得通过不在一行中使用缩进并使用缩进代码将更具可读性:

Dim fred As Integer

If True Then
    fred = 5
    fred = 3
    fred = 6
Else
    fred = 4
    fred = 2
    fred = 1
End If

I believe that is equivalent code, if I am not mistaken. 我相信这是等效的代码,如果我没有记错的话。

But, if the code is not equivalent, that brings up another point: The original code is "tricky" in a way that what it seems to be saying is not quite what is really happening. 但是,如果代码不相同,那就会提出另一个观点:原始代码是“棘手的”,它看起来似乎并不是真正发生的事情。 Similar to the trap in C-style languages: 与C风格语言中的陷阱类似:

if (condition)
    do_something();
    do_other_thing();

The code seems to say imply that do_something and do_other_thing is executed when the condition is true, but in reality, do_other_thing is always executed. 代码似乎暗示当condition为真时执行do_somethingdo_other_thing ,但实际上, do_other_thing总是被执行。

It is best to try to adhere to coding styles which make the intention of the code more obvious and less ambigious. 最好是尝试遵循编码样式,使代码的意图更明显,更少暧昧。

The final result is 6. 最终结果是6。
Now the real question is: How did you get into my repo? 现在真正的问题是:你是如何进入我的回购的?
:-) :-)

Just a guess 只是一个猜测

fred = 6 because you can have multiple statements on the same line separated by a colon. fred = 6因为你可以在由冒号分隔的同一行上有多个语句。

Not sure if the "else" is legal (ie compilable) 不确定“其他”是否合法(即可编辑)

IMHO a better coding style should be chosen: 恕我直言,应选择更好的编码风格:


if (condition) then
  statement
  statement
else
  statement
  statement
end if

In really old BASIC dialects, the only thing that could follow a "THEN" was a line number. 在真正古老的BASIC方言中,唯一可以遵循“那么”的是行号。 Many dialects improved upon this by allowing code to follow the "THEN"; 许多方言通过允许代码遵循“那样”来改进; after parsing past the "THEN" they would skip to the next line if the indicated condition was false, or else continue with the present line. 在解析过“THEN”之后,如果指示的条件为假,它们将跳到下一行,或者继续当前行。 Further dialects added the ability to skip until either end-of-line or "ELSE", whichever came first; 进一步的方言增加了跳到行尾或“ELSE”的能力,以先到者为准; attempting to execute an "ELSE" statement would skip to end-of-line. 尝试执行“ELSE”语句将跳到行尾。

When QuickBasic was introduced, it added support for multi-line if/then/else blocks, but kept support for the old-style approach. 引入QuickBasic时,它增加了对多行if / then / else块的支持,但仍然支持旧式方法。 Visual Basic followed suit, and vb.net continues the tradition. Visual Basic紧随其后,vb.net延续了传统。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM