while(n--)
和 while(n=n-1)
有什么区别?当我在代码中使用 while(n=n-1)
时,我可以输入少于 1 个数字。
示例:首先输入 3,然后输入 3 次单个数字(但在 while(n=n-1)
中不会发生这种情况)。
但是当我使用while(n--)
时,就正常了。
我的代码是:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
long long inum;
scanf("%d", &n);
while(n--)
{
scanf("%lld", &inum);
if(inum == 0 || inum % 2 == 0)
{
printf("even\n");
}
else
{
printf("odd\n");
}
}
return 0;
}
最佳答案
n--
的值是n
的前一个值
int n = 10;
// value of (n--) is 10
// you can assign that value to another variable and print it
int k = (n--); // extra parenthesis for clarity
printf("value of n-- is %d\n", k);
n = n - 1
的值比 n
的前一个值小 1
int n = 10;
// value of (n = n - 1) is 9
// you can assign that value to another variable and print it
int k = (n = n - 1); // extra parenthesis for clarity
printf("value of n = n - 1 is %d\n", k);
关于c++ - while(n--) 和 while(n=n-1) 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33655155/