Do you really need the "Then" at the end of the If block?

9

The Visual Basic .NET compiler seems to ignore the reserved word Then , which is at the end of the If block.

 If (1 + 1 = 2) Then
      Console.WriteLine("Passou no teste.")
 End If

And now, without Then :

 If (1 + 1 = 2)
      Console.WriteLine("Também passou no teste.")
 End If

This was the result in the Console:

  

Passou no teste.

     

Também passou no teste.

Is Keyword Then really needed in Visual Basic .NET? Does it have a special function or is it some kind of "Improve reading code"?

    
asked by anonymous 22.10.2015 / 00:29

1 answer

12

It is mandatory only when you are going to use commands on a line, so it is the separator. When you execute a block of commands it is optional .

Documentation .

Example of single line :

If x > 0 Then y = 0

Or more than one command on a line:

If x > 0 Then y = 0 : z = 0

Code block:

If x > 0
    y = 0
    z = 0
End If
    
22.10.2015 / 00:51