Batch Script Logical operators
Logical operators
Logical operators are used to evaluate Boolean expressions.
The batch language has a full set of Boolean logical operators such as AND, OR, XOR, but only for binary numbers. There are also no values for TRUE or FALSE.
note
The only logical operator available for conditions is the NOT operator.
So, if statements do not support logical operators.
The following table lists the available logical operators.
| Operator | Description |
|---|---|
AND | This is the logical and operator |
OR | This is the logical or operator |
NOT | This is the logical not operator |
Implement AND operator to use in if conditions
Logical AND is implemented with nested conditions:
@echo off
set /A a=5
set /A b=10
if %a% geq 5 (
if %b% leq 10 (
echo "True Result"
)
)
"True Result"
Implement OR operator to use in if conditions
Logical OR is implemented with a separate variable:
OR implementation
@echo off
set /A a=5
set /A b=10
set result=false
if %a% == 1 set result=true
if %b% == 1 set result=true
if "%result%" == "true" (
echo "True result"
)
"True Result"
note
You are essentially using an additional variable to accumulate your boolean result over multiple if statements.