Wikia

Visual Basic Wiki

If

Talk0
71pages on
this wiki

The If StatementEdit

The If statement is probably the most common statement you will use in VB. It only executes the code inside it if the expression is true.

Simple UsageEdit

The simplest ways to use If are the following

'Small statement
If Expression Then MsgBox "It's true!"
If Expression Then MsgBox "It's true!": MsgBox "OMG OMG IT'S TRUE!! ONEONE11"
'Large Statement
If Expression Then
   MsgBox "It's true!"
   MsgBox "ZOMG ITS TRUE"
End If

ElseEdit

The else statement is executed when all else fails

'Small statement
If Expression Then MsgBox "It's true!" Else MsgBox "Boo hoo, it isn't!"
'Large Statement
If Expression Then
   MsgBox "It's true!"
   MsgBox "You kick my dog!"
Else
   MsgBox "Noooooooooo!"
End If

ElseIfEdit

The ElseIf is attempted if the previous statement fails and it is true. I don't know how to do a single line version but I really wouldn't want to, would you like messy code like that?

ElseIf's require Then as do If's, you can also use more than one ElseIf.

If Expression = "True" Then
   MsgBox "It's true!"
   MsgBox "You kick my dog!"
ElseIf Expression = "Poo" Then
   MsgBox "Oh poo!"
Else
   MsgBox "Double Noooooooooo!"
End If
Flow Control
If | For | While | Do | Until

Around Wikia's network

Random Wiki