Help with evaluating a condition

Brand new Gopher…
in…
if i%2 == 0
continue

I understand it as reading 'while the value of 'i’s remainder when doing i/2 is equal to 0; then continue

is that correct, and more importantly for me… how is i%2 == 0 ever correct except division by zero? then how can the condition be met where this for condition allows the sequential printing of odd only nums?

I appreciate any insight.

Imagine you were back in math class and you had to simplify an improper fraction into a mixed number. That is you had to turn a fraction like 7/3 into 2 1/3.

The remainder operation basically gives you the numerator to the fraction part of the the answer. Eg in integer math we have:

7/3 = 2
7%3= 1 (the 1 in 1/3 above)

This is always true for odd numbers with X%2 because there will always be a 1/2 fraction when you simplify any odd number over 2. Eg 5/2 = 2 1/2. 7/2 = 3 1/2.

I’m not 100% sure what you are doing with the continue in your code. Those are typically used to skip an iteration of a loop, but without more context (more code) I can’t say if you are using it correctly or not.

2 Likes

Thanks Jon…
I get the math mechanics, what eluded me was the evaluation ==0.
the continue drops to a simple
fmt.Println(i)
with a conditional check to stop the loop with a 'break’
so to sum up; i%2 == 0 would always be true for even nums, and only even nums; while false for odds…
is that the correct view?
Jon (jbarkls)

Can you post the full code for the loop? Not trying to be unhelpful but without seeing code I am not completely sure what you are saying.

Yupp.

x%y == 0 will always be true for any x that is evenly divisible by y.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.