Pre Decrement and post Decrement

When to use a pre-decrement and when to use a post-decrement?

and for the next piece of code whether to use pre or post decment.

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])
        {
        }
    }
}

      

+2


source to share


2 answers


You use a pre-decrement when you want to decrease the value of a variable before the value is passed to the rest of the expression. On the other hand, 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 is true for increments.

+4


source


you should have ++i;

(not this important), and should have tempCounter--

Otherwise, you will miss the "first" charArr index



0


source







All Articles