Click here to Skip to main content
15,885,668 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
C++
for(int i=0; i<10; i++)
{
  WriteLine("saisir une valeur");
  int valeur = Convert.ToInt32(ReadLine());
  if(valeur == 50){
       WriteLine("Bingo! FOUND!");
       break;
  }
}

2.
C++
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.
Posted
Updated 27-Oct-17 11:36am
v2
Comments
Richard MacCutchan 27-Oct-17 11:30am    
It is largely a question of style and personal choice. However, option 2 makes it clear what condition will cause the loop to break.
W Balboos, GHB 27-Oct-17 11:41am    
Isn't that also absolutely clear in example 1? I.e., valeur==50 ?
Richard MacCutchan 27-Oct-17 11:53am    
It is in that example, but in a longer and more complex loop it may not be. Personally I generally choose option 1.
W Balboos, GHB 27-Oct-17 11:56am    
My head works the other way for increasing complexity - it must be some sort of French thing.
Richard MacCutchan 27-Oct-17 12:01pm    
I don't have any real preference either way, I just tend to write what seems to make sense at the time. And then spend the next few hours trying to debug it.

I usually go with the first option (even with return, if appropriate) because of less code to type and see.
However it is just personal taste.
 
Share this answer
 
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.
 
Share this answer
 
My like goes to first option.
But I use both depending on situation, there is no absolute rule.
 
Share this answer
 
I don't think the two examples shown are comparable. When the loop on the second example exits, information persists in a variable 'tentatives that will tell you how many iterations occurred to find the match ... if it found the match. The first example does not persist the iteration count.

The usual discriminating factor in the choice of 'for vs. 'while is whether you know the number of iterations to be carried out. I find it helpful to C# students to suggest thinking of 'for as 'repeat this many times;' with 'while I suggest an analogy of 'keep on doing this as long as whatever.' With 'do-while 'keep doing this until whatever.'
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900