The following table lists the available arithmetic operators.
| Operator | Description | Example |
|---|---|---|
+ |
Addition of two operands | 1 + 2 will give 3 |
− |
Subtracts second operand from the first | 2 − 1 will give 1 |
* |
Multiplication of both operands | 2 * 2 will give 4 |
/ |
Division of the numerator by the denominator | 3 / 2 will give 1.5 |
% |
Modulus operator and remainder of after an integer/float division | 3 % 2 will give 1 |
Example
The following code snippet shows how the various operators can be used.
@echo off
set /A a=5
set /A b=10
set /A c= %a%+%b%
echo %c%
set /A c= %a%-%b%
echo %c%
set /A c= %b%*%a%
echo %c%
set /A c=%b%/%a%
echo %c%
set /A c=%b% %% %a%
echo %c%15
-5
50
2
0Precedence of Arithmetic operators
The precedence of arithmetic operators is in the following order:
/ * % + -Let's clarify this with the following example:
@echo off
set /A s = (20 - 5) * 2 + 8 / 2
echo s = %s%s = 34Explanation:
(20 - 5)gets the highest priority and calculated first giving 15.- Then division operator
/gets next priority thus8 / 2is evaluated giving4. - After this multiplication operator
*gets next priority thus15 * 2yields30. - Finally, addition operator
+performs addition operation30 + 4giving final result34.