Skip to content

Commit 2ca8544

Browse files
Merge branch 'main' into lerp-entry
2 parents 042b0b9 + 78017a4 commit 2ca8544

File tree

1 file changed

+86
-9
lines changed

1 file changed

+86
-9
lines changed

content/powershell/concepts/conditionals/conditionals.md

Lines changed: 86 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
---
22
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.'
44
Subjects:
5+
- 'Bash/Shell'
56
- 'Code Foundations'
67
- 'Computer Science'
7-
- 'Bash/Shell'
88
Tags:
99
- 'Break'
1010
- 'Conditionals'
@@ -26,9 +26,17 @@ There are four types of conditional statements in PowerShell:
2626
3. `elseif`
2727
4. `switch`
2828

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+
```
3038

31-
The `if` statement executes a block of code if the condition expression is `True`.
39+
### Example for `if` statement in PowerShell
3240

3341
```shell
3442
$my_num = 2
@@ -40,13 +48,26 @@ Write-Host "After the if statement"
4048

4149
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"`.
4250

43-
## else
51+
## `else` in PowerShell
4452

4553
The `else` clause always follows the `if` statement.
4654

4755
- If the condition is `True`, the code in the `if` section is executed.
4856
- If the condition is `False`, the code in the `else` section is executed.
4957

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+
5071
```shell
5172
$my_num = 2
5273
if($my_num -gt 0) {
@@ -59,9 +80,23 @@ else {
5980
}
6081
```
6182

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+
```
6398

64-
One or more `elseif` statements can be added between `if` and `else` to test multiple conditions.
99+
### PowerShell `elseif` statement example
65100

66101
```shell
67102
$my_num = 2
@@ -79,9 +114,19 @@ else {
79114
}
80115
```
81116

82-
## switch
117+
## `switch` in PowerShell
83118

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
85130

86131
```shell
87132
$my_num = 2
@@ -139,3 +184,35 @@ switch ($my_num) {
139184
}
140185
}
141186
```
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:
193+
194+
```shell
195+
$number = 10
196+
if ($number -gt 5) {
197+
Write-Output "Number is greater than 5"
198+
}
199+
```
200+
201+
### 2. Does PowerShell have else if?
202+
203+
Yes, PowerShell supports `elseif` for multiple conditions.
204+
205+
```shell
206+
$score = 75
207+
if ($score -ge 90) {
208+
Write-Output "Grade: A"
209+
} elseif ($score -ge 75) {
210+
Write-Output "Grade: B"
211+
} else {
212+
Write-Output "Grade: C"
213+
}
214+
```
215+
216+
### 3. Can I use ++ in PowerShell?
217+
218+
Yes. PowerShell supports the unary `++` and `--` operators for incrementing and decrementing values. It also supports unary `-` for negation.

0 commit comments

Comments
 (0)