0

In code that I've seen, a java for loop in the normal style goes as follows:

for(int i = 0; i < n; i++){\\do stuff}

In c++, the equivalent example would be written as follows:

for(int i = 0; i < n; ++i){\\do stuff}

I can't see a reason for the syntax difference. I've read that ++i might be slightly faster which justifies it's use in c++, but then why do we use i++ in java? Also, why don't we call it ++c?

Clarification: I am interested in why the syntax difference has developed differently between the two languages, not what the difference between the two ways of writing the for loop is. From what I can tell this is likely not for practical reasons, but rather historical ones, and I'd like to know what those reasons are.

Alex Li
  • 246
  • 4
  • 11
  • 1
    Note there's no practical difference in the for loop examples you give, at least with regards to Java. However, there is a difference that can be significant depending on context. The `i++` will increment and return the _old value_, whereas `++i` will increment and return the _new value_ (again, at least that's how it is in Java). – Slaw Dec 20 '19 at 01:41
  • 1
    There isn't any relevant difference between the two in this specific case in C++ either. Postfix increment will need to save a temporary copy of the old value, but that makes no practical difference, as it will be optimized to the same code. – walnut Dec 20 '19 at 01:43
  • 1
    I would assume ot comes from non-optimized code, where `++i` would be more efficient. However, any compiler with even the most basic optimizations would optimize `i++` anyway. – ChrisMM Dec 20 '19 at 01:49
  • Does this answer your question? [++i or i++ in for loops ??](https://stackoverflow.com/questions/4261708/i-or-i-in-for-loops) and [Is there a performance difference between i++ and ++i in C++?](https://stackoverflow.com/questions/24901/is-there-a-performance-difference-between-i-and-i-in-c) – walnut Dec 20 '19 at 01:49
  • Those questions just have answers saying that 'they are the same really'/it doesn't matter which you use. I'm wondering about the historical reasons for why the different styles have developed in different languages, knowing that they produce the same result. – Alex Li Dec 20 '19 at 02:14
  • 1
    @AlexLi Could you add a sentence clarifying that to your question? For me it wasn't clear from your question that you are interested in the historical background rather than the practical relevance. – walnut Dec 20 '19 at 02:26
  • @walnut Thanks for the suggestion, I have done that. – Alex Li Dec 20 '19 at 02:32
  • Evidence please. Your question is based on a mere assertion. – user207421 Dec 20 '19 at 03:16
  • 1
    *"why don't we call it ++c"*. The joke is: C++ is an improvement of C, so the increment, but we return old value, as we inherit of so any pitfall of C, and also because most programmer (at least at the beginning) just write C with C++ compiler. Teachers show `new[]` before `std::vector`, .... – Jarod42 Dec 20 '19 at 08:55

2 Answers2

3

Realistically, if the type is an integer, the difference is negligible to non-existant on the most common platforms. Where it can make a difference, is when the type is a custom object of some sorts (for example an iterator). Consider:

class iter
{
  int* ptr; //< lazy
public:
  iter& operator ++ () //< prefix
  {
    ++ptr;
    return *this;
  }
  iter operator ++ (int) //< postfix
  {
    iter it(*this); //< temp copy
    ++ptr;
    return it;
  }
};

In this case, the postfix operator needs to take a copy, whereas the prefix does not. In most common cases the compiler will see through what you are doing (pre or post fix), and optimise the code to be exactly the same. The only real problem these days is if the compiler can't see through your iterator (e.g. instead of inlining the iterator class, you've DLL exported it, or it has a non trivial implementation)

robthebloke
  • 9,331
  • 9
  • 12
0

The postfix form was introduced in early C programming books and used a lot. After that the prefix form got some popularity, and was introduced in Java programming books. So, basically, it depends on when did you start your programming career.

With the contemporary optimizing compilers there's no practical difference (for C/C++ at least).

If you want to look more senior, use the postfix form =)

lenik
  • 23,228
  • 4
  • 34
  • 43
  • Might be worth noting _why_ it would have been used in early C books, which is that prefix is a bit more efficient if you don't have a compiler that optimizes it away. Specifically, you can just increment the variable wherever it is, rather than first copying it and then incrementing. – yshavit Dec 20 '19 at 02:17
  • Why my comment above was deleted will forever remain a mystery. – user207421 Dec 20 '19 at 06:43
  • @user207421 never seen your comment, so please, don't ask me =) – lenik Dec 21 '19 at 04:30