我注意到一个预增量/减量运算符可以应用于变量(如++count
).它编译,但它实际上并没有改变变量的值!
Python中预增量/减量运算符(++/ - )的行为是什么?
为什么Python偏离了C/C++中这些运算符的行为?
我有这段代码(取自这个问题):
var walk = function(dir, done) {
var results = [];
fs.readdir(dir, function(err, list) {
if (err)
return done(err);
var pending = list.length;
if (!pending)
return done(null, results);
list.forEach(function(file) {
file = path.resolve(dir, file);
fs.stat(file, function(err, stat) {
if (stat && stat.isDirectory()) {
walk(file, function(err, res) {
results = results.concat(res);
if (!--pending)
done(null, results);
});
} else {
results.push(file);
if (!--pending)
done(null, results);
}
});
});
});
};
Run Code Online (Sandbox Code Playgroud)
我正在努力遵循它,我想我理解除了接近结尾的所有内容!--pending
.在这种情况下,该命令的作用是什么?
编辑:我感谢所有进一步的评论,但这个问题已被多次回答.不管怎么说,还是要谢谢你!
我正在看Xcode 7.3笔记,我注意到了这个问题.
++和 - 运算符已被弃用
有人可以解释为什么它被弃用了吗?我是对的,在Xcode的新版本中,你现在要使用而不是++
这个x += 1
;
例:
for var index = 0; index < 3; index += 1 {
print("index is \(index)")
}
Run Code Online (Sandbox Code Playgroud)
我希望有一个像这样的for循环:
for counter in range(10,0):
print counter,
Run Code Online (Sandbox Code Playgroud)
输出应为10 9 8 7 6 5 4 3 2 1
看看这个问题并尝试一些代码:
int x = 100;
while ( 0 <-------------------- x )
{
printf("%d ", x);
}
Run Code Online (Sandbox Code Playgroud)
我试图编译gcc
并得到以下错误:
file.c: In function 'main':
file:c:10:27: error: lvalue required as decrement operand
while ( 0 <-------------------- x )
Run Code Online (Sandbox Code Playgroud)
但编译与g++
作品.为什么这段代码在C++中有效但在C中无效?
当用户在php和mysql中删除它时,我想减少一个值.我想检查不要低于0.如果值为0则不减少.
mysql_query("UPDATE table SET field = field - 1 WHERE id = $number");
Run Code Online (Sandbox Code Playgroud)
如果字段为0则不执行任何操作
你如何在Swift 3.0中表达一个递减的索引循环,其中下面的语法不再有效?
for var index = 10 ; index > 0; index-=1{
print(index)
}
// 10 9 8 7 6 5 4 3 2 1
Run Code Online (Sandbox Code Playgroud) 我知道如何在coffeescript中进行循环递增,例如:
CoffeeScript的:
for some in something
Run Code Online (Sandbox Code Playgroud)
生成的Javascript:
for (_i = 0, _len = something.length; _i < _len; _i++)
Run Code Online (Sandbox Code Playgroud)
如何在Coffeescript中创建类似于此的循环递减?
for (var i = something.length-1; i >= 0; i--)
Run Code Online (Sandbox Code Playgroud) 增加/减少数字和/或数字变量的惯用 Common Lisp方法是什么?
这是一个例子
#include <iostream>
using namespace std;
int main()
{
int x = 0;
cout << (x == 0 ? x++ : x) << endl; //operator in branch
cout << "x=" << x << endl;
cout << (x == 1 || --x == 0 ? 1 : 2) << endl; //operator in condition
cout << "x=" << x << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
0
x=1
1
x=1
Run Code Online (Sandbox Code Playgroud)
我理解输出,但这是不确定的行为?在任何一种情况下,评估顺序是否得到保证?
即使有保证,我也非常清楚使用递增/递减会很快成为可读性的问题.我只是问我看到类似的代码并且立即不确定,因为有很多关于递增/递减运算符的模糊/未定义使用的示例,例如......
C++没有定义评估函数参数的顺序.↪
int nValue = Add(x, ++x);
Run Code Online (Sandbox Code Playgroud)C++语言表示你不能在序列点之间多次修改变量.↪
x …
Run Code Online (Sandbox Code Playgroud)decrement ×10
increment ×4
for-loop ×3
c ×2
c++ ×2
operators ×2
python ×2
swift ×2
coffeescript ×1
common-lisp ×1
javascript ×1
lisp ×1
loops ×1
mysql ×1
not-operator ×1
swift2 ×1
swift3 ×1