Bicep 論理演算子

論理演算子は、ブール値を評価するか、null 以外の値を返すか、または条件式を評価します。 この例を実行するには、Azure CLI または Azure PowerShell を使用して、Bicep ファイルをデプロイします。

演算子 名前
&& And
|| Or
! Not
?? Coalesce
? : 条件式

And &&

operand1 && operand2

両方の値が true かどうかを判断します。

オペランド

オペランド 種類 Description
operand1 boolean true かどうかを確認する最初の値。
operand2 boolean true かどうかを確認する 2 番目の値。
追加のオペランド boolean 追加のオペランドを含めることができます。

戻り値

両方の値が true の場合は True、それ以外の場合は false が返されます。

一連のパラメーター値と一連の式を評価します。

param operand1 bool = true
param operand2 bool = true

output andResultParm bool = operand1 && operand2
output andResultExp bool = 10 >= 10 && 5 > 2

出力例を次に示します。

名前 Type
andResultParm boolean true
andResultExp boolean true

Bicep オブジェクトを使って "言語式プロパティ 'foo' が存在しません" 例外を回避するには、次の例のように And 論理演算子を使います。

param objectToTest object = {
  one: 1
  two: 2
  three: 3
}

output bar bool = contains(objectToTest, 'four') && objectToTest.four == 4

Or ||

operand1 || operand2

どちらかの値が true かどうかを判断します。

オペランド

オペランド 種類 Description
operand1 boolean true かどうかを確認する最初の値。
operand2 boolean true かどうかを確認する 2 番目の値。
追加のオペランド boolean 追加のオペランドを含めることができます。

戻り値

どちらかの値が true の場合は True、それ以外の場合は false が返されます。

一連のパラメーター値と一連の式を評価します。

param operand1 bool = true
param operand2 bool = false

output orResultParm bool = operand1 || operand2
output orResultExp bool = 10 >= 10 || 5 < 2

出力例を次に示します。

名前 Type
orResultParm boolean true
orResultExp boolean true

"言語式のプロパティ配列インデックス 'x' が範囲外です" 例外を回避するには、次の例のように Or 論理演算子を使います。

param emptyArray array = []
param numberArray array = [1, 2, 3]

output foo bool = empty(emptyArray) || emptyArray[0] == 'bar'
output bar bool = length(numberArray) >= 3 || numberArray[3] == 4

Not !

!boolValue

ブール値を否定します。

オペランド

オペランド 種類 Description
boolValue boolean 否定されるブール値。

戻り値

初期値を否定し、ブール値を返します。 初期値が true の場合は、false が返されます。

not 演算子は、 値を否定します。 値は括弧で囲むことができます。

param initTrue bool = true
param initFalse bool = false

output startedTrue bool = !(initTrue)
output startedFalse bool = !initFalse

出力例を次に示します。

名前 Type
startedTrue boolean false
startedFalse boolean true

Coalesce ??

operand1 ?? operand2

オペランドから最初の null 以外の値を返します。

オペランド

オペランド 種類 説明
operand1 文字列、整数、ブール値、オブジェクト、配列 null をテストする値。
operand2 文字列、整数、ブール値、オブジェクト、配列 null をテストする値。
追加のオペランド 文字列、整数、ブール値、オブジェクト、配列 null をテストする値。

戻り値

最初の null 以外の値を返します。 空の文字列、空の配列、および空のオブジェクトは null ではなく、<空の>値が返されます。

出力ステートメントは、null 以外の値を返します。 出力の型は、比較の型と一致する必要があります。一致しない場合、エラーが生成されます。

param myObject object = {
  isnull1: null
  isnull2: null
  string: 'demoString'
  emptystr: ''
  integer: 10
  }

output nonNullStr string = myObject.isnull1 ?? myObject.string ?? myObject.isnull2
output nonNullInt int = myObject.isnull1 ?? myObject.integer ?? myObject.isnull2
output nonNullEmpty string = myObject.isnull1 ?? myObject.emptystr ?? myObject.string ?? myObject.isnull2

出力例を次に示します。

名前 Type
nonNullStr string demoString
nonNullInt INT 10
nonNullEmpty string <empty>

Conditional expression ? :

condition ? true-value : false-value

条件を評価し、条件が true か false かに関係なく値を返します。

オペランド

オペランド 種類 Description
condition boolean true または false として評価する条件。
true-value 文字列、整数、ブール値、オブジェクト、配列 条件が true の場合の値。
false-value 文字列、整数、ブール値、オブジェクト、配列 条件が false の場合の値。

この例では、パラメーターの初期値を評価し、条件が true か false かに関係なく値を返します。

param initValue bool = true

output outValue string = initValue ? 'true value' : 'false value'

出力例を次に示します。

名前 Type
outValue string true 値

次のステップ