循环与休息或不休息 [英] Loop with break or without
本文介绍了循环与休息或不休息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨专家,
一些acadimic和非学术程序员之间存在争论,特别是在Franco世界中是否要打破循环。
下面的代号说明了我的意思。
你认为最好的是什么?使用及原因:
1.
for ( int i = 0 ; i< 10; i ++)
{
WriteLine( saisir une valeur);
int valeur = Convert.ToInt32(ReadLine());
if (valeur == 50 ){
WriteLine( Bingo!FOUND!);
break ;
}
}
2.
bool found = false ;
int tentatives = 0 ;
while (!found&& ++ tentatives< = 10 )
{
WriteLine( saisir une valeur);
int valeur = Convert.ToInt32(ReadLine());
if (valeur == 50 ){
WriteLine( Bingo!FOUND!);
found = true ;
}
}
我的尝试:
我尝试了第一个选项,10年前一位老师要求不这样做。同样的事情发生在我正在准备电气工程学位的朋友身上。
解决方案
我通常选择第一个选项(即使用返回
,如果合适的话)因为输入和查看的代码较少。
但这只是个人品味。
我更喜欢内部中断 - 它是绝对清晰的,如果有一个例如多个处理程序可以获得所需的值和休息时间更容易理解;
或者, ()测试可能会增长和增长,变得更难理解并且更容易编码出错。
此外,第一种方法允许您立即停止处理 - 而不是落入更多代码。使用'continue;'可以解决这个问题 - 但重点是什么?
独立的内部中断确实能说明问题,可以添加,修改和大括号之间的整个内容,否则很容易被操纵。
我喜欢转到第一个选项。
但我根据情况使用两者,没有绝对的规则。
Hi experts,
There is a debate between some acadimic and non academic programmers especially in the Franco world of whether to break inside a loop.
The bellow code illustrates what I mean.
What do you think best to use and why:
1.
for(int i=0; i<10; i++)
{
WriteLine("saisir une valeur");
int valeur = Convert.ToInt32(ReadLine());
if(valeur == 50){
WriteLine("Bingo! FOUND!");
break;
}
}
2.
bool found = false;
int tentatives = 0;
while(!found && ++tentatives <= 10)
{
WriteLine("saisir une valeur");
int valeur = Convert.ToInt32(ReadLine());
if(valeur == 50){
WriteLine("Bingo! FOUND!");
found = true;
}
}
What I have tried:
I tried the first option, a teacher 10 years ago asked to not do that. The same happened to a friend of mine who is preparing his degree in Electrical engineering.
解决方案
I usually go with the first option (even withreturn
, if appropriate) because of less code to type and see.
However it is just personal taste.
I would prefer the internal break - it's absolutely clear and makes it easier to follow if one has, for example, multiple handlers for a desired value and a break;
Alternatively, the while() test could grow and grow, becoming more difficult to understand and easier to code with errors.
Also, the first method allows you to stop processing immediately - rather than falling through to more code. Using 'continue;' would get around this - but what would be the point?
The independent internal breaks really speak for themselves and can be added to, modified, and the entire content between the curly-braces otherwise manipulated easily.
My like goes to first option.
But I use both depending on situation, there is no absolute rule.
这篇关于循环与休息或不休息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文