Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question What does ++, -- and stuff like that mean?

Discussion in 'Scripting' started by Terrosis, Apr 14, 2021.

  1. Terrosis

    Terrosis

    Joined:
    Apr 3, 2021
    Posts:
    2
    Hello there! I am new to unity and C# in general and I just have one question. Let's say for example that someone creates an int variable called i with the value and then write
    Code (CSharp):
    1. Debug.Log(i++)
    , what does that mean?

    I want to know what the process is.
     
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,907
    SparrowGS likes this.
  3. Terrosis

    Terrosis

    Joined:
    Apr 3, 2021
    Posts:
    2
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    As was already linked... it's the "increment" operator. It increments i by 1.

    -- is the decrement, and it decrements i by 1.

    ...

    That website that was linked. MSDN. Yeah, keep a hold of that. It should be your go to source for all things C#. C# is actually independent of Unity and you'll severely limit yourself if you search for answer ONLY in the context of Unity, rather than in the context of C# in general, when dealing with language specific things (rather than unity specific things).
     
    SparrowGS likes this.
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    At the risk of venturing off topic, I don't really recommend actually doing arithmetic which changes a variable inside a Debug.Log statement. This will mean when you remove the Debug.Log statement, you'll affect the rest of the code which depends on that variable.
     
    SparrowGS, lordofduct and Kurt-Dekker like this.
  6. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    664
    To be brutally pedantic about it, that's the "postfix" increment operator. Similar to, but not the same as, the "prefix" increment operator (used as in "++i"). Believe it or not, you can actually get dinged in interviews for not being able to explain why you (as some say) should use the prefix version whenever you have a choice.

    When it comes to learning C#, lordofduct is correct: the MSDN pages on it are gospel. Add to that the fact that running code is free. Download a copy of LINQPad from linqpad.net. LINQPad is also free and will let you run countless little test programs quickly, without each one taking up megabytes on your disk or making you wait several minutes while it opens.

    And welcome to programming! When it isn't making you crazy, you are going to love it.
     
    SparrowGS and Joe-Censored like this.
  7. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    664
    I'll add a personal note on style, by the way: I do use the increment operators, but mostly in "for" loops. It can be a bit cryptic and easy to miss. I pretty much always use "i = i + 1" when I need to add one to i. Back in the Stone Age (1969), it was part of the "B" programming language, which was the predecessor to "C," which begat "C++," which (with some other ancestors) gave us Java and C# (kinda, sorta). All of them have increment operators. But why? You really don't need them. Well, at the time B was designed, computers were slow and memory was small. A lot of computers included, deep down in the hardware, an instruction that would add one to a number already kept in a place called a "register." Modern machines all still have this feature, but back then, being able to add one with a single instruction was a lot more appealing than what you need to do to add pretty much any other number. And adding one is something you do a lot (which is why those early machines had that instruction built in). By adding an explicit increment operator to the language, it was easy for the B compiler to convert it directly into the instruction that did this quickly and with less memory than more general addition operations. So you can see why they included it.

    Today, modern compilers do a great job of recognizing opportunities to use specialized instructions (and even combinations of them) to generate optimized code, without us poor, overworked programmers having to recognize those opportunities ourselves. That's why I pretty much always write "i = i + 1" instead of "++i." It's more self-documentary, harder to mis-read (important when you have my vision), and just as fast at run time.

    Others are sure to have different points of view, but arguing about such things is one of the great joys of being a programmer, so let the festivities commence!
     
    SparrowGS and Lurking-Ninja like this.
  8. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,907
    I'm in the pro ++ team forevah!
     
  9. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    My favorite part of when I first learned what ++ does, was it hit me that the name of the C++ language had meaning within the language itself. The meaning of C# on the other hand is music related.
     
    SparrowGS likes this.
  10. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    664
    I have heard that the "C#" moniker is an attempt to "outincrement" the C++ name:

    upload_2021-4-14_15-5-31.png
     
    SparrowGS and Joe-Censored like this.
  11. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    It was both.

    They actually had lots of names for it and settled on C#... one of the original names was 'Cool'. Which is... terrible... and Anders Hejlsberg (one of the original creators) sort of agrees (though he did like cool... just not some of the others):

    https://www.red-gate.com/simple-talk/opinion/geek-of-the-week/anders-hejlsberg-geek-of-the-week/

    But yeah it's mostly ++ ++:
    https://web.archive.org/web/2019080...cle/261958/a-z_programming_languages_c_/?pp=2

    Thing is, even though that quote makes it sound like it's mostly ++ ++. That could just be Anders opinion coming out in that interview as there's a lot of back and forth on the internet about the actual intents about which of the 2 was more prominent. I mean... it was named by committee, so to each member of said committee probably had different weights of opinions to both influences. In the end all we can really say is that Ander's has publicly stated that both were part of it.

    Honestly... I doublt much thought of the "meaning" was put in. Like it could literally been like "hey guys... ever notice that # looks like 4 pluses and is also like a musical symbol meaning half octave higher? That's cool... right?" It's marketing, "cool" factor is primary thing. Heck... the name of the language was originally COOL... uuuggghghghghhhhh

    ...

    I would also like to note it is specifically the sharp symbol and not the number symbol. The only reason they explicitly use the number symbol is because it's on most keyboards, but in marketing materials they explicitly favor the ♯ symbol.
     
    Last edited: Apr 14, 2021
    SparrowGS and Joe-Censored like this.
  12. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    664
    May or may not be a connection, but Microsoft had a project for an incremental compiler back in 1988. The project was canceled, but the compiler's name was "C#." So the name had some history, and was available. From "My History of Visual Studio," by Rico Mariani: