10

Bait question: What's your style?
a) for ( ... ; ++i ) {
b) for ( ... ; i++ ) {

I am disgusted of b). a) for the win 😎

Comments
  • 10
    ... ; ) {
    i++;
    }
  • 23
  • 10
    I'm in the i++ camp, and it doesn't matter in C anyway.

    However, it may matter in C++ if i isn't a POD and the adding operator is overloaded, then ++i has the potential to be faster so that it should be preferred in general.
  • 15
    Um, really? ++i is pre-increment and i++ is post-increment, and are used in different situations. Pre-increment increments the value before it is used. Post increment increments the value after the expression completes. Because of this, the outcomes will be different. There are distinct cases in which each is used.
  • 10
    Very different uses.
    Learn2C
  • 1
    @FrodoSwaggins I'm old school, what can I say? 🤷‍♂ï¸
  • 4
    By preference i++, when needed ++i
  • 4
    Performancewise, ++i is better. It increments i and returns it. i++ copies i, returns it and then increments original iirc.
  • 1
    @Root No worries, I know C and C++ 😉
  • 1
  • 0
    Pre-increment for performance-critical code and by default as post-increment has to make a copy of i and retain it which is a little overhead and to me ++i does make it more clear what’s going on in a longer arithmetic expression for example because you don’t have to keep track of i’s state.
  • 0
    array.forEach

    I forgot the last time I used a for loop. Oh, javascript!
  • 1
    you guys want to see something scary?

    $i = 0;

    while ($i < 5) do {

    // some shit

    $i = $i + 1;

    }
  • 0
    I prefer foreach
  • 0
    For those citing performance, you really should restructure you loops to run backwards instead; referencing the end value as initialisation (especially for something like length) and then counting down comparing with zero will be much more performant than switching increment operator.

    Also, I prefer code that's easier to read and change, which makes me go for the `i += 1` style ;)
  • 1
    @FrodoSwaggins good to learn, thanks!
  • 1
    @PonySlaystation oh the Bait question is a master.

    I prefer B. But it is a case to case basis. We need context 😀
  • 1
    Int[] x = new int[n]
    Int I = 0;
    For(int y:n){
    I++;
    }
Add Comment