for i in (0..5)
if(i==0)
i=4
end
puts i
end
In the above program I excepted the output as – 4 5
But instead it is – 4 1 2 3 4 5
So I conclude that loop variable isnt changing. How can change it? Can anyone tell me?
Actually, In my program I need to save the current state of loop and retrive later so that on the next start program resumes from the same point where it was left.
The only way you’ll be able to modify the loop variable is to use a
whileloop:Output:
In a
forloop, as you’ve discovered, you can modify the loop variable and that value will hold for that iteration of the loop. On the next iteration, it will retrieve the next element from the range you specified so your modified value will essentially be overwritten.