Sunday 23 June 2019

Batch: Evaluating Mathematical Expressions


‘set /a expression’ is used to evaluate mathematical expressions.

Hello.bat
@echo off

set /A sum = 10 + 20
set /A subtraction = 10 - 20
set /A mul = 10 * 20
set /A div = 10 / 20

echo Sum of 10 and 20 is %sum%
echo Subtraction of 10 and 20 is %subtraction%
echo Multiplication of 10 and 20 is %mul%
echo Division of 10 and 20 is %div%

Output
C:\Users\Public>Hello
Sum of 10 and 20 is 30
Subtraction of 10 and 20 is -10
Multiplication of 10 and 20 is 200
Division of 10 and 20 is 0

You can use % operator to evaluate the variable values.


Hello.bat
@echo off

set a = 10
set b = 20

set /A sum = %a%+%b%
set /A subtraction = %a% - %b%
set /A mul = %a% * %b%
set /A div = %a% / %b%

echo Sum of %a% and %b% is %sum%
echo Subtraction of %a% and %b% is %subtraction%
echo Multiplication of %a% and %b% is %mul%
echo Division of %a% and %b% is %div%

Output
C:\Users\Public>Hello
Sum of 5 and 10 is 15
Subtraction of 5 and 10 is -5
Multiplication of 5 and 10 is 50
Division of 5 and 10 is 0



Previous                                                 Next                                                 Home

No comments:

Post a Comment