Alex Rivera | Logout

'If' statement and the colon

Asked 2008-12-15T02:15:04.907
12

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.

Edit
Report

1 Answer

0

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.

answered 2011-03-04T21:18:54.740

Your Answer