Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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.

The final result is 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.

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top