6

I hate to ask this question on here, but I have searched both SO and Google to no success. I have seen in many places statements such as while(var != -1) and other statements, often loops, containing some sort of reference to -1. Is there a certain meaning to the use of -1, or is it just used as giving an integer representation of a boolean, or something like that? I have mainly seen this in C# programming if that is any help.

Daniel Underwood
  • 2,191
  • 2
  • 22
  • 48
  • 11
    `-1` means negative one. – p.s.w.g Jun 12 '13 at 20:08
  • 1
    this is a well-formed question. A tip of the hat to you. – Jonesopolis Jun 12 '13 at 20:11
  • 1
    Generally is used to advice that an error ocurred in a function – DGomez Jun 12 '13 at 20:14
  • 3
    Why has this question been closed? It looks like a good and easy to understand question. Nominating for reopening. – Sergey Kalinichenko Jun 12 '13 at 20:15
  • 2
    imho this is a well formed question and totally valid, the op may be confussed and perhaps needs to do some homework but I can't say I agree with the reason why it was closed, perhaps SO is beginning to have too many people with rights they cannot properly comprehend or manage responsibly – Jason Jun 12 '13 at 20:19
  • 1
    Can you post sample code? My guess code is `while(reverseIndex != - 1) {... reverseIndex-- }` for iterating sequence backward, usually for removing items from the list. – Alexei Levenkov Jun 12 '13 at 20:22
  • 1
    _and other statements, often loops, containing some sort of reference to -1_ : this is not really a well formed or well documented question. – H H Jun 12 '13 at 20:29
  • I apologize for the lack of a firm code example and description. I didn't see one right before I asked the question, and could not find what I was thinking of when I did. I will be sure to add an example as soon as I see another example of this while I'm coding. – Daniel Underwood Jun 12 '13 at 20:32

3 Answers3

7

in C# -1 is just negative one. They're comparing a number against a number, seeing if it is indeed equal to negative one.

It's not uncommon to have an integer field that should only have positive values (for example, when representing an index in a list) and in such cases -1 is sometimes used to represent "not a valid value", for example, there is no item, and hence no index. They use -1 because an int is not nullable; they cannot assign null.

In theory this is probably a bad practice; it's using a "magic value" to mean something more than it really should. Ideally if "there is not valid" is a valid thing for the variable to represent it should be a nullable integer (int? or Nullable<int>) but this is an old convention (carried over from other languages without a feature for nullable ints) so it's hard to eliminate entirely.

Servy
  • 202,030
  • 26
  • 332
  • 449
  • I do believe this is what I was thinking about. Both the element that does not exist and a null integer. From what I remember, thinking of it in that way causes the code to make more sense. Thanks! – Daniel Underwood Jun 12 '13 at 20:33
  • +1. I'd say that the only case when -1 would be ok directly and not represents "magic value" is reverse iteration boundary for an indexed collection (like `for(index=collection.Count; index != -1; index--)...` ). Also in most cases LINQ constructs would be safer/better constructs for such loop. – Alexei Levenkov Jun 12 '13 at 21:05
  • @AlexeiLevenkov In that case it's not a "magic value". -1 isn't a special value used to represent anything, it's just the first value that's not valid when decrimenting. That said, using `index >= 0` instead can be clearer, while being mathematically equal. – Servy Jun 12 '13 at 21:07
5

Nothing special about it. It's just that in most frameworks and libraries, functions or methods that return an index of an element in a collection will return -1 when whatever you're looking for isn't in the collection.

For example, the index of the character b in the string foo would be -1 in JavaScript, .NET and, as far as I remember, Java as well.

So many devs have burned a rom in their minds saying that -1 is the index for not found items. Now you know why.

Geeky Guy
  • 9,229
  • 4
  • 42
  • 62
3

If you know that an int should always contain positive value (for instance an item count or an index in a list, -1 can be a kind of "reserved value", so you would for instance assign the count to -1 and as long as it's -1, you know no real value has been put in there, a bit like a "null"

other than that I don't think there's any special meaning to -1