postfix and prefix increment or decrement operators very very confusing for me i try my best but i can not understand the basic different between them plz help me i know The prefix form first performs the increment operation and then returns the value of the increment operation. The postfix form first returns the current value of the expression and then performs the increment operation on that value.
i dont know why i can not understanding this plz help me it is very very confusing there are two codes that give same output why
first is

class LoopOperator
{
    public static void main(String a[])
    {   
        int i;
        for(i=1;i<=10;i++)
        System.out.println(i);
    }
}

and second

class LoopOperator
{
    public static void main(String a[])
    {   
        int i;
        for(i=1;i<=10;++i)
        System.out.println(i);
    }
}

plz help me

Recommended Answers

All 12 Replies

The loops are confusing this. Try a simpler example

int i = 1;
System.out.println(i++);
System.out.println(i);

int j = 1
System.out.println(++j);
System.out.println(j);

i understant it but in for loop the output is same why

Because you're using i++/++i as a statement on its own without doing anything with its value. The side-effects of ++i and i++ are the same, only the value differs. So if you don't use the value, there is no observable difference.

look at this code

    int i = 1;
    System.out.println(i++);
    System.out.println(i);
    int j = 1
    System.out.println(++j);
    System.out.println(j);

in upper code i++and ++i are also as a statement but answer is different but in the case of for loop working is same whyyyyyyy

i think in for loop the compiler leaves increment/decrement operators.
Am i write?

I don't understand that question, sorry.

my quebtion is that how for loop is works i mean inrement and derement operator

In that particular code the i++ or ++i is executed at the end of each iteration and its execution is complete before the next iteration starts and its value is tested, so it makes no difference whether you use i++ or ++i

in upper code i++and ++i are also as a statement

No, they're not. System.out.println(i++) is a statement and i++ is an expression inside that statement. It's not used as a statement on its own here. Most importantly System.out.println uses the value of i++, your loop does not.

To be completely accurate the i++ in the loop is an expression as well (if it were a statement, it would have a semicolon at the end), but it's an expression that is only executed for its side effects and whose value is not used, so it's basically used as a statement.

I'll simplify this down to terms that some people might be able to understand if I'm not mistaken. when you put i++ in to a for loop it will add the value of "i" after the compiler has checked the middle comparison statement but will add 1 to "i" before the compiler proceeds to the bracket. If on the other hand you do ++i it does a similar thing except it adds 1 to i as if though it were the first line of code in the for loop. May not make much of a difference in this circumstance but in other circumstances can make a difference.

For example if you were to print i++ and ++i one would print the value of i then add and the other would add then print the value. Makes a difference.

when you put i++ in to a for loop it will add the value of "i" after the compiler has checked the middle comparison statement but will add 1 to "i" before the compiler proceeds to the bracket

That's not true. The steps happen in the following order:

  1. Condition is checked. If false abort.
  2. Loop body is executed.
  3. Increment is executed.
  4. Goto 1

So no matter whether you write i++ or ++i or i=f(i) or whatever, the increment always happens before the condition is checked (except the first time when the increment doesn't happen at all).

For example if you were to print i++ and ++i one would print the value of i then add and the other would add then print the value. Makes a difference.

The difference between i++ and ++i is not when the increment happens. It always happens exactly when the expression is evaluated. The difference is the resulting value. The expression i++ results in the old value of i while the expression ++i results in the new value.

So if i starts as 42 and you do f(i++), then f will be called with the argument 42, but the value of i will already be 43 (you can test this by making i an instance variable and letting f print both its argument and i).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.