Best way of breaking from for loop
-
I have an example of for loop like
for( int I = 0; I < 100; i++)
{
//
//
//
If (condition)
{
//how to break when this condition is satisfied
Method1. Write ‘break’ here or
Method2. I = 100 (make I equal to 100)
}
}Which method is efficient to use? Why? Thanx
-
I have an example of for loop like
for( int I = 0; I < 100; i++)
{
//
//
//
If (condition)
{
//how to break when this condition is satisfied
Method1. Write ‘break’ here or
Method2. I = 100 (make I equal to 100)
}
}Which method is efficient to use? Why? Thanx
Imho break is more efficient because using break will just jump out of the loop while setting the variable 'I' to a value has to perform memory read/write operations, then it will jump to the condition-evaluation part, where it will check the variable against 100 and will then jump out of the loop. Am not sure how smart the compiler is but it might even optimize the later case to a simple break or "value-set" then break itself.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
-
I have an example of for loop like
for( int I = 0; I < 100; i++)
{
//
//
//
If (condition)
{
//how to break when this condition is satisfied
Method1. Write ‘break’ here or
Method2. I = 100 (make I equal to 100)
}
}Which method is efficient to use? Why? Thanx
I would use a control variable. I strongly advise not to set the loop variable, 'I' in your case, to a bogus value just to exit the loop because you might want to use the variable after the loop. Something like this:
bool bStop = false;
int nCount;
for( nCount = 0; (nCount < 100) && !bStop; ++nCount )
{
if( condition )
{
bSstop = true;
}
}// Here you'll be able to tell how far the loop has reached if you need to
if( nCount < 100 )
{
// Take proper action, whatever that may be....
}"It's supposed to be hard, otherwise anybody could do it!" - selfquote
"High speed never compensates for wrong direction!" - unknown -
I have an example of for loop like
for( int I = 0; I < 100; i++)
{
//
//
//
If (condition)
{
//how to break when this condition is satisfied
Method1. Write ‘break’ here or
Method2. I = 100 (make I equal to 100)
}
}Which method is efficient to use? Why? Thanx
Either one is fine. I prefer to go the
break;
route."The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon "Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
-
I have an example of for loop like
for( int I = 0; I < 100; i++)
{
//
//
//
If (condition)
{
//how to break when this condition is satisfied
Method1. Write ‘break’ here or
Method2. I = 100 (make I equal to 100)
}
}Which method is efficient to use? Why? Thanx
The
break
keyword was designed just for this. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
I have an example of for loop like
for( int I = 0; I < 100; i++)
{
//
//
//
If (condition)
{
//how to break when this condition is satisfied
Method1. Write ‘break’ here or
Method2. I = 100 (make I equal to 100)
}
}Which method is efficient to use? Why? Thanx
-
I have an example of for loop like
for( int I = 0; I < 100; i++)
{
//
//
//
If (condition)
{
//how to break when this condition is satisfied
Method1. Write ‘break’ here or
Method2. I = 100 (make I equal to 100)
}
}Which method is efficient to use? Why? Thanx
Resetting your loop counter variable is the worst thing to do. In this simple example, it might seem all OK to you, but what if there are other routines that will depend on the counter variable after the loop is complete? What if you needed to know how many times the loop executed?
vipin_nvk wrote:
Which method is efficient to use? Why?
Just exiting out of the loop with a break would be the ideal way, IMHO. You may use a control variable, as another user pointed out as well.
It is a crappy thing, but it's life -^ Carlo Pallini
-
The
break
keyword was designed just for this. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
I'm glad about your enthusiasm. :-D BTW it is CPallini, since
C
stands either forCarlo
or theC
programming language :rolleyes: , both requiring the capital letter. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
i use the break key word in my virii like so... if(DeployCondition) break ThatGuysPC->HDD;
killabyte wrote:
virii
Should be viruses (even if my ancestors didn't foresee such a proliferation :rolleyes: of). :-D
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]