'If' statement and the colon
Solution 1
I'm assuming you mean VB.Net.
According to the grammar in the VB Language spec, which you can read here:
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...
Solution 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.
Solution 3
The final result is 6.
Now the real question is: How did you get into my repo?
:-)
BlackMael
Lead Developer working for a Financial Company in New Zealand. I work across many teams within the company and develop line of business apps, public facing web sites, SSIS based processes, CI/CD pipelines, DevOps set-up, Azure DevOps.
Updated on June 04, 2022Comments
-
BlackMael almost 2 years
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.
-
Peter Mortensen over 10 years"debuging" -> "debugging. "VB.Net" -> "VB.NET" (ref. en.wikipedia.org/wiki/Visual_Basic_.NET).
-
Patrick Szalapski almost 9 yearsTo clarify, the colon operator simply lets you combine multiple statements on one line. It is the same as executing the statements on successive lines.