Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding a Julia statement comprising == && increment and continue

Tags:

continue

julia

I am trying to translate some Julia routine into C (C#, C++). I fail to understand the particular line

n == n0 && (n+=1; continue)

in the following loop.

ret1, ret2, n = 1, 0, 0
while n < m
    n == n0 && (n+=1; continue)
    ret1 *= z+n
    ret2 += 1/(z+n)
    n += 1
end

How is the increment function before exiting the loop.

like image 881
Douar Gwenn Avatar asked Oct 28 '25 14:10

Douar Gwenn


1 Answers

In this code, (n+=1; continue) is a compound expression, similar to using a block. The && has short-circuit evaluation semantics, where the right-hand-side is only evaluated if the left-hand-side is true. It would be much clearer and better style, imo, to write this code like this:

ret1, ret2, n = 1, 0, 0
while n < m
    if n == n0
        n += 1
        continue
    end
    ret1 *= z+n
    ret2 += 1/(z+n)
    n += 1
end

Better still, would be to invert the condition and only evaulate the body statements if on the later iterations:

ret1, ret2, n = 1, 0, 0
while n < m
    if n != n0
        ret1 *= z+n
        ret2 += 1/(z+n)
    end
    n += 1
end
like image 85
StefanKarpinski Avatar answered Oct 30 '25 13:10

StefanKarpinski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!