【问题标题】:Increments --i and i--递增 --i 和 i--
【发布时间】:2022-07-20 23:54:34
【问题描述】:

请有人以非常详细的外行理解的方式向我解释清楚。 --i 和 i-- 之间的实际区别以及应该在哪个位置使用哪个。欢迎示例或图片说明。 我一直在编程,我了解其他复杂的东西,如递归、迭代、控制结构等,但我并没有真正完全理解增量运算符。我只是将 any 用于我的项目中的任何代码。 请帮忙。

【问题讨论】:

标签: javascript python c increment


【解决方案1】:

let count;
count = 0;

console.log('count--',count--);

count = 0;

console.log('--count',--count);

// after either count-- or --count the value of count will be -1
// but the difference is the immediate value returned by the operation

【讨论】:

    【解决方案2】:

    为了澄清,你可以阅读

    • 将“--”或“++”表示为“递减”/“递增”
    • 变量名称为“使用值”

    所以使用--i 你会递减然后得到值
    但是使用i-- 你会得到递减

    在实际用例中意味着

    function print(value) {
        console.log(value)
    }
    
    let i = 10;
    print(i--) //The function is called like print(10)
    //From here, the value of i is 9
    
    let j = 10;
    print(--j) //The function is called like print(9)
    //From here, the value of j is 9
    
    j = ++i //j is set to 10 and i is 10 too
    i = j++ //i is still ten but j become 11
    
    

    (为了清楚起见,python 没有这些运算符,只有 +=-=

    【讨论】:

      猜你喜欢
      • 2014-02-22
      • 2018-06-21
      • 1970-01-01
      • 2013-03-18
      • 1970-01-01
      • 2018-08-20
      • 2012-07-10
      • 2017-08-11
      • 1970-01-01
      相关资源
      最近更新 更多