From 633c12b9a06b5e65e3c6a54753591630cabc27c5 Mon Sep 17 00:00:00 2001 From: Sean Wheeler Date: Tue, 17 Sep 2019 19:48:56 -0700 Subject: [PATCH 1/2] Fixes #4648 - ternary operator --- .../About/about_If.md | 26 ++++++++++++++++++- .../About/about_Operators.md | 7 +++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/reference/7/Microsoft.PowerShell.Core/About/about_If.md b/reference/7/Microsoft.PowerShell.Core/About/about_If.md index 353aeed5cd5f..24c975d37b44 100644 --- a/reference/7/Microsoft.PowerShell.Core/About/about_If.md +++ b/reference/7/Microsoft.PowerShell.Core/About/about_If.md @@ -81,7 +81,6 @@ else { To further refine this example, you can use the Elseif statement to display a message when the value of $a is equal to 2. As the next example shows: - ```powershell if ($a -gt 2) { Write-Host "The value $a is greater than 2." @@ -95,6 +94,31 @@ else { } ``` +### Using the ternary operator syntax + +PowerShell 7.0 introduced a new syntax using the ternary operator. It follows the C# ternary +operator syntax: + +```Syntax + ? : +``` + +The ternary operator behaves like the simplified `if-else` statement. The `` expression +is evaluated and the result is converted to a boolean to determine which branch should be evaluated +next: + +- The `` expression is executed if the `` expression is true +- The `` expression is executed if the `` expression is false + +For example: + +```powershell +$message = (Test-Path $path) ? "Path exists" : "Path not found" +``` + +In this example, the value of `$message` is "Path exists" when `Test-Path` returns `$true`. When +`Test-Path` returns `$false`, the value of `$message` is "Path not found". + ## SEE ALSO [about_Comparison_Operators](about_Comparison_Operators.md) diff --git a/reference/7/Microsoft.PowerShell.Core/About/about_Operators.md b/reference/7/Microsoft.PowerShell.Core/About/about_Operators.md index 1d7d2aedcf51..21645ae65562 100644 --- a/reference/7/Microsoft.PowerShell.Core/About/about_Operators.md +++ b/reference/7/Microsoft.PowerShell.Core/About/about_Operators.md @@ -442,6 +442,13 @@ $($x * 23) $(Get-WmiObject win32_Directory) ``` +#### Ternary operator `? : ` + +You can use the ternary operator as a replacement for `if-else` statement in simple conditional +cases. The ternary operator was introduced in PowerShell 7.0. + +For more information, see [about_If](about_If.md). + ## See also [about_Arithmetic_Operators](about_Arithmetic_Operators.md) From 315ead8777205b1c93994dc81b961e12bd55e8e5 Mon Sep 17 00:00:00 2001 From: Sean Wheeler Date: Tue, 17 Sep 2019 19:53:00 -0700 Subject: [PATCH 2/2] fix line wrap --- .../7/Microsoft.PowerShell.Core/About/about_Operators.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/reference/7/Microsoft.PowerShell.Core/About/about_Operators.md b/reference/7/Microsoft.PowerShell.Core/About/about_Operators.md index 21645ae65562..084f88590dfe 100644 --- a/reference/7/Microsoft.PowerShell.Core/About/about_Operators.md +++ b/reference/7/Microsoft.PowerShell.Core/About/about_Operators.md @@ -444,8 +444,8 @@ $(Get-WmiObject win32_Directory) #### Ternary operator `? : ` -You can use the ternary operator as a replacement for `if-else` statement in simple conditional -cases. The ternary operator was introduced in PowerShell 7.0. +You can use the ternary operator as a replacement for the `if-else` statement in +simple conditional cases. The ternary operator was introduced in PowerShell 7.0. For more information, see [about_If](about_If.md).