使用For循环而不是While循环 [英] Use For loop instead of While loop
问题描述
在下面的程序中我想使用而不是While循环。
这个程序返回正整数的个别数字之和。
#include < stdio.h >
#include < conio.h >
void main()
{
int num,k,sum = 0;
clrscr();
printf( 输入要添加数字的数字: - );
scanf( %d,& num);
while (num!= 0)
{
k = num%10;
sum = sum + k;
num = num / 10;
}
printf( \ nSum of the Digits: - %d,总和);
getch();
}
输出
输入数字为的数字待补充: - 456
数字总和: - 15
< BLOCKQUOTE>为什么呢?虽然是这个的自然循环。替代方案基本上是,而
更难以阅读:
for (; num!= 0 ;)
{
...
}
通常,当您有一个计数器控制循环执行的次数时,使用。你没有这样的计数器,虽然你可以构建一个。
OriginalGriff在解决方案1中做了这个,使用与你的<$ c $相同的变量c>而用于控制循环。我更进一步,因为你在每次迭代中修改你的控制变量:这是一个通常放在for
语句的最后一部分的步骤,如下所示: br />
for (; num!= 0 ; num = num / 10)
{
...
}
就个人而言,我会做一些完全不同的事情:输入本身作为字符串输入,那么为什么不将其作为字符串读取并简单地遍历该字符串中已存在的数字?这样可以更自然地使用!
另外,你将这个问题标记为C ++ / Objective C,但你使用的是C函数而不是C ++ io流!?
另外,永远不要定义main()
返回类型void
! main函数应始终返回int
,即使编译器可能接受void
。这是每个可执行程序的预期惯例。
尝试这样的事情:
#include < iostream >
#include < string >
int main(){
string line;
std :: cout<< 请输入数字:<<的std :: ENDL;
std :: getline(std :: cin,line);
int sum = 0 ;
for ( auto position = line.begin(); position!= line.end( ); ++位置)
{
// 获取位置的字符,转换为数字,并加上总和
}
...
return 0 跨度>; // 0表示成功;如果上面发生错误,则返回其他内容
}
In the following program I want to use instead of While loop.
This program returns the sum of individual numbers of positive integer number.
#include<stdio.h>
#include<conio.h>
void main()
{
int num,k,sum=0;
clrscr();
printf("Enter the numbers whose digits are to be added :- ");
scanf("%d",&num);
while(num!=0)
{
k=num%10;
sum=sum+k;
num=num/10;
}
printf("\nSum of the Digits :- %d",sum);
getch();
}
Output
Enter the numbers whose digits are to be added :- 456
Sum of the Digits :- 15
Why? While is the natural loop for this. The alternative is basically awhile
that is harder to read:for( ; num != 0 ; ) { ... }
Typically,for
is used when you have a counter controlling the number of times your loop is executed. You don't have such a counter here, although you could construct one.
OriginalGriff did just that in solution 1, using the same variable that yourwhile
uses to control the loop. I'd go one step further, since you modify your control variable in each iteration: that is a step typically placed in the last section of thefor
statement, like this:
for( ; num != 0 ; num = num/10 ) { ... }
Personally, however, I'd do something entirely different: the input is natively entered as a string, so why not read it as a string and simply iterate over the digits that are already present in that string? This would lend itself to a much more natural use offor
!
Also, you tagged this question as C++/Objective C, but you are using C functions instead of C++ io streams!?
Additionally, never definemain()
with a return type ofvoid
! the main function should always returnint
, even though the compiler may acceptvoid
. That is a convention expected from every executable program.
Try something like this:
#include <iostream> #include <string> int main() { string line; std::cout << "please enter number: " << std::endl; std::getline(std::cin, line); int sum = 0; for (auto position = line.begin(); position != line.end(); ++position) { // get character at position, convert to digit, and add to sum } ... return 0; // 0 indicates success; return something else if an error occurs above }
这篇关于使用For循环而不是While循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!