Hello,
I am a programming newbie. This question will show my newbiness! Here
'goes:
I just learned something about C++. Apparently, any modern C++ compiler
statically solves certain problems at compile time. If I have a FOR loop
that looks like this (pseudo code):
FOR(i=1; i<= iterations; i ++)
result = sqrt(i)
END FOR
Print result
…then the compiler would simply calculate Result = sqrt(iterations)
and skip all the loops. The FOR loops won’t even be executed since the
compiler knows it only needs to return the result of the last iteration.
My question to you is, "What would Ruby do in a situation like this?
Here is the Ruby code I used (I know, not very pretty, and yes,
contrived):
def for_loop(iterations)
i = 0
result = 0
while i <= iterations do
result = i**0.5
i += 1
end
puts result
end
I’m pretty sure the value of “result” is being replaced on every
loop. Can you please confirm this?
Thank you very much! I love this forum, BTW.
Jason
PS - I’m doing this because I’m running benchmark comparisons to some
numerical methods software and I have to make sure I’m comparing apples
to apples.