Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
Dim count As Integer = 0
Module Multiline
Public Sub Main()
'Create a Random object to seed our starting value
Dim randomizer As New Random()
'set our variable
Dim count As Integer = randomizer.Next(0, 5)

Dim message As String

'If count is zero, output will be no items
If count = 0 Then
message = "There are no items."
'If count is 1, output will be "There is 1 item.".
ElseIf count = 1 Then
message = "There is 1 item."
'If count is greater than 1, output will be "There are {count} items.", where {count} is replaced by the value of count.
Else
message = "There are " & count & " items."
message = $"There are {count} items."
End If

Console.WriteLine(message)
End Sub
End Module
'This example displays output like the following:
' There are 4 items.
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
Module Nested
Public Sub Main()
' Run the function as part of the WriteLine output.
Console.WriteLine("Time Check is " & CheckIfTime() & ".")
End Sub

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

' Return True if Wednesday from 2 to 4 P.M.,
' or if Thursday from noon to 1 P.M.
' Return True if Wednesday from 2 to 3:59 P.M.,
' or if Thursday from noon to 12:59 P.M.
If dayW = DayOfWeek.Wednesday Then
If hour = 14 Or hour = 15 Then
Return True
Expand All @@ -20,4 +26,7 @@
Else
Return False
End If
End Function
End Function
End Module
'This example displays output like the following:
'Time Check is False.
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
Module SingleLine
Public Sub Main()

'Create a Random object to seed our starting values
Dim randomizer As New Random()

Dim A As Integer = randomizer.Next(10, 20)
Dim B As Integer = randomizer.Next(0, 20)
Dim C As Integer = randomizer.Next(0, 5)

'Let's display the initial values for comparison
Console.WriteLine($"A value before If: {A}")
Console.WriteLine($"B value before If: {B}")
Console.WriteLine($"C value before If: {C}")

' If A > 10, execute the three colon-separated statements in the order
' that they appear
If A > 10 Then A = A + 1 : B = B + A : C = C + B
If A > 10 Then A = A + 1 : B = B + A : C = C + B

'If the condition is true, the values will be different
Console.WriteLine($"A value after If: {A}")
Console.WriteLine($"B value after If: {B}")
Console.WriteLine($"C value after If: {C}")

End Sub
End Module
'This example displays output like the following:
'A value before If: 11
'B value before If: 6
'C value before If: 3
'A value after If: 12
'B value after If: 18
'C value after If: 21
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
---
title: "If...Then...Else Statement (Visual Basic)"
ms.date: 07/20/2015
ms.date: 04/16/2018
ms.prod: .net
ms.reviewer: ""
ms.suite: ""
ms.technology:
- "devlang-visual-basic"
ms.topic: "article"
ms.topic: "conceptual"
f1_keywords:
- "vb.ElseIf"
- "vb.Then"
Expand Down Expand Up @@ -37,7 +37,7 @@ Conditionally executes a group of statements, depending on the value of an expre
## Syntax

```
' Multiple-line syntax:
' Multiline syntax:
If condition [ Then ]
[ statements ]
[ ElseIf elseifcondition [ Then ]
Expand All @@ -49,15 +49,23 @@ End If
' Single-line syntax:
If condition Then [ statements ] [ Else [ elsestatements ] ]
```


## Quick links to example code

This article includes several examples that illustrate uses of the `If`...`Then`...`Else` statement:

* [Multiline syntax example](#multi-line)
* [Nested syntax example](#nested)
* [Single-line syntax example](#single-line)

## Parts
`condition`
Required. Expression. Must evaluate to `True` or `False`, or to a data type that is implicitly convertible to `Boolean`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On line 65, there should be a space between Nullable and Boolean
if the expression is not True can be easily misread; much better to say "if the expression is `False`
no comma before "the `Else`"


If the expression is a [Nullable](../../../visual-basic/programming-guide/language-features/data-types/nullable-value-types.md)`Boolean` variable that evaluates to [Nothing](../../../visual-basic/language-reference/nothing.md), the condition is treated as if the expression is not `True`, and the `Else` block is executed.
If the expression is a [Nullable](../../../visual-basic/programming-guide/language-features/data-types/nullable-value-types.md) `Boolean` variable that evaluates to [Nothing](../../../visual-basic/language-reference/nothing.md), the condition is treated as if the expression is `False` and the `Else` block is executed.

`Then`
Required in the single-line syntax; optional in the multiple-line syntax.
Required in the single-line syntax; optional in the multiline syntax.

`statements`
Optional. One or more statements following `If`...`Then` that are executed if `condition` evaluates to `True`.
Expand All @@ -72,43 +80,51 @@ If condition Then [ statements ] [ Else [ 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.
Terminates the multiline version of `If`...`Then`...`Else` block.

## Remarks

## 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 `True``elseifcondition` 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`.
### Multiline 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 `True` `elseifcondition` 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.
In the multiline 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.

> [!TIP]
> The [Select...Case Statement](../../../visual-basic/language-reference/statements/select-case-statement.md) might be more useful when you evaluate a single expression that has several possible values.

## 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.
### Single-Line syntax
You can use the single-line syntax for a single condition with code to execute if it's true. However, the multiple-line syntax provides more structure and flexibility and is 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.

## Example
The following example illustrates the use of the multiple-line syntax of the `If`...`Then`...`Else` statement.

[!code-vb[VbVbalrStatements#101](../../../visual-basic/language-reference/error-messages/codesnippet/VisualBasic/if-then-else-statement_1.vb)]

## Example

## Multiline syntax example

<a name="multi-line"></a>

The following example illustrates the use of the multiline syntax of the `If`...`Then`...`Else` statement.

[!code-vb[VbVbalrStatements#101](../../../visual-basic/language-reference/error-messages/codesnippet/VisualBasic/if-then-else-statement_1.vb?highlight=11,14,17,19)]

## Nested syntax example

<a name="nested"></a>

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

[!code-vb[VbVbalrStatements#102](../../../visual-basic/language-reference/error-messages/codesnippet/VisualBasic/if-then-else-statement_2.vb)]
[!code-vb[VbVbalrStatements#102](../../../visual-basic/language-reference/error-messages/codesnippet/VisualBasic/if-then-else-statement_2.vb?highlight=14,15,17,19,20,21,23,25,26,28)]

## Single-Line syntax example

## Example
<a name="single-line"></a>
The following example illustrates the use of the single-line syntax.

[!code-vb[VbVbalrStatements#103](../../../visual-basic/language-reference/error-messages/codesnippet/VisualBasic/if-then-else-statement_3.vb)]
[!code-vb[VbVbalrStatements#103](../../../visual-basic/language-reference/error-messages/codesnippet/VisualBasic/if-then-else-statement_3.vb?highlight=18)]

## See Also
## See also
<xref:Microsoft.VisualBasic.Interaction.Choose%2A>
<xref:Microsoft.VisualBasic.Interaction.Switch%2A>
[#If...Then...#Else Directives](../../../visual-basic/language-reference/directives/if-then-else-directives.md)
Expand Down