score:13

Accepted answer

i think the confusion comes from 3. we'd think that 3 and 4 should behave the same. in fact, 3 is equivalent to this:

if 3 = 3 then: (do nothing) 'an empty statement
   debug.print 3 ' <-- this will be executed regardless of the previous if condition

to see it, change 3 into this:

if 3 = 0 then:
    debug.print 3 '<-- 3 will be printed! ;)

in conclusion, yes, the : is indeed to merge many statements on a single line

good job @vityata !!! :)

score:3

if then blocks require a matching end if when they are multi-line.

the first case is fine because the command after then is on the same line.

the second case is fine for the same reason, the : makes no difference in this situation.

the third case works because when the if statement evaluates to true, there is no command following the ':'. the : signals to compiler that the next command should be seen as being on the same line. there is nothing following, so processing moves to the next line which is seen as outside the if then block.

the fourth case has no notation to tell the compiler that the if then command is single line, and therefore it is looking for the end if to the block.

option explicit

public sub testme()

  if 1 = 1 then: debug.print 1 '<-- all on a single line - ok

  if 2 = 2 then debug.print 2  '<-- all on a single line - ok

  if 3 = 3 then:               '<-- no command following :
    debug.print 3              '<-- seen as outside the if then block

'   gives error:
'    if 4 = 4 then
'        debug.print 4
'    end if                   '<-- required to show the end of the if then block
end sub

Related Query

More Query from same tag