Jump to content
Sign in to follow this  
frew

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

Recommended Posts

frew

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 this post


Link to post
Share on other sites
Zinthose

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 this post


Link to post
Share on other sites
frew

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×