-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added main concept: bools - Added concept exercise: pac-man - Added minor concept: comment-based help
- Loading branch information
Showing
13 changed files
with
586 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"blurb": "Boolean type in PowerShell, true and false values and logical operators.", | ||
"authors": ["glaxxie"], | ||
"contributors": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# About | ||
|
||
## Booleans | ||
|
||
PowerShell has the [`bool`][booleans] type to represents true and false values. | ||
In this type there are only two values: `True` and `False`. | ||
You can bind these values to variables with the prefix `$`: | ||
|
||
```powershell | ||
> $TrueVar = $true | ||
> $FalseVar = $FALSE | ||
#Case senstitivity doesn't matter here | ||
``` | ||
|
||
We can use [logical operators][logical-operators] like `-and`, `-or`, and `-not` to combine and evaluate Boolean expressions. | ||
|
||
```powershell | ||
> $TrueVar = $true -and $true | ||
> $FalseVar = $true -and $false | ||
> $TrueVar = $true -or $false | ||
> $FalseVar = $false -or $false | ||
> $TrueVar = -not $false | ||
> $FalseVar = -not $true | ||
``` | ||
|
||
## Conversion and truthiness | ||
|
||
PowerShell can implicitly treat any type as a Boolean. | ||
However you can also use the Boolean type accelerator `[bool]` for explicit conversion. | ||
|
||
Here are some general rules that PowerShell use to convert other types to Boolean values: | ||
|
||
$false: | ||
- Emptry strings like `''` or `""` | ||
- Null values like `$null` | ||
- Numeric type with the value of zero (0) | ||
|
||
$true: | ||
- Non-empty strings | ||
- Instances of any other non-collection type | ||
|
||
```powershell | ||
> [bool] 0 | ||
False | ||
> [bool] 0.0 | ||
False | ||
> [bool] "" | ||
False | ||
> [bool] "Hello" | ||
True | ||
> [bool] 1234 | ||
True | ||
``` | ||
|
||
For in depth rules about converting Boolean values from collection types, please read [here][collection-conversion]. | ||
|
||
[booleans]: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_booleans?view=powershell | ||
[logical-operators]: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_logical_operators | ||
[collection-conversion]: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_booleans?#converting-from-collection-types |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Introduction | ||
|
||
|
||
PowerShell has the [`bool`][booleans] type to represents true and false values. | ||
In this type there are only two values: `True` and `False`. | ||
You can bind these values to variables with the prefix `$`: | ||
|
||
```powershell | ||
> $TrueVar = $true | ||
> $FalseVar = $FALSE | ||
#Case senstitivity doesn't matter here | ||
``` | ||
|
||
We can use [logical operators][logical-operators] like `-and`, `-or`, and `-not` to combine and evaluate Boolean expressions. | ||
|
||
```powershell | ||
> $TrueVar = $true -and $true | ||
> $FalseVar = $true -and $false | ||
> $TrueVar = $true -or $false | ||
> $FalseVar = $false -or $false | ||
> $TrueVar = -not $false | ||
> $FalseVar = -not $true | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[ | ||
{ | ||
"url": "https://en.wikipedia.org/wiki/Truth_table", | ||
"description": "truth table" | ||
}, | ||
{ | ||
"url": "https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_operator_precedence", | ||
"description": "operator precedence" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.