-1

I'm going through some golang tutorials, and I came across this for loop:

for n := 0; n <= 5; n++ {
    if n%2 == 0 {
        continue
    }
    fmt.Println(n)
}

I'm confused by the n%2 statement.

The output of this is:

1
3
5

It looks like these are not multiples of 2, but I'm not understanding the == 0 part of the statement if that's the case? Is there a resource that talks about this operation, or something I should look up?

rpivovar
  • 3,150
  • 13
  • 41
  • 79
  • 2
    You need a better book / tutorial. – KevinDTimm Aug 28 '17 at 20:50
  • 1
    I think the tutorial is operating under the assumption that the student has some knowledge of all operators - no fault to the tutorial itself. I just don't know what I'm doing, and a Google search with whatever arrangement of keywords I was trying wasn't turning up anything. Stack Overflow has no truck for basic questions - I've posted dozens of questions, and I know this, but it's an invaluable resource for newbies. I learned something and lost some internet points in the process. – rpivovar Aug 29 '17 at 04:11
  • 1
    Just an FYI, typing your subject line into google the first response that wasn't your question was `https://golang.org/ref/spec` which describes all of the operators of the language. Granted, it was the 6th occurrence of the character `%` that defined it as `remainder`, a euphemism for `modulo division`. So, a little more diligence saves you -10 in reputation :) – KevinDTimm Aug 29 '17 at 13:40
  • 1
    I didn't know I was even looking at an operator, so even though that information was valuable to me, I wasn't aware it was valuable information. The only reason I said "operator" in my previous reply was because of the good answers I got on this post. It was -20 reputation, but I learned more making the post than doing the search. – rpivovar Aug 29 '17 at 19:33
  • Google searching before making a post here is common courtesy, or it should be, anyway. I didn't find the answer that way, or if I found it, I did not know I was looking at it. It's an internet catch-22, where you post in an educational forum (where questions are meant to be asked and answered), and someone says "are you really that stupid?", and you have to admit, "Yes, I'm that stupid. Please help me." Lo and behold, the site works, a question was asked, and answers were provided. 20 points, vapor reputation, were lost. – rpivovar Aug 29 '17 at 19:50

2 Answers2

12

This is called the remainder operator, it returns the remainder of a division operation. Hence X % Y == 0 will be true when X can be evenly divided by Y.

This operator and % to represent it is common in many languages.

See related question: Understanding The Modulus Operator %

Cameron Roberts
  • 7,127
  • 1
  • 20
  • 32
2

It's the remainder/modulo-operator. This returns the rest of the division with the given number: https://en.wikipedia.org/wiki/Modulo_operation

This code fragment calculates all uneven numbers.

Piwo
  • 1,347
  • 1
  • 12
  • 19