'If' statement and the colon

杀马特。学长 韩版系。学妹 提交于 2019-11-27 03:56:33

问题


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?

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.

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.


回答1:


I'm assuming you mean VB.Net.

According to the grammar in the VB Language spec, which you can read here:

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

The result should be "6".

This is because the grammar for a "line if statement" is:

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.

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...




回答2:


I haven't used BASIC that extensively in a while, so this is just a guess, but I think that fred is 6.

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:

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.

It is best to try to adhere to coding styles which make the intention of the code more obvious and less ambigious.




回答3:


The final result is 6.
Now the real question is: How did you get into my repo?
:-)




回答4:


Just a guess

fred = 6 because you can have multiple statements on the same line separated by a colon.

Not sure if the "else" is legal (i.e. compilable)

IMHO a better coding style should be chosen:


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



回答5:


In really old BASIC dialects, the only thing that could follow a "THEN" was a line number. 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. Further dialects added the ability to skip until either end-of-line or "ELSE", whichever came first; attempting to execute an "ELSE" statement would skip to end-of-line.

When QuickBasic was introduced, it added support for multi-line if/then/else blocks, but kept support for the old-style approach. Visual Basic followed suit, and vb.net continues the tradition.



来源:https://stackoverflow.com/questions/367325/if-statement-and-the-colon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!