You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Update If Then Else (Visual Basic) to improve CSAT (#4942)
* Added Quick links to example code section
* Revised based on review feedback
* Updating code samples
* Revising links
* Updating Code sample links based on feedback
* revising code sample links
* Updating highlighting
* Updated code comments, changed header case
* Added Output at end of each sample, updated highlighting
@@ -37,7 +37,7 @@ Conditionally executes a group of statements, depending on the value of an expre
37
37
## Syntax
38
38
39
39
```
40
-
' Multiple-line syntax:
40
+
' Multiline syntax:
41
41
If condition [ Then ]
42
42
[ statements ]
43
43
[ ElseIf elseifcondition [ Then ]
@@ -49,15 +49,23 @@ End If
49
49
' Single-line syntax:
50
50
If condition Then [ statements ] [ Else [ elsestatements ] ]
51
51
```
52
-
52
+
53
+
## Quick links to example code
54
+
55
+
This article includes several examples that illustrate uses of the `If`...`Then`...`Else` statement:
56
+
57
+
*[Multiline syntax example](#multi-line)
58
+
*[Nested syntax example](#nested)
59
+
*[Single-line syntax example](#single-line)
60
+
53
61
## Parts
54
62
`condition`
55
63
Required. Expression. Must evaluate to `True` or `False`, or to a data type that is implicitly convertible to `Boolean`.
56
64
57
-
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.
65
+
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.
58
66
59
67
`Then`
60
-
Required in the single-line syntax; optional in the multiple-line syntax.
68
+
Required in the single-line syntax; optional in the multiline syntax.
61
69
62
70
`statements`
63
71
Optional. One or more statements following `If`...`Then` that are executed if `condition` evaluates to `True`.
@@ -72,43 +80,51 @@ If condition Then [ statements ] [ Else [ elsestatements ] ]
72
80
Optional. One or more statements that are executed if no previous `condition` or `elseifcondition` expression evaluates to `True`.
73
81
74
82
`End If`
75
-
Terminates the `If`...`Then`...`Else` block.
83
+
Terminates the multiline version of `If`...`Then`...`Else` block.
76
84
77
85
## Remarks
78
86
79
-
##Multiple-Line Syntax
80
-
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`.
87
+
### Multiline syntax
88
+
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`.
81
89
82
90
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.
83
91
84
-
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.
92
+
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.
85
93
86
94
> [!TIP]
87
95
> 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.
88
96
89
-
## Single-Line Syntax
90
-
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.
97
+
###Single-Line syntax
98
+
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.
91
99
92
100
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`.
93
101
94
102
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.
95
-
96
-
## Example
97
-
The following example illustrates the use of the multiple-line syntax of the `If`...`Then`...`Else` statement.
0 commit comments