Skip to content

Commit 725c2b4

Browse files
authored
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
1 parent b1f122a commit 725c2b4

File tree

4 files changed

+99
-29
lines changed

4 files changed

+99
-29
lines changed
Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
1-
Dim count As Integer = 0
1+
Module Multiline
2+
Public Sub Main()
3+
'Create a Random object to seed our starting value
4+
Dim randomizer As New Random()
5+
'set our variable
6+
Dim count As Integer = randomizer.Next(0, 5)
7+
28
Dim message As String
39

10+
'If count is zero, output will be no items
411
If count = 0 Then
512
message = "There are no items."
13+
'If count is 1, output will be "There is 1 item.".
614
ElseIf count = 1 Then
715
message = "There is 1 item."
16+
'If count is greater than 1, output will be "There are {count} items.", where {count} is replaced by the value of count.
817
Else
9-
message = "There are " & count & " items."
18+
message = $"There are {count} items."
1019
End If
20+
21+
Console.WriteLine(message)
22+
End Sub
23+
End Module
24+
'This example displays output like the following:
25+
' There are 4 items.

docs/visual-basic/language-reference/error-messages/codesnippet/VisualBasic/if-then-else-statement_2.vb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1+
Module Nested
2+
Public Sub Main()
3+
' Run the function as part of the WriteLine output.
4+
Console.WriteLine("Time Check is " & CheckIfTime() & ".")
5+
End Sub
6+
17
Private Function CheckIfTime() As Boolean
28
' Determine the current day of week and hour of day.
39
Dim dayW As DayOfWeek = DateTime.Now.DayOfWeek
410
Dim hour As Integer = DateTime.Now.Hour
511

6-
' Return True if Wednesday from 2 to 4 P.M.,
7-
' or if Thursday from noon to 1 P.M.
12+
' Return True if Wednesday from 2 to 3:59 P.M.,
13+
' or if Thursday from noon to 12:59 P.M.
814
If dayW = DayOfWeek.Wednesday Then
915
If hour = 14 Or hour = 15 Then
1016
Return True
@@ -20,4 +26,7 @@
2026
Else
2127
Return False
2228
End If
23-
End Function
29+
End Function
30+
End Module
31+
'This example displays output like the following:
32+
'Time Check is False.
Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,33 @@
1+
Module SingleLine
2+
Public Sub Main()
3+
4+
'Create a Random object to seed our starting values
5+
Dim randomizer As New Random()
6+
7+
Dim A As Integer = randomizer.Next(10, 20)
8+
Dim B As Integer = randomizer.Next(0, 20)
9+
Dim C As Integer = randomizer.Next(0, 5)
10+
11+
'Let's display the initial values for comparison
12+
Console.WriteLine($"A value before If: {A}")
13+
Console.WriteLine($"B value before If: {B}")
14+
Console.WriteLine($"C value before If: {C}")
15+
116
' If A > 10, execute the three colon-separated statements in the order
217
' that they appear
3-
If A > 10 Then A = A + 1 : B = B + A : C = C + B
18+
If A > 10 Then A = A + 1 : B = B + A : C = C + B
19+
20+
'If the condition is true, the values will be different
21+
Console.WriteLine($"A value after If: {A}")
22+
Console.WriteLine($"B value after If: {B}")
23+
Console.WriteLine($"C value after If: {C}")
24+
25+
End Sub
26+
End Module
27+
'This example displays output like the following:
28+
'A value before If: 11
29+
'B value before If: 6
30+
'C value before If: 3
31+
'A value after If: 12
32+
'B value after If: 18
33+
'C value after If: 21

docs/visual-basic/language-reference/statements/if-then-else-statement.md

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
---
22
title: "If...Then...Else Statement (Visual Basic)"
3-
ms.date: 07/20/2015
3+
ms.date: 04/16/2018
44
ms.prod: .net
55
ms.reviewer: ""
66
ms.suite: ""
77
ms.technology:
88
- "devlang-visual-basic"
9-
ms.topic: "article"
9+
ms.topic: "conceptual"
1010
f1_keywords:
1111
- "vb.ElseIf"
1212
- "vb.Then"
@@ -37,7 +37,7 @@ Conditionally executes a group of statements, depending on the value of an expre
3737
## Syntax
3838

3939
```
40-
' Multiple-line syntax:
40+
' Multiline syntax:
4141
If condition [ Then ]
4242
[ statements ]
4343
[ ElseIf elseifcondition [ Then ]
@@ -49,15 +49,23 @@ End If
4949
' Single-line syntax:
5050
If condition Then [ statements ] [ Else [ elsestatements ] ]
5151
```
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+
5361
## Parts
5462
`condition`
5563
Required. Expression. Must evaluate to `True` or `False`, or to a data type that is implicitly convertible to `Boolean`.
5664

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.
5866

5967
`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.
6169

6270
`statements`
6371
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 ] ]
7280
Optional. One or more statements that are executed if no previous `condition` or `elseifcondition` expression evaluates to `True`.
7381

7482
`End If`
75-
Terminates the `If`...`Then`...`Else` block.
83+
Terminates the multiline version of `If`...`Then`...`Else` block.
7684

7785
## Remarks
7886

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`.
8189

8290
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.
8391

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.
8593

8694
> [!TIP]
8795
> 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.
8896
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.
9199

92100
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`.
93101

94102
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.
98-
99-
[!code-vb[VbVbalrStatements#101](../../../visual-basic/language-reference/error-messages/codesnippet/VisualBasic/if-then-else-statement_1.vb)]
100-
101-
## Example
103+
104+
## Multiline syntax example
105+
106+
<a name="multi-line"></a>
107+
108+
The following example illustrates the use of the multiline syntax of the `If`...`Then`...`Else` statement.
109+
110+
[!code-vb[VbVbalrStatements#101](../../../visual-basic/language-reference/error-messages/codesnippet/VisualBasic/if-then-else-statement_1.vb?highlight=11,14,17,19)]
111+
112+
## Nested syntax example
113+
114+
<a name="nested"></a>
115+
102116
The following example contains nested `If`...`Then`...`Else` statements.
103117

104-
[!code-vb[VbVbalrStatements#102](../../../visual-basic/language-reference/error-messages/codesnippet/VisualBasic/if-then-else-statement_2.vb)]
118+
[!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)]
119+
120+
## Single-Line syntax example
105121

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

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

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

0 commit comments

Comments
 (0)