逻辑运算符计算布尔值、返回非 null 值或计算条件表达式。 若要运行这些示例,请使用 Azure CLI 或 Azure PowerShell 来部署 Bicep 文件。
operand1 && operand2
确定这两个值是否为 true。
操作数 | 类型 | DESCRIPTION |
---|---|---|
operand1 |
布尔 | 检查 true 的第一个值。 |
operand2 |
布尔 | 要检查是否为 true 的第二个值。 |
更多作数 | 布尔 | 可以包含更多作数。 |
True
如果两个值均为 true,则返回其他 false
值。
计算一组参数值和一组表达式。
param operand1 bool = true
param operand2 bool = true
output andResultParm bool = operand1 && operand2
output andResultExp bool = 10 >= 10 && 5 > 2
示例中的输出:
名称 | 类型 | 价值 |
---|---|---|
andResultParm |
布尔 | 是 |
andResultExp |
布尔 | 是 |
若要避免 Bicep 对象的语言表达式属性“foo”不存在异常,可以使用 And 逻辑运算符,如以下示例所示:
param objectToTest object = {
one: 1
two: 2
three: 3
}
output bar bool = contains(objectToTest, 'four') && objectToTest.four == 4
operand1 || operand2
确定任一值是否为 true。
操作数 | 类型 | DESCRIPTION |
---|---|---|
operand1 |
布尔 | 检查 true 的第一个值。 |
operand2 |
布尔 | 要检查是否为 true 的第二个值。 |
更多作数 | 布尔 | 可以包含更多作数。 |
True
如果任一值为 true,则返回其他 false
值。
计算一组参数值和一组表达式。
param operand1 bool = true
param operand2 bool = false
output orResultParm bool = operand1 || operand2
output orResultExp bool = 10 >= 10 || 5 < 2
示例中的输出:
名称 | 类型 | 价值 |
---|---|---|
orResultParm |
布尔 | 是 |
orResultExp |
布尔 | 是 |
为了避免 语言表达式属性数组索引“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
!boolValue
否定布尔值。
操作数 | 类型 | DESCRIPTION |
---|---|---|
boolValue |
布尔 | 已否定的布尔值。 |
否定初始值并返回布尔值。 如果初始值为 true
,则 false
返回。
运算符 not
对值求反。 这些值可以用括号包装。
param initTrue bool = true
param initFalse bool = false
output startedTrue bool = !(initTrue)
output startedFalse bool = !initFalse
示例中的输出:
名称 | 类型 | 价值 |
---|---|---|
startedTrue |
布尔 | 假 |
startedFalse |
布尔 | 是 |
operand1 ?? operand2
从作数返回第一个非 null 值。
操作数 | 类型 | DESCRIPTION |
---|---|---|
operand1 |
string, integer, boolean, object, array | 要测试 null 的值。 |
operand2 |
string, integer, boolean, object, array | 要测试 null 的值。 |
更多作数 | string, integer, boolean, object, array | 要测试 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
示例中的输出:
名称 | 类型 | 价值 |
---|---|---|
nonNullStr |
字符串 | demoString |
nonNullInt |
整数 (int) | 10 |
nonNullEmpty |
字符串 | <空> |
condition ? true-value : false-value
计算条件并返回一个值,该值是条件为 true 还是 false。
操作数 | 类型 | DESCRIPTION |
---|---|---|
condition |
布尔 | 要计算为 true 或 false 的条件。 |
true-value |
string, integer, boolean, object, array | 条件为 true 时的值。 |
false-value |
string, integer, boolean, object, array | 条件为 false 时的值。 |
此示例计算参数的初始值,并返回条件为 true 还是 false 的值。
param initValue bool = true
output outValue string = initValue ? 'true value' : 'false value'
示例中的输出:
名称 | 类型 | 价值 |
---|---|---|
outValue |
字符串 | 真值 |
- 若要创建 Bicep 文件,请参阅快速入门:使用 Visual Studio Code 创建 Bicep 文件。
- 有关如何解决 Bicep 类型错误的信息,请参阅 Bicep 的 Any 函数。
- 若要比较 Bicep 和 JSON 的语法,请参阅比较模板的 JSON 和 Bicep。
- 有关 Bicep 函数的示例,请参阅 Bicep 函数。