pre递减与后递减 [英] pre Decrement vs. post Decrement

查看:130
本文介绍了pre递减与后递减的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我应该使用pre递减,以及何时使用后递减?

和以下code片断中,我应该使用pre或交递减。

 静态私人无效功能(INT数)
{
    charArr =新的char [编号]
    INT I = 0;
    INT tempCounter;
    INT J = 0;
    而(charrArr!= someCharArr)
    {
        tempCounter = - 1;
        密码[tempCounter] =元素[I%element.Length]
        我++;
        //这是我想使用在递减的循环。
        //假设我有大小5的字符数组,索引5的最后一个元素被更新
        //在previous声明。
        //即将到来的指标4,3,2,1和零。
        //我应该如何实现呢?
        // --tempCounter或tempCounter--?
        而(charArr [ - tempCounter] ==元素[element.Length - 1])
        {
        }
    }
}


解决方案

如果你想在剩余的前pression传递前值递减变量可以使用pre-递减。在另一方面,递减后评估前pression变量递减之前​​:

  INT I = 100,X;
X = --i; //都是99

  INT I = 100,X;
X =我 - ; // X = 100,I = 99

同样显然如此递增。

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

解决方案

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.

这篇关于pre递减与后递减的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆