0

I'm currently stuck on an assignment for my OS class dealing with batch scripting. The main issue I'm having is writing a functional for loop in the program that accurately goes through a range from 1 to 100 and calculates the sum via the arithmetic expression %%variable mod 5 + 3.

The current results I've been getting ranges from either getting just a 0 as an answer when running, or numbers which increment by 3, for example, 3 then 6 then 9 etc.

Here is the appropriate section of my code:

for /l %%i in (1, 1, 100) do (
set /a result = %%i %% 5
set /a calctotal = %result% + 3
)
set /a average = %calctotal%/100
echo %average%
pause
4
  • 2
    Possible duplicate of Windows Batch file processing - Loops Commented Mar 16, 2016 at 19:47
  • @SomethingDark: I've seen this very problem before - a few days ago - with the same poorly-expressed calculation. Regrettably, your nominated "duplicate" is IMHO overly complex for a beginner - the ; are unnecessary for instance - and you actually don't need delayedexpansion (in this instance.) Commented Mar 16, 2016 at 20:18
  • @Magoo - %calctotal% is being initialized inside of a code block; how do you not need delayed expansion? Commented Mar 16, 2016 at 20:24
  • 1
    @SomethingDark : read my solution & try it! The set /a syntax works on the run-time value, not the parse-time value... Commented Mar 16, 2016 at 20:27

1 Answer 1

1
SET /a calctotal=0
for /l %%i in (1, 1, 100) do (
 set /a result = %%i %% 5
 set /a calctotal += result + 3
)
set /a average = %calctotal%/100
echo %average%

The problem to me is the interpretation of "calculate the sum via the arithmetic expression %%variable mod 5 + 3"

Now does this mean the sum of (%%variable mod 5 + 3) or (the sum of (%%variable) mod 5 + 3 ??

Anyhow, there are some problems with your code (well, if there wasn't, you'd not be asking, would you?)

First, in a 'block' (parenthesised series of lines) any %var% is replaced by the value of var at the time the block was encountered, and does not change as the block is executed. The classic way to deal with this is to invoke delayedexpansion.

Hence, in your code, you were executing set /a calctotal = + 3 100 times, as %result% would have been replaced by its value at the start of the loop (presumably undefined, hence nothing)

Next problem is that you are attempting to set calctotal to the latest value of result - you are not accumulating the values.

however in this case, you can use the set /a syntax, which works on the current value of the variables - provided you don't invoke them as %var%.

set /a calctotal += result + 3 uses += to accumulate the values. You could also use set /a calctotal = calctotal + result + 3 which may be more obvious.

I set calctotal to zero before starting just in case it's aready set.

And no doubt you could remove the need for result by using set /a calctotal += %%i %% 5 + 3

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.