Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAlp Eren Can
25,230 PointsRuby For Loops - Variable Modification
In Ruby Foundations course and in the quiz following the Loops badge it asks: In a for loop, will modifications to variables (addition, substraction, etc) passed in to the body of the loop affect those variables in the original?
My answer was Yes but the correct answer appears to be No. If this is not by mistake can somebody explain why?
2 Answers
Maciej Czuchnowski
36,441 PointsI could be wrong but they could mean that, for example, the i
variable in for i in 1...5 do
is not influenced by modifications made inside the loop. So in the first run, i
will be 1. Let's say that inside the loop you do i = i + 5. It will be 6 UNTIL the next run starts, then it will become 2, so it was not influenced by the addition inside the body. Something will be done to it again, but in the next run it becomes 3, it follows the order set in the first line. But they could have made the question clearer...
Alp Eren Can
25,230 PointsThat makes sense, thanks!
What I thought was declaring variable i before the loop and its value not changing when the loop is done with it.