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
Copy file name to clipboardExpand all lines: content/powershell/concepts/conditionals/conditionals.md
+86-9Lines changed: 86 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,10 @@
1
1
---
2
2
Title: 'Conditionals'
3
-
Description: 'The if, else, elseif, and switch conditionals in PowerShell are used for control flow. They allow the choice of which sections of code are executed based on conditions.'
3
+
Description: 'Control program flow in PowerShell using if, else, elseif, and switch to execute code based on conditions.'
4
4
Subjects:
5
+
- 'Bash/Shell'
5
6
- 'Code Foundations'
6
7
- 'Computer Science'
7
-
- 'Bash/Shell'
8
8
Tags:
9
9
- 'Break'
10
10
- 'Conditionals'
@@ -26,9 +26,17 @@ There are four types of conditional statements in PowerShell:
26
26
3.`elseif`
27
27
4.`switch`
28
28
29
-
## if
29
+
## `if` in PowerShell
30
+
31
+
The `if` statement executes a block of code if the condition expression is `True`. Syntax for `if` in PowerShell is:
32
+
33
+
```pseudo
34
+
if (<condition>) {
35
+
<statements>
36
+
}
37
+
```
30
38
31
-
The `if` statement executes a block of code if the condition expression is `True`.
39
+
### Example for `if` statement in PowerShell
32
40
33
41
```shell
34
42
$my_num = 2
@@ -40,13 +48,26 @@ Write-Host "After the if statement"
40
48
41
49
The example above prints `"A True Statement"` and `"After the if statement"` if `$my_num` is equal to `2`. Otherwise, it skips the statement within the `if` curly braces `{ }` and just prints `"After the if statement"`.
42
50
43
-
## else
51
+
## `else` in PowerShell
44
52
45
53
The `else` clause always follows the `if` statement.
46
54
47
55
- If the condition is `True`, the code in the `if` section is executed.
48
56
- If the condition is `False`, the code in the `else` section is executed.
49
57
58
+
Here is the syntax for `else` in PowerShell:
59
+
60
+
```pseudo
61
+
if (<condition>) {
62
+
<statements>
63
+
}
64
+
else {
65
+
<statements>
66
+
}
67
+
```
68
+
69
+
### PowerShell `else` statement example
70
+
50
71
```shell
51
72
$my_num = 2
52
73
if($my_num -gt 0) {
@@ -59,9 +80,23 @@ else {
59
80
}
60
81
```
61
82
62
-
## elseif
83
+
## `elseif` in PowerShell
84
+
85
+
One or more `elseif` statements can be added between `if` and `else` to test multiple conditions. Here is the syntax for `elseif` in PowerShell:
86
+
87
+
```pseudo
88
+
if (<condition1>) {
89
+
<statements>
90
+
}
91
+
elseif (<condition2>) {
92
+
<statements>
93
+
}
94
+
else {
95
+
<statements>
96
+
}
97
+
```
63
98
64
-
One or more `elseif`statements can be added between `if` and `else` to test multiple conditions.
99
+
### PowerShell `elseif`statement example
65
100
66
101
```shell
67
102
$my_num = 2
@@ -79,9 +114,19 @@ else {
79
114
}
80
115
```
81
116
82
-
## switch
117
+
## `switch` in PowerShell
83
118
84
-
The `switch` statement provides a simpler syntax for the same behavior as `if` / `elseif` expressions that check for equality.
119
+
The `switch` statement provides a simpler syntax for the same behavior as `if` / `elseif` expressions that check for equality. Here is the syntax for `switch` in PowerShell:
120
+
121
+
```pseudo
122
+
switch (<value>) {
123
+
<pattern1> { <statements> }
124
+
<pattern2> { <statements> }
125
+
default { <statements> }
126
+
}
127
+
```
128
+
129
+
### Example for `switch` in Powershell
85
130
86
131
```shell
87
132
$my_num = 2
@@ -139,3 +184,35 @@ switch ($my_num) {
139
184
}
140
185
}
141
186
```
187
+
188
+
## Frequently Asked Questions
189
+
190
+
### 1. How to use if condition in PowerShell?
191
+
192
+
In PowerShell, the `if` statement checks whether a condition is true before running a block of code. Here is how to implement it:
0 commit comments