Maikeru Konare
Maikeru Konare

Reputation: 31

Number Comparisons in Batch Code

I'm having difficulty with number comparisons like <, >, and == in my Batch code. What Im doing is generating a random number and using that answer to do something, this is what I've written:

set rand=%random%
set rand=%rand:~1,1% 
If %rand%==9 goto nine
If %rand%>5 goto above 5
If %rand%>1 goto above 1
If %rand%==0 goto zero

And the code just closes when I run it. I tried putting space between the two objects being compared and the inequality but it still doesn't work.

Remember, this is Batch code on Windows.

Upvotes: 3

Views: 20063

Answers (2)

EDG
EDG

Reputation: 163

For the if commands use these keys instead of the equal and greater than symbols:

EQU - equal
NEQ - not equal
LSS - less than
LEQ - less than or equal
GTR - greater than
GEQ - greater than or equal

But I would suggest using == instead of EQU. It is easier to type.

Upvotes: 10

PA.
PA.

Reputation: 29339

read HELP IF and then try this

if %rand% equ 9 goto nine
if %rand% gtr 5 goto above5
goto below5

note that the label names cannot contain blanks

as an additional bonus, read HELP SET and change the way you try to obtain the random 0 to 9 number into

set /a rand=%random% %% 10

Upvotes: 5

Related Questions