0

I was making a code to find square roots for practice with basic cmd coding. However, when I type in a negative number, or an imperfect square, then it goes in an infinite loop. I know how to fix it, but I need to be able to use inequalities( Less than, greater than). (x is the number I have, and y is it's square root) That way I can say:

  • if %x% (is less than) 0 (Don't operate)
  • if %y% (is greater than) %x% (Don't operate)

or I could create another variable that is everything less(z)/greater(n) than x:

  • if %x% == %z% (Don't operate)
  • if %x% == %n% (Don't operate)

Please help me. I can't seem to find an answer.

marapet
  • 54,856
  • 12
  • 170
  • 184
user1377474
  • 1
  • 1
  • 1

3 Answers3

3

You could try reading the documentation for the IF statement - type HELP IF or IF /? from the command line. About halfway through it describes the command extensions that support inequalities, such as LSS for less than.

if 0 lss 1 (echo 0 is less than 1) else (echo 0 is not less than 1)
dbenham
  • 127,446
  • 28
  • 251
  • 390
2

+1 to @dbenham's answer, here's the excerpt of the IF documentation with all the comparison operators:

If Command Extensions are enabled IF changes as follows:

IF [/I] string1 compare-op string2 command
IF CMDEXTVERSION number command
IF DEFINED variable command

where compare-op may be one of:

EQU - equal
NEQ - not equal
LSS - less than
LEQ - less than or equal
GTR - greater than
GEQ - greater than or equal
Community
  • 1
  • 1
marapet
  • 54,856
  • 12
  • 170
  • 184
  • 2
    I intentionally didn't include the excerpt because I wanted to encourage the OP to use the HELP system :-) – dbenham May 06 '12 at 12:51
  • That's why your answer is the better one ... :-) There's still a lot of information in the documentation one ought to read to really understand the possibilities. So if the OP is really in practice mode, she will explore the doc, and all other people stumbling onto this question just looking for a quick answer have the list available right here. – marapet May 06 '12 at 13:18
0

Well in code this would look something like this

:: this is Regional settings dependant so tweak this according 
:: your current Windows regional settings
for /f "tokens=1-3 delims=,: " %%a in ('echo %time%') do set hhmmsss=%%a%%b%%c

if /i %hhmmsss% LSS 95959 SET hhmmsss=0%hhmmsss%
Yordan Georgiev
  • 5,114
  • 1
  • 56
  • 53