Is there a way to offset the value that has been passed in the code block.
For example
C# – This will always print 5
for (int i = 0; i < 10; i++)
{
i=5
Console.WriteLine(i);
}
Ruby – This will print 5 for only 10 times.
10.times do |i|
i = 5
puts i
end
Is there a way to get |i| to get back to 5?
and another question. How can you make .times block skip(not increasing by 1 all the time)
No, you can’t reset the counter of
times,uptoetc. from within the block.You can use
redoto restart the current iteration of the loop, which in this case will have a similar effect:This will print 5 forever (though take note, that the initial value of
iwill still be0at each iteration).If you need more control than this, you need to use a
whileloop. Though I’d advice that in most cases where you think you need this, you actually don’t and you’re just approaching your problem from the wrong angle.