You are on page 1of 3

If...Then...

Else Statement (Visual Basic)


Conditionally executes a group of statements, depending on the value of an expression.
' Multiple-line syntax: If condition [ Then ] [ statements ] [ ElseIf elseifcondition [ Then ] [ elseifstatements ] ] [ Else [ elsestatements ] ] End If ' Single-line syntax: If condition Then [ statements ] [ Else [ elsestatements ] ]

condition Required. Expression. Must evaluate to True or False, or to a data type that is implicitly convertible to Boolean. If the expression is a NullableBoolean variable that evaluates to Nothing, the condition is treated as if the expression is not True, and the Else block is executed. Then Required in the single-line syntax; optional in the multiple-line syntax. statements Optional. One or more statements following If...Then that are executed if condition evaluates to True. elseifcondition Required if ElseIf is present. Expression. Must evaluate to True or False, or to a data type that is implicitly convertible to Boolean. elseifstatements Optional. One or more statements following ElseIf...Then that are executed if elseifcondition evaluates to True. elsestatements Optional. One or more statements that are executed if no previous condition or elseifcondition expression evaluates to True. End If Terminates the If...Then...Else block.

Multiple-Line Syntax
When an If...Then...Else statement is encountered, condition is tested. If condition is True, the statements following Then are executed. If condition is False, each ElseIf statement (if there are any) is evaluated in order. When a Trueelseifcondition is found, the statements immediately following the associated ElseIf are executed. If no elseifcondition evaluates to True, or if there are no ElseIf statements, the statements following Else are executed. After executing the statements following Then, ElseIf, or Else, execution continues with the statement following End If.

The ElseIf and Else clauses are both optional. You can have as many ElseIf clauses as you want in an If...Then...Else statement, but no ElseIf clause can appear after an Else clause. If...Then...Else statements can be nested within each other. In the multiple-line syntax, the If statement must be the only statement on the first line. The ElseIf, Else, and End If statements can be preceded only by a line label. The If...Then...Else block must end with an End If statement.

Single-Line Syntax
You can use the single-line syntax for short, simple tests. However, the multiple-line syntax provides more structure and flexibility and is usually easier to read, maintain, and debug. What follows the Then keyword is examined to determine whether a statement is a single-line If. If anything other than a comment appears after Then on the same line, the statement is treated as a single-line If statement. If Then is absent, it must be the start of a multiple-line If...Then...Else. In the single-line syntax, you can have multiple statements executed as the result of an If...Then decision. All statements must be on the same line and be separated by colons. EXAMPLES
Dim count As Integer = 0 Dim message As String If count = 0 Then message = "There are no items." ElseIf count = 1 Then message = "There is 1 item." Else message = "There are " & count & " items." End If

The following example contains nested If...Then...Else statements.


Private ' Determine Dim dayW As Dim hour As Function CheckIfTime() As Boolean the current day of week and hour of day. DayOfWeek = DateTime.Now.DayOfWeek Integer = DateTime.Now.Hour

' Return True if Wednesday from 2 to 4 P.M., ' or if Thursday from noon to 1 P.M. If dayW = DayOfWeek.Wednesday Then If hour = 14 Or hour = 15 Then Return True Else Return False End If ElseIf dayW = DayOfWeek.Thursday Then If hour = 12 Then Return True Else Return False End If Else Return False End If End Function

The following example illustrates the use of the single-line syntax.


If A > 10 Then A = A + 1 : B = B + A : C = C + B

If Statement
The if statement is a conditional branch statement. The syntax of the if statement is either one of these two:

if (booleanExpression) { statement (s) }

if (booleanExpression) { statement (s) } else { statement (s) }

For example, in the following if statement, the if block will be executed if x is greater than 4.
public class MainClass { public static void main(String[] args) { int x = 9; if (x > 4) { // statements } } }

In the following example, the if block will be executed if a is greater than 3. Otherwise, the else block will be executed.
public class MainClass { public static void main(String[] args) { int a = 3; if (a > 3) { // statements } else { // statements } } }

You might also like