4

When should I use pre decrement and when to use post decrement?

and for the following code snippet, should I use pre or post decrement.

static private void function(int number)
{
    charArr = new char[number];
    int i = 0;
    int tempCounter;
    int j = 0;
    while(charrArr!=someCharArr)
    {
        tempCounter = number - 1;
        password[tempCounter] = element[i%element.Length];
        i++;
        //This is the loop the I want to use the decrementing in.
        //Suppose I have a char array of size 5, the last element of index 5 is updated
        //in the previous statement.
        //About the upcoming indexes 4, 3, 2, 1 and ZERO.
        //How should I implement it?
        // --tempCounter or tempCounter-- ?
        while (charArr[--tempCounter] == element[element.Length - 1])
        {
        }
    }
}
sikas
  • 5,435
  • 28
  • 75
  • 120
  • As far as your code goes, I guess that it should be `tempCounter = number;`, `password[tempCounter - 1]` and `charArr[--tempCounter]` despite the fact that the `while`-loop will work on a non-initialized array and `tempCounter` can become negative. – sjngm Dec 25 '10 at 22:27
  • Possible duplicate of [What is the difference between ++i and i++](http://stackoverflow.com/questions/24853/what-is-the-difference-between-i-and-i) – TylerH Jun 22 '16 at 14:25

2 Answers2

5

You use pre-decrement if you want to decrement the variable before the value is passed on to the remaining expression. On the other hand, a post-decrement evaluates the expression before the variable is decremented:

int i = 100, x;
x = --i;                // both are 99

and

int i = 100, x;
x = i--;                // x = 100, i = 99

The same obviously is true for increments.

sjngm
  • 12,423
  • 14
  • 84
  • 114
  • so `charArr[--tempCounter]` will differ in the value from `charArr[tempCounter--]` in the condition of the while loop. This will decrement the `tempCounter` before checking the condition. – sikas Dec 25 '10 at 22:19
  • 1
    @sikas: I don't understand your second line. Both versions will cause the same value to be used inside the loop. The pre/post-decrement only affects what's happening inside the `while`-expression. – sjngm Dec 25 '10 at 22:24
0

you should have ++i; (not that it matters), and should have tempCounter-- Otherwise you will miss the "first" index of charArr

EnabrenTane
  • 7,428
  • 2
  • 26
  • 44
  • In C#, iirc, pre- vs. post-increment/decrement make no difference in speed. – Jeff Hubbard Dec 25 '10 at 22:14
  • I\`m updating the values of the charArr in the second while loop from the `last index - 1` till `index = ZERO`. so in the condition of the loop should I use `tempCounter--` or `--tempCounter`? – sikas Dec 25 '10 at 22:17
  • This may be true for built in times, but is generally untrue for user classes that define these operators. `--i;` may be faster, but is never slower than `i--;`. But we both know a line like `i--;` will be compiler optimized especially when i is of type int. – EnabrenTane Dec 25 '10 at 22:18
  • @sikas I would use `while(tempCounter > 0) { charArr[tempCounter--] = /* value */ } ` – EnabrenTane Dec 25 '10 at 22:22