How to modify loop variable in ruby? -
for in (0..5) if(i==0) i=4 end puts end
in above program excepted output - 4 5
but instead - 4 1 2 3 4 5
so conclude loop variable isnt changing. how can change it? can tell me?
actually, in program need save current state of loop , retrive later on next start program resumes same point left.
the way you'll able modify loop variable use while
loop:
x = (2..7).to_a = 0 while (i < x.length) = 4 if == 0 puts x[i] = + 1 end
output:
6 7
in for
loop, you've discovered, can modify loop variable , value hold iteration of loop. on next iteration, retrieve next element range specified modified value overwritten.
Comments
Post a Comment