[英]Why I can't use i/10 in FOR LOOP, C++?
I can't use i/10 in **FOR LOOP, C++.我不能在 **FOR 循环,C++ 中使用i/10 。 Can you please find why I can't use that!!
你能找出我不能使用它的原因吗!! when I write the code below;
当我写下面的代码时;
#include <iostream>
using namespace std;
int main()
{
int sum=0,i;
cout<<"enter a no.";
cin>>i;
for(i;i!=0;i=i/10)
{
sum=sum+i%10;
}
cout<<"sum="<<sum;
}
After compiling it shows that:-编译后显示:-
cpp_2.cpp:8:7: warning: statement has no effect [-Wunused-value]
cpp_2.cpp:8:7: 警告:语句无效 [-Wunused-value]
for(i;i!=0;i=i/10)
for(i;i!=0;i=i/10)
Please help me!!请帮我!!
You can, although i = i / 10
can be abbreviated to the arguably clearer i /= 10
.你可以,尽管
i = i / 10
可以缩写为更清晰的i /= 10
。
Your helpful compiler is warning you about the initialisation expression of the for
, which is just i
, and that is a no-op.您有用的编译器警告您
for
的初始化表达式,它只是i
,这是一个空操作。
Writing写作
for (; i; i /= 10)
is equivalent, and will keep the compiler happy.是等价的,并且会让编译器满意。 Note that I've replaced the tautologous
i != 0
with, simply, i
.请注意,我已将同义重复的
i != 0
替换为简单的i
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.