Search code examples
pythonfor-looprangejuliaskip

Is there a skip parameter in Julia's range?


In Python, we could iterate using a for-loop and skip the indices using the skip parameter as such:

max_num, jump = 100, 10
for i in range(0, max_num, jump):
    print (i)

I could achieve the same with a while loop by doing this:

max_num, jump = 100, 10
i = 0
while i < max_num
    print(i)
    i+=jump
end

And using the same i+=jump syntax shown below in the for-loop doesn't skip the index:

for i in range(0,max_num)
    print(i)
    i+=jump
end

Within a for-loop is "skipping" possible? If so, how?


Solution

  • You can just do

    max_num, step = 100, 10
    
    for i in 0:step:max_num
        println(i)
    end
    

    Using range(), you do not specify max_num, but the desired number of iterations. So 0:step:max_num equals range(0, step, max_num/step).