Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

question about exit() method

Status
Not open for further replies.

jayjay60

Programmer
Jun 19, 2001
97
FR
consider a for loop like that:
for(i=0;i<=Bound;i++)
In this loop i would like to do an &quot;if&quot; test which if the test succeed i would like to go to the next iteration and if not i do another thing. For example:
if(i>Max||i<Min)
//at this moment if the test succeed go to the next iteration;
else
//call another method.

So could i use exit() method or how could i do that?

thanks in advance

jayjay60
 
if you want to get out of the loop, use break

if you want to get to the beginning of the loop again use continue

if you want to get to a certain point in the code, set a value and either switch on that value or use if conditionals.

If the condition will exist INSIDE the loop, do not use contiue or break. If some value will be set within the loop but then checked outside the loop, use break and if the condition is checked at the beginning of the loop , continue would be fine.

continue however, is usually used to avoid certain situations. For example, suppose you wanted to rename all the files in a specific folder and sub folders. CFileFind can return all the files in the current directory, however it also lists directorys. You dont want to rename the directorys so you do a check

if(finder.IsDirectory())
continue;

an example of break is if you were looking for a specific value in an array of 10000 integers. The value had to exist only once.

Code:
for(int i = 0;i<10000;i++)
   if(array[i] == PREDETERMINED_VALUE)
   {  
       found = true;
       break;
   }

exit on the other hand will cause you to kill the calling process and return a value. On a unix system, you can query this value to see how things went and with windows, it can be checked with a batch file.


Matt
 
Well Matt has said is correct.

exit() will take you completely out of the loop that is
while( condition)
{

}
// the control will come here..

as for ur other part of question you can go to the next iteration like this

while (condition)
{

int a; // dummy variable.
a=2;
if (a)
{
cout<< a;
continue(); // this is take the control back to while
// loop and iterates the loop again without
//going to the next statement.
}
a=5;
cout<<a;
}

This program will only print a=2 not a=5. Because the control is not going to the next statement when you use continue.

if you use break() then the control will go to the next statement .

I hope this satisfies your need. If you still have any problem then write back.

Anand
 
thanks to the both for your answer, i'm going to test it in my application.

jayjay
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top