# How to get the sum of a variables value in a For loop?

## Recommended Posts

Hi,

I'm wondering if anybody knows how to solve this coding problem I'm trying to figure out.

```;a little text editing script where I send the caret down a random amount from 1 To 10
;and then comment out a line (Ctrl+Q in Scite)
;...but I want the script to exit if the caret has gone down a total of 78 lines

For \$i =1 To 25
\$x = Random(1, 10, 1)
;Send("{Down "& \$x &"}")
;Send("^q")

;here I want \$n to be the value of the accumulating sum of all the the \$x values
;that is, \$x + \$x + \$x + \$x, etc. adding all the values of \$x together each time
;the For loop goes around...so that when the sum of all the \$x's reaches 78, the script exits
;any ideas? Thank you.

If \$n > 78 Then Exit
Next```

Thanks for any ideas,

frew

##### Share on other sites

You mean something like this?

```\$xSum = 0
For \$i = 1 To 25
\$x = Random(1, 10, 1)
\$xSum += \$x

If \$xSum >= 78 Then Exit

Next```

--- TTFN

##### Share on other sites

Wow, thanks so much Zinthose!

I just couldn't quite get that, but I knew there must be a way to do that in a

loop. By the way, this version allows to go past 78, which is not what I want:

```\$xSum = 0
For \$i = 1 To 25
\$x = Random(1, 10, 1)
\$xSum += \$x
;do stuff
MsgBox(0, '', \$xSum)
If \$xSum >= 78 Then Exit
Next```

Whereas this version is just what I want...ie all the caret going down and putting

in the comments (Ctrl+Q) stops if \$xSum has reached 78

```\$xSum = 0
For \$i = 1 To 25
\$x = Random(1, 10, 1)
\$xSum += \$x
If \$xSum >= 78 Then Exit
MsgBox(0, '', \$xSum)
;do stuff
Next```

Thanks again so much, that really helps a lot!

frew

Edited by frew

## Create an account

Register a new account

×

• Wiki

• Back

• Git