Click here to Skip to main content
15,909,591 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi
in my method i have IF statement in for loop in another for loop , if once IF condition fails the execution of method should be terminated . i am new please help me how to terminate method
Posted

Since everyone else is overstating the obvious, you can use break or return to exit any loop.
 
Share this answer
 
Use break;

Read about it: break (C# Reference)[^]
 
Share this answer
 
As other answers have said, just use the break statement to exit your loop. Consider the following code

C#
public void TestMethod()
{
    for (int i = 0; i < 1000; ++i)
    {
        if (i == 10)
            break;
    }
}


In this code, we've set a loop to iterate 1000 times.

However, the if statement is checking for when i == 10, when this happens the break statement will be hit and the loop will exit.

Because there is no other code after this, the method will exit as soon as we exit the loop.
 
Share this answer
 
Hi,
You can exit from the loop by using break; in the else of the if condition you're evaluating.

Hope this helps

Laurence
 
Share this answer
 
Comments
srinivasvempa 23-Feb-11 8:07am    
thanx, but method not terminating
Laurence1234 23-Feb-11 8:09am    
Hmm, that's odd. Could you post your code please?
Use break; statement to get out of loop.
see here[^]
 
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