calculations with integer variables?

  • Thread starter Thread starter BigMan
  • Start date Start date
B

BigMan

I would like the variable %I to iterate from 0 to %C-1 rather than from 1 to
%C. Note that I cannot change the range of %C to0-9!

for /L %%C in (1,1,10) do (
for /L %%I in (1,1,%%C) do (
echo %%C %%I
)
)

What should I do?
 
I would like the variable %I to iterate from 0 to %C-1 rather than from 1 to
%C. Note that I cannot change the range of %C to0-9!

for /L %%C in (1,1,10) do (
for /L %%I in (1,1,%%C) do (
echo %%C %%I
)
)

What should I do?

@echo off
setlocal enabledelayedexpansion
for /L %%C in (1,1,10) do (
set /a cm1=%%C - 1
for /L %%I in (0,1,!cm1!) do (
echo %%C %%I
)
)
endlocal


Jerold Schulman
Windows Server MVP
JSI, Inc.
http://www.jsiinc.com
 
BigMan said:
I would like the variable %I to iterate from 0 to %C-1 rather than from 1 to
%C. Note that I cannot change the range of %C to0-9!

for /L %%C in (1,1,10) do (
for /L %%I in (1,1,%%C) do (
echo %%C %%I
)
)
Requires w2k or newer for delayedexpansion:

@echo off&setlocal EnableDelayedExpansion
for /L %%C in (1 1 10) do (
set /A Cm1=%%C-1
for /L %%I in (0 1 !Cm1!) do (
echo %%C %%I
)
echo.
)

HTH
 
Jerold said:
@echo off
setlocal enabledelayedexpansion
for /L %%C in (1,1,10) do (
set /a cm1=%%C - 1
for /L %%I in (0,1,!cm1!) do (
echo %%C %%I
)
)
endlocal
I swear: *I hadn't seen your solution before posting mine* ;-)
 
Jerold said:
I believe you.
cm1 just seemed natural.

Sigh, I'm relieved to hear that. Set /a doesn't like var names with
operators inside, so c-1 doesn't work and and c_1 isn't that clear.
 
Back
Top