Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop Looping C#?

Tags:

c#

c#-3.0

c#-4.0

How to stop my Loop if the value is already existing?

here's my code in C#...

foreach (ArrayList item in ArrData)
{    
  HCSProvider.NewProviderResult oResult;
  oResult = oHCSProvider.CreateNewProvider(providercode, oProviderDetail)

  DBInterface ProviderDetail = new DBInterface(); 

  ProviderDetail.InsertProvider(Convert.ToInt64(providercode), Convert.ToString(oProviderDetail));
}
like image 481
im useless Avatar asked Mar 30 '11 05:03

im useless


People also ask

How do I stop a loop in C?

break command (C and C++) The break command allows you to terminate and exit a loop (that is, do , for , and while ) or switch command from any point other than the logical end. You can place a break command only in the body of a looping command or in the body of a switch command.

How do you stop an infinite loop in C?

There is no way to stop an infinite loop. However, you can add a condition inside of the loop that causes it to break, or you can call the exit() function inside of the loop, which will terminate your program. Highly active question.

How do you terminate a loop?

break terminates the execution of a for or while loop. Statements in the loop after the break statement do not execute. In nested loops, break exits only from the loop in which it occurs.

How do you stop a repeat loop?

To break out of a while loop, you can use the endloop, continue, resume, or return statement.


6 Answers

you can skip iteration with

continue; 

and stop loop with

break;
like image 161
Paweł Smejda Avatar answered Oct 06 '22 03:10

Paweł Smejda


If you are inside a loop and want to abort the loop execution and jump to the code after the loop, insert a break; statement.

If you only want to stop the current loop iteration, and continue with the rest of the loop, add a continue; statement instead.

like image 35
Øyvind Bråthen Avatar answered Oct 06 '22 02:10

Øyvind Bråthen


You can stop any loop in c# by a break statement

You can write something like this:

foreach(var o in list)
{
 if (o.SomeValue == 1)
 {
   break;
 }
}
like image 43
Rafal Spacjer Avatar answered Oct 06 '22 02:10

Rafal Spacjer


Continue, break and goto are used in C# for skipping the loop.

Continue Skips the execution of current iteration

Continue;

break Comes out of the loop and continues the next statement after the loop

break;

goto is normally not recommend, but still can be used to move control out of the loop

goto Outer;
like image 24
Ajith Avatar answered Oct 06 '22 03:10

Ajith


You can put a break command to exit from For Loop.

foreach(var item in items)
{
if(item == myitem)
{
break;
}
Console.WriteLine(item);
}
like image 42
Anuraj Avatar answered Oct 06 '22 04:10

Anuraj


you can easily stop your lop on a condition with the break statement!

Small example:

var arr = new [] {1,2,3,4,5,6,7};
int temp = 0;

foreach(var item in arr)
{
    temp = item +1;
    if(temp == 5)
    {
        break;
            //...
    }
     //do something
}   
like image 42
cordellcp3 Avatar answered Oct 06 '22 04:10

cordellcp3