如何在 if 語句中結合多個條件

  1. PowerShell 中的 if 語句
  2. 在 PowerShell 的 if 語句中使用邏輯運算符結合多個條件
  3. 使用多個運算符在 PowerShell 的 if 語句中結合多個條件
  4. 在 PowerShell 中嵌套 if 語句以結合多個條件
  5. 結論
如何在 if 語句中結合多個條件

在 PowerShell 中,if 語句是進行決策的重要工具。然而,許多任務需要您結合多個條件以獲得更精確的控制。

本文將探討如何在 PowerShell 的 if 語句中使用邏輯運算符結合多個條件的各種方法。

PowerShell 中的 if 語句

以下是 if 語句的簡單範例。如果括號內的條件評估為真,則將執行命令 Write-Host "2 is equal to 2."

代碼:

if (2 -eq 2) {
    Write-Host "2 is equal to 2."
}

如果括號 () 中指定的條件評估為 $true,那麼它將執行大括號 {} 中的命令。

輸出:

2 is equal to 2.

如果條件為 $false,則會跳過該代碼區塊。上面的範例使用了一個條件,但您也可以在 if 語句中評估多個條件。

在 PowerShell 的 if 語句中使用邏輯運算符結合多個條件

邏輯運算符用於在 PowerShell 中連接語句和表達式。您可以使用單一表達式來測試多個條件。

PowerShell 中支持的邏輯運算符為 -and-or-not-xor!。本文將僅討論如何使用 -and-or-not-xor 運算符在 PowerShell 的 if 語句中結合多個條件。

在 PowerShell 的 if 語句中使用 -and 運算符結合多個條件

以下範例使用 -and 運算符在 if 語句中連接兩個語句。如果第一個條件 5 小於 10 為真且第二個條件 7 大於 5 為真,將執行 Write-Host 命令。

代碼:

if ((5 -lt 10) -and (7 -gt 5)) {
    Write-Host "The above conditions are true."
}

輸出:

The above conditions are true.

由於在這種情況下兩個條件都為真,因此代碼區塊被執行,並在輸出中打印消息 "The above conditions are true."

在 PowerShell 的 if 語句中使用 -or 運算符結合多個條件

在這段代碼中,我們使用 -lt(小於)和 -gt(大於)運算符來檢查 5 是否小於 10 或 7 是否大於 5。然後,我們使用 -or 運算符來檢查 (5 -lt 10)(7 -gt 5) 是否為真。

代碼:

if ((5 -lt 10) -or (7 -gt 5)) {
    Write-Host "At least one of the above conditions is true."
}
else {
    Write-Host "Neither of the above conditions is true."
}

輸出:

At least one of the above conditions is true.

由於在原始代碼中兩個條件都為真,因此打印出 "At least one of the conditions is true."

在 PowerShell 的 if 語句中使用 -not 運算符結合多個條件

在以下代碼中,我們有一個變數 $number 代表一個數值。我們使用 -lt(小於)和 -gt(大於)運算符來檢查該數字是否小於 5 或大於 10。

-or 運算符用於結合左右兩側的兩個條件。如果至少有一個條件為真,它將返回真。由於兩個條件都為假,因此 -or 表達式評估為假。

-not 運算符應用於 -or 表達式的結果。由於 -or 表達式為假,應用 -not 使其變為真。

代碼:

$number = 7

if (-not ($number -lt 5 -or $number -gt 10)) {
    Write-Host "The number is between 5 and 10 (inclusive)."
}
else {
    Write-Host "The number is outside the range of 5 to 10."
}

輸出:

The number is between 5 and 10 (inclusive).

由於 -not 運算符使結合的條件為真,因此 if 區塊中的代碼被執行。輸出顯示 $number = 7 的值落在 5 到 10(含)的指定範圍內。

在 PowerShell 的 if 語句中使用 -xor 運算符結合多個條件

以下代碼檢查兩個條件並使用 -xor 運算符來確定其結合的真值。在 if 語句中,有兩個條件:第一個條件是 5 是否小於 10,第二個條件是 7 是否大於 5。

-xor 運算符是一種排他性 OR 運算符,這意味著它在恰好一個操作數為真時返回真,如果兩個操作數都為真或都為假,則返回假。

在這種情況下,兩個條件都為真,因此 -xor 表達式評估為假,並執行 else 區塊中的代碼。

代碼:

if ((5 -lt 10) -xor (7 -gt 5)) {
    Write-Host "Exactly one of the above conditions is true."
}
else {
    Write-Host "Neither or both of the above conditions are true."
}

輸出:

Neither or both of the above conditions are true.

輸出打印 "Neither or both of the above conditions are true.",因為這表明這兩個條件都不是單獨為真;兩個條件要麼一起為真,要麼一起為假。

使用多個運算符在 PowerShell 的 if 語句中結合多個條件

同樣,您可以在 PowerShell 的 if 語句中使用邏輯運算符結合多個條件。在此範例中,根據結合條件是否為 truefalse 執行不同的命令。

下面的代碼使用邏輯運算符 -and-or 結合多個條件。如果任何這些條件為真,則將執行第一條命令(Write-Host "It is true."),否則將執行第二條命令(Write-Host "It is false.")。

代碼:

if (((10 -lt 20) -and (10 -eq 10)) -or ((15 -gt 5) -and (12 -lt 6))) {
    Write-Host "It is true."
}
else {
    Write-Host "It is false."
}

輸出:

It is true.

在這裡,if 語句檢查結合條件並根據條件的評估執行適當的命令區塊。由於條件為真,因此打印了消息 "It is true."

請記得將每組條件放在括號 () 中,並使用適合您特定要求的邏輯運算符。

在 PowerShell 中嵌套 if 語句以結合多個條件

當您有需要多層評估的複雜條件時,嵌套 if 語句可能會非常有用。例如,當您需要在特定上下文中檢查條件或根據不同條件執行不同操作時,嵌套 if 語句可以實現更結構化的方式。

在這段代碼中,我們宣告一個變數 $number = 15。然後,我們創建外部 if 語句來檢查是否 $number 大於或等於 10。

如果外部條件為真,嵌套的 if 語句檢查 $number 是否小於或等於 20。

代碼:

$number = 15

if ($number -ge 10) {
    if ($number -le 20) {
        Write-Host "The number is between 10 and 20."
    }
}

輸出:

The number is between 10 and 20.

在這段代碼中,外部 if 語句檢查 $number 是否 大於或等於 10。如果滿足此條件,則繼續進入嵌套的 if 語句,該語句檢查 $number 是否 小於或等於 20

只有當兩個條件都為真時,才執行 Write-Host 命令。在輸出中,兩個條件都得到了滿足,並打印消息 "The number is between 10 and 20."

結論

總之,在 PowerShell 的 if 語句中結合多個條件對於製作高效的腳本至關重要。通過邏輯運算符 -and-or-not-xor,您可以創建複雜的條件邏輯以解決廣泛的腳本挑戰。

無論是簡單的條件還是嵌套的 if 語句,PowerShell 提供了強大的工具來幫助您做出明智的決策,並在腳本中執行正確的操作。

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
作者: Rohan Timalsina
Rohan Timalsina avatar Rohan Timalsina avatar

Rohan is a learner, problem solver, and web developer. He loves to write and share his understanding.

LinkedIn Website

相關文章 - PowerShell Statement