Breaking out of loops, in C?

Started by
42 comments, last by pex22 19 years, 4 months ago
Hm, I have always used while loops in such a case, and I really don't see why not. Then you can just do something like this:
int index=0;bool Found=false;while(index<Array.Size&&!Found){	if(Array[index]==Item) 		Found=true;	else		index++;}if(Found) 	return index; else 	return -1;


No need for breaks and it makes a lot more sense in my opinion, because you can tell from looking at the while that it only loops until something is found, which is not the case with the for-loop.
Advertisement
Quote:Original post by thedustbustr
Offtopic: a legitimate use of goto
*** Source Snippet Removed ***Is there a cleaner way to do this?


Exceptions :
int main(int argc, char* argv[]){        Kernel* k = 0;        try        {	     logger.start(true);	     k =new Kernel(argc, argv);	     k->init();	     k->mainloop();	     k->exit();        }        catch (Exception & e)        {	     LOG("The shit hit the fan : ",e.errorMessage());             }        delete k;	logger.stop();	return 0;}


Off course init,mainloop and exit should throw exceptions.

And to call exit when init or mainloop fail you could do something like this :

void Kernel::init(){        try { do stuff ... }        catch (...)        {               exit();        }}


Or like this to avoid the extra indentation levels :
void Kernel::init()try{       }catch (...){        exit();}
"THE INFORMATION CONTAINED IN THIS REPORT IS CLASSIFIED; DO NOT GO TO FOX NEWS TO READ OR OBTAIN A COPY." , the pentagon
Your lecturer is wrong. "break" is perfectly fine as long as you use it where appropriate. If you are using it in every loop in order to break out of the loop (rather than mostly using a condition at the top of the loop) then you're using it too much. However, there ARE instances when a break statement is the neatest construct to use to break out of a loop. For example you want to say "If this condition becomes true in the body of the loop, then break out of the loop without doing any further processing, even if the condition at the start of the loop remains true." "break" would be an appropriate way to do that. In this situation "continue" would NOT be appropriate because it simply jumps execution on to the closing brace of the loop before testing the condition again.
Our teacher told us not to use 'break' in loops, too. i dont know about whiles, but it can confuse you (and ignore the meaning of the 'third parameter of for', and make the 'for' loop have more than 1 condition)-

if you 'break' a for loop, so the third parameter of the 'for' will not be called.
for example,
for (int i=0;i<1;i++) break; printf("%d",i);

software will print 0 instead of 1 (if you cut the 'break' word, it will print 1)

maybe thats why he/she said not to use breaks in a for loop.
for's should call the third parameter after every loop, right? but it is not called and it may confuse you (maybe)- did the loop called the third parameter this time or not?

for (int i=0;i<4;i++){if (i==3)break;}

it also 'decides' the condition of the 'for' statement, but you already defined the condition. more than 1 condition? weird and maybe also unreadable, i dont know. it 're-define' the condition of the 'for' loop.
so in the code above, the first condition will never return 'true'.
the loop body is not the place for the condition of the loop body, its like to create x from x, i think. or let x decide things for itself (which is a good thing for humans, but for machines?)


The second parameter should be the only condition of the loop, and the third should always be called after the condition returns 'true'.
Shouldn't the 3 parameters always decide what will be to the loop body? (so the loop body can't change it)
maybe thats why you can't use 'break's in exams.

Isn't it like the 3 parameters are what god decide and the human is the 'loop body'?
the human can't decide by himself what was decided for him (e.g. body color, destiny), its already made. so he'll have to stay with it forever.

hope i found the right reason for not using it in exams,
pex.

[Edited by - pex22 on December 17, 2004 11:56:01 AM]
pex.

This topic is closed to new replies.

Advertisement