while(n-)和while(n=n-1)之间的区别是什么
what is the difference between while(n--) and while(n=n-1)?
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("evenn");
}
else
{
printf("oddn");
}
}
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 %dn", 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 %dn", k);
while(n--)
在其主体中使用n
,递减的值用于下一次迭代。
while(n=n-1)
与while(--n)
相同,后者递减并在其主体中使用n
的这个新值。
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- int(c) 和 c-'0' 之间的区别。C++
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- while循环中while循环的时间复杂度是多少
- Arduino:for/while/if在void setup()或void loop()之前?——错误:之前需要不合格
- C++ - "!pointer"和"pointer == nullptr"的区别?
- C++ 使用 assign 函数的字符串与直接使用 '=' 更改值的字符串之间的区别
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- std::atomic和std::condition_variable wait,notify_*方法之间的区别
- 擦除while循环中迭代的元素
- 在 .h 文件中的类中声明静态变量和在.cpp文件中声明"global"变量有什么区别
- 我是C++编程的新手,这些代码之间有什么区别,我应该使用哪一个
- matlab 和 c++ 中的 while 循环之间的区别
- 在getline上检查eof()有什么区别,即while(!getline().eof())和简单地检查while(ge
- 这两个 while 循环有什么区别?
- C++ 中循环的 while 和 do while 之间的区别
- c++中while和do的区别
- 字符串复制函数中这两个while循环条件的区别