# How is this code coming up with the following result?

``````function setup()
total=0
a=30
b=50
for i=a,b do
total=total+i
end
print(total)
end
``````

Studying @Ignatz LUA beginner. I just want to know how this is coming up with the result of 840?

I am reading this code as…
for i (the variable) is set to a,b (respectively… 30,50)

following line states, the total should be added (+) to i. ok, I get that…

But what math is it running for a,b prior to adding it’s total to +i
I do not see any math operands. (*, /,… etc)

Your Output is 840.

I’m not sure what you are expecting, but I takes the current index of the for loop (30, 31, 32, …, 48, 49, 50) and running the code with I being each of the options. Your code is therefore:

``````total = 0 [+ 30] [+ 31] [+ 32] [+ ...] [+ 49] [+ 50]
``````

Which makes sense to be 840.

Thank you @Anatoly

Truth is, I was not expecting anything, since I did not know what was going on - I was seeking. My true goal was me REALLY trying to figure out the process, of how it went about the calculations, to get to that number.

Now that you taught me what it is doing - thank goodness. I was meeting madness in my mind, trying to figure out how it was going about getting 840.

Now I understand that…

``````i=a,b do
total=total+i
``````

Is indeed adding along the way, but not the way I thought. I was thinking 30+50=70

Instead, the code is really going through each integer, and then adding them into the following number in the sequence, until it reaches 50.
(30 + 31+ 32 + 33… all the way up to 50)

Thank You!

Some links I’d suggest you for reading about how to code:

Other links:

I’ll also be trying to create a community wiki; but currently I have much to study for school:

In ca. one week I’ll likely have time to update the wiki and add much more content.

Thanks for the links @Anatoly

But those links confusing to me (regarding LUA) which is what I am learning at the moment with a follow along guide and a Udemy course I picked up.

The code block from my opening post, was taken from my follow along @ignatz guide.Which is literally a step by step, follow along, as he codes, he speaks to you as to what he’s doing and then he has you do it. Perfect speed for my noob ass.

That code block I asked for help above in my opening post. Was from me being encouraged in his guide to mess around with the code and see what I can learn.

Which is what I did, I placed my own numerical values into the variables. When I noticed I wasn’t getting the results. I was expecting, I knew I was not understanding “how” the code is going “about” adding those numbers.

I was aware + increments the given value. I was NOT aware that the code was adding every number pair it incremented as it it moved up to the next pair of numbers, to get it’s result.

Now that you pointed that out, and that’s all I needed to know - and I learned something from messing around.

What I will NOT be doing is reading those confusing LUA links you sent me. lol.

Though I appreciate it, I’m good with my current learning methods (I know way more - but still a noob). My next step will be a coding bootcamp eventually. For now, doing what I can, with the time I have.

@tactfulgamer The best way to learn is to take existing code and modify it to see what happens. Then try to understand what your changes did. After that, then you ask questions here about what you don’t understand. The sooner you ask, the sooner you get a response and the faster you learn. There are plenty of people here that will give you help.

Yep… That’s exactly what I did.

As I described above in my comment. Appreciate the support.