Author Options:

Help me at "for" command (Batch/CMD) Answered

Can anyone help me? I've got a problem with the "for" command. I can't include decimals (like 0,1).
Here's the code:

@echo off
echo. & echo.
echo Let's play a game! It's called
echo NUMBER! Type in any number
echo between 1 and 10:
set/p "choice=>"
for /L %%G in (1,1,10) do if %choice%==%%G goto Add
echo Choose a number between 1 and 10!
ping localhost -n >nul 2
goto Start
if %choice%==10 goto Tie
set one=1
set /a result=%choice%+%one%
echo I choose %result%!
ping localhost -n >nul 3
goto Loser
echo I choose 10, too!
ping localhost -n >nul 3
echo -=-=-=-=-=-TIE-=-=-=-=-=-
echo Press ENTER
goto Again
echo. & echo.
echo %result% is bigger than %choice%.
echo Sorry, you lose.
echo Press ENTER
goto Again
echo. & echo.
echo Wanna play again? (y/n)
set/p "choice1=>"
if %choice1%==y goto Start
if %choice1%==n goto End
echo Choose y or n!
ping localhost -n >nul 3
goto Again

The problem is at line 9: "for /L %%G in (1,1,10) do if %choice%==%%G goto Add"
With (1,1,10) the sysem can only recognise Integers.
So, if you know any way to solve that problem or to make the code more compact, please tell me :P
PS: I'm from Brazil, so I'm sorry if there are any grammar mistakes.


Thank you mh76dk and kelseymh. You've helped a lot!


5 years ago

I dont know batch programming specifically, but you can fake a decimal pointed loop by nesting a 10ths (or hundreds) loop inside the integers loop

here is an example in pseudocode:

for a = 1 to 10 do
for b = 1 to 10 do
print a + "," + b
endfor b
endfor a

You might need to do additional checking on first a then b if you want to compare two "pairs"

This is certainly not optimal, it is prone to errors and likely wont work in many situations - but for a simple 1/10 counter loop, and basic comparisons it should be ok

You wrote your floating-point (decimal) number in European style (0,1 = 1/10). Programming languages tend to be very U.S.-centric :-/, and floating point numbers are written using the full-stop, not the comma: 1/10 = 0.1, for example.

However, only integers are supported for numbers in batch. You can't do a floating point loop, and you can't set floating point values. If you tried, for example, A = 1/10, you'll discover that A is set to 0.