I need help - ++i, i++ and where to use it

This post was flagged by the community and is temporarily hidden.

Not sure what this has to do with kotlin. This is java code. Also you want to use i++ instead of i = i++.
foo = i++ is the same as

int temp = i
i = i + 1
foo = temp

therefor i = i++ is just i = i.
There is also foo = ++i which is equal to

int temp = i + 1
i = temp
foo = temp

insted of i=i+1

I’m kind of restating what @Wasabi375 said in a slightly different way. You may want to go through some beginner tutorials as they will cover these concepts and give you the skills and words to research new ones on your own.
There’s a lot to learn and it will be significantly easier for you to spend some time finding good tutorials than to wait on replies on forums. Especially since programming requires constant research without being always being able to ask questions–researching these low-hanging fruit is a great way to learn how to do the research :slight_smile:

The statement:

i++

is a shortcut for:

i = i + 1

This is incrementing by 1. It’s there so that coders have a quick and easy shortcut for incrementing a value. There’s another form of incrementing where you put the “++” before the variable–that version also increments but since you’re new to these I’ll just recommend that only increment on it’s own line while you’re learning.

EDIT: See other replies for correction

When you say: ``` i = i++ ``` You're really saying: ``` i = i i = i + 1 ```

Here’s a runnable example (your example was in Java but this is in Kotlin):

fun main() {
    var i = 1
    while (i <= 10) {
        println("i=$i")
        i++ // Try replacing this with "i = i + 1" or with "i += 1" or even "++i". They all do the same thing.
    }
}

No, your not.

i = ++i

is the same as

i = i + 1
i = i
i = i++

would be the same as

i = i
i+1 // this is only calculated but never used
fun main() {
    var i = 0
    println(i)
    i = i++
    println(i)
    i = ++i
    println(i)
}

++i and i++ are the same if they are just used on their own. In that case they work just like i = i + 1. When used within more complex expressions there is a slight, but important difference though. The fact that someone as experienced as @arocnies can mess this up should give you an idea why I (and lot’s of other people) never use it within a more complex expression.

i++ // this is good

// java code, not kotlin but still a valid use of ++
for(int i = 0; i < 100; i++)

// bad idea. Many people will not know what this does exactly
foo = i++
foo = ++i

// never ever do something like this. You will never find the bug
// if there is one caused by a wrong use of ++i/i++
someRealyLongFunctionName(someOtherFunction(i++), andAnotherFunction(++i))
1 Like

Nice catch! I definitely missed that one.

Yeah I never use the increment operator in a complex expression. I think my confusion comes from this:

fun main() {
    var i = 10
    var a = i++ // Returns i then increment i
    println("i=$i")
    println("a=$a")

    i = i++ // returns i? Doesn't increment i? Nope! The incremented value is thrown away.
    println("i=$i")
}

I don’t think I’ve ever written it using the same variable before :stuck_out_tongue:
I was surprised a = i++ behaved so different to i = i++.

I see now how @Wasabi375 example explains it with temp is correct (although instead of foo on the last line maybe it should be “i”?). The incremented value is thrown away :+1: