Difference between pre-increment and post-increment in a loop?

后端 未结 22 1739
暗喜
暗喜 2020-11-21 23:41

Is there a difference in ++i and i++ in a for loop? Is it simply a syntax thing?

相关标签:
22条回答
  • 2020-11-22 00:28

    Since you ask about the difference in a loop, i guess you mean

    for(int i=0; i<10; i++) 
        ...;
    

    In that case, you have no difference in most languages: The loop behaves the same regardless of whether you write i++ and ++i. In C++, you can write your own versions of the ++ operators, and you can define separate meanings for them, if the i is of a user defined type (your own class, for example).

    The reason why it doesn't matter above is because you don't use the value of i++. Another thing is when you do

    for(int i=0, a = 0; i<10; a = i++) 
        ...;
    

    Now, there is a difference, because as others point out, i++ means increment, but evaluate to the previous value, but ++i means increment, but evaluate to i (thus it would evaluate to the new value). In the above case, a is assigned the previous value of i, while i is incremented.

    0 讨论(0)
  • 2020-11-22 00:29

    As @Jon B says, there is no difference in a for loop.

    But in a while or do...while loop, you could find some differences if you are making a comparison with the ++i or i++

    while(i++ < 10) { ... } //compare then increment
    
    while(++i < 10) { ... } //increment then compare
    
    0 讨论(0)
  • 2020-11-22 00:34

    a++ is known as postfix.

    add 1 to a, returns the old value.

    ++a is known as prefix.

    add 1 to a, returns the new value.

    C#:

    string[] items = {"a","b","c","d"};
    int i = 0;
    foreach (string item in items)
    {
        Console.WriteLine(++i);
    }
    Console.WriteLine("");
    
    i = 0;
    foreach (string item in items)
    {
        Console.WriteLine(i++);
    }
    

    Output:

    1
    2
    3
    4
    
    0
    1
    2
    3
    

    foreach and while loops depend on which increment type you use. With for loops like below it makes no difference as you're not using the return value of i:

    for (int i = 0; i < 5; i++) { Console.Write(i);}
    Console.WriteLine("");
    for (int i = 0; i < 5; ++i) { Console.Write(i); }
    

    0 1 2 3 4
    0 1 2 3 4

    If the value as evaluated is used then the type of increment becomes significant:

    int n = 0;
    for (int i = 0; n < 5; n = i++) { }
    
    0 讨论(0)
  • 2020-11-22 00:34

    As this code shows (see the dissambled MSIL in the comments), the C# 3 compiler makes no distinction between i++ and ++i in a for loop. If the value of i++ or ++i were being taken, there would definitely be a difference (this was compiled in Visutal Studio 2008 / Release Build):

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace PreOrPostIncrement
    {
        class Program
        {
            static int SomethingToIncrement;
    
            static void Main(string[] args)
            {
                PreIncrement(1000);
                PostIncrement(1000);
                Console.WriteLine("SomethingToIncrement={0}", SomethingToIncrement);
            }
    
            static void PreIncrement(int count)
            {
                /*
                .method private hidebysig static void  PreIncrement(int32 count) cil managed
                {
                  // Code size       25 (0x19)
                  .maxstack  2
                  .locals init ([0] int32 i)
                  IL_0000:  ldc.i4.0
                  IL_0001:  stloc.0
                  IL_0002:  br.s       IL_0014
                  IL_0004:  ldsfld     int32 PreOrPostIncrement.Program::SomethingToIncrement
                  IL_0009:  ldc.i4.1
                  IL_000a:  add
                  IL_000b:  stsfld     int32 PreOrPostIncrement.Program::SomethingToIncrement
                  IL_0010:  ldloc.0
                  IL_0011:  ldc.i4.1
                  IL_0012:  add
                  IL_0013:  stloc.0
                  IL_0014:  ldloc.0
                  IL_0015:  ldarg.0
                  IL_0016:  blt.s      IL_0004
                  IL_0018:  ret
                } // end of method Program::PreIncrement             
                 */
                for (int i = 0; i < count; ++i)
                {
                    ++SomethingToIncrement;
                }
            }
    
            static void PostIncrement(int count)
            {
                /*
                    .method private hidebysig static void  PostIncrement(int32 count) cil managed
                    {
                      // Code size       25 (0x19)
                      .maxstack  2
                      .locals init ([0] int32 i)
                      IL_0000:  ldc.i4.0
                      IL_0001:  stloc.0
                      IL_0002:  br.s       IL_0014
                      IL_0004:  ldsfld     int32 PreOrPostIncrement.Program::SomethingToIncrement
                      IL_0009:  ldc.i4.1
                      IL_000a:  add
                      IL_000b:  stsfld     int32 PreOrPostIncrement.Program::SomethingToIncrement
                      IL_0010:  ldloc.0
                      IL_0011:  ldc.i4.1
                      IL_0012:  add
                      IL_0013:  stloc.0
                      IL_0014:  ldloc.0
                      IL_0015:  ldarg.0
                      IL_0016:  blt.s      IL_0004
                      IL_0018:  ret
                    } // end of method Program::PostIncrement
                 */
                for (int i = 0; i < count; i++)
                {
                    SomethingToIncrement++;
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 00:34

    I dont know for the other languages but in Java ++i is a prefix increment which means: increase i by 1 and then use the new value of i in the expression in which i resides, and i++ is a postfix increment which means the following: use the current value of i in the expression and then increase it by 1. Example:

    public static void main(String [] args){
    
        int a = 3;
        int b = 5;
        System.out.println(++a);
        System.out.println(b++);
        System.out.println(b);
    

    } and the output is:

    • 4
    • 5
    • 6
    0 讨论(0)
  • 2020-11-22 00:35

    It boggles my mind why so may people write the increment expression in for-loop as i++.

    In a for-loop, when the 3rd component is a simple increment statement, as in

    for (i=0; i<x; i++)  
    

    or

    for (i=0; i<x; ++i)   
    

    there is no difference in the resulting executions.

    0 讨论(0)
提交回复
热议问题