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));
}
you can skip iteration with
continue;
and stop loop with
break;
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.
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;
}
}
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;
You can put a break command to exit from For Loop.
foreach(var item in items)
{
if(item == myitem)
{
break;
}
Console.WriteLine(item);
}
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
}
Related
Hi guys how can you repeat one iteration in a foreach?
foreach (string line in File.ReadLines("file.txt"))
{
// now line == "account", next line == "account1"
if (line.Contains("a"))
//next loop take "account1";
else
// need to set that next loop will take line == "account" again
}
How to do it?
While I don't fully understand your example, I think I understand your question. I had the same problem and was able to come up with a solution: include a while loop within the foreach. In your example it would look like this:
foreach (string line in File.ReadLines("file.txt"))
{
bool repeat = true;
while (repeat)
{
// now line == "account", next line == "account1"
if (line.Contains("a"))
{
//do your logic for a break-out case
repeat = false;
}
else
{
//do your logic for a repeat case on the same foreach element
//in this instance you'll need to add an "a" to the line at some point, to avoid an infinite loop.
}
}
}
I know I'm very late to the game, but hopefully this will be helpful for anyone else who stumbles in here with the same problem.
There is no need to change your code, assuming it only has an if/else construct in the loop.
When the if condition evaluates to true the else will not execute and the loop resumes.
In a more complex where you want to immediately resume the loop and ensure nothing else following the condition executes, use the continue statement:
The continue statement passes control to the next iteration of the enclosing while, do, for, or foreach statement in which it appears.
foreach (string line in File.ReadLines("file.txt"))
{
// now line == "account", next line == "account1"
if (line.Contains("a"))
continue;
else
// need to set that next loop will take line == "account" again
// more stuff that we don't want to execute if line.Contains("a")
}
I guess this might also helpful if someone else come
for (int i = 0; i < inventoryTimeBlocks.Count; i++)
{
if (line.Contains("a"))
//next loop take "account1";
else
{
if(i > 0)
{
i = i - 1;
continue;
}
}
}
I have a While loop that reads a line of a file.txt. I also have a method named VerifyPhoto that returns true/false I want to go to next item of the while loop if the returned value is false. How Could I do that ? I tried break and return but it just leave all and back to the form...
while (!reader.EndOfStream)
{
if(VerifyPhoto(filed.matriculation) == false)
{
//go to the next line of the file.txt
}
}
You may want to get familiar with other control statement: continue
[EDIT] Newest version of the doc: continue, thanks Jeppe.
continue; (some more to make it 30 chars)
Depending on your actual code, maybe you could simply invert the boolean test, so you do something only if VerifyPhoto returns true:
while (...)
{
if(VerifyPhoto(filed.matriculation))
{
// Do the job
}
}
The continue statement passes control to the next iteration of the enclosing iteration statement in which it appears
while (!reader.EndOfStream)
{
if(VerifyPhoto(filed.matriculation) == false)
{
continue;
//go to the next line of the file.txt
}
}
Am I missing something in the way you are doing this?
Do you read the first line before starting the loop?
if so don't you need something like
**string line;**
while (!reader.EndOfStream)
{
if(VerifyPhoto(filed.matriculation) == false)
{
//go to the next line of the file.txt
**line = file.ReadLine();**
}
}
If you're trying to read line by line then File.ReadLines may be useful.
Also what you're looking for is the continue statement.
string myFile = #"c:\path\to\my\file.txt";
foreach(string line in File.ReadLines(myFile))
{
//Do stuff
//if(!VerifyPhoto())
// continue;
//Do other logic
}
I have a C# while loop, does a continue in this loop issue the same behaviour as moving to the next item in the loop? Exactly the same as for a "for loop".
For example see any problems in the following code sample?
while ((line = file.ReadLine()) != null)
{
string messageDownloadID = line ;
if (String.IsNullOrEmpty(messageDownloadID))
{
continue;
}
}
Thanks in advance.
I have a C# while loop, does a continue in this loop issue the same behaviour as moving to the next item in the loop? Exactly the same as for a "for loop".
No, a continue does not have the behavior of moving to the next item unless the part of the while block that moves to the next item is part of the loop condition or occurs and is executed before the continue statement. A continue will move you to the next iteration of the loop. The continue statement always starts a new iteration in the nearest enclosing while, do, for or foreach statement. But if in a while loop, say, i++ occurs after the continue statement it will not be executed. It is NOT exactly the same as a for loop.
The semantics of continue are like so:
while(condition) {
statement
continue
statement
}
is equivalent to
top:
while(condition) {
statement
goto top;
statement
}
In this case, the statements after the continue will never be executed if the continue is executed. But, for example, these two loops have different semantics:
for (int i = 0; i < 10; i++) {
if (i == 5) {
continue;
}
Console.WriteLine(i);
}
int j = 0;
while (j < 10) {
if (j == 5) {
continue;
}
Console.WriteLine(j);
j++;
}
The former loop will print 0 through 9 on the console while the second will enter an infinite loop after print 0 through 4 on the console.
For example see any problems in the following code sample?
while ((line = file.ReadLine()) != null) {
string messageDownloadID = line;
if (String.IsNullOrEmpty(messageDownloadID)) {
continue;
}
}
Well, it depends. First, this loop isn't doing anything really except moving the file pointer in file to the end of the file. Now, assuming that you meant to have statements after the if statement like so
while ((line = file.ReadLine()) != null) {
string messageDownloadID = line;
if (String.IsNullOrEmpty(messageDownloadID)) {
continue;
}
ProcessMessageDownloadID(messageDownloadID);
}
then your code is fine. This is because the part of the while that moves to the next item is part of the while condition (see my bolded statement above) and thus will be executed every iteration of the loop. Your code is a standard pattern for parsing a file and skipping over items that can't be parsed (because they are, say, blank).
Yes, continue moves to the next iteration of the loop.
Yes, a continue call will restart the loop. Kind of like a goto to a label at the start of the loop.
Yes.
"A while loop can be terminated when a break, goto, return, or throw statement transfers control outside the loop. To pass control to the next iteration without exiting the loop, use the continue statement."
I am writing a for loop with multiple if statements.
Is it possible that if the if statement (or one part of it) in the for statement evaluates to false, then the loop does not exit but the integer to iterates increments by one and continues through the loop (I need functionality like the continue; keyword).
Example:
for (int i = 0; i <= Collection.Count && Collection[i].Name != "Alan"; i++)
{
// If name is not Alan, increment i and continue the loop.
}
Is this possible?
Thanks
You need functionality like the continue keyword - have you considered using the continue keyword, then?
Update: Your example code is hard to decipher the intention of.
for (int i = 0; i <= Collection.Count && Collection[i].Name != "Alan"; i++)
{
// If name is not Alan, increment i.
}
The for loop has three parts to it, separated by two semi-colons. The first part initializes the loop variable(s). The second part is an expression that is evaluated each time an iteration is about to start; if it is false, the loop terminates. The third part executes after each iteration.
So your loop above will exit at the first "Alan" it encounters, and also it will increment i every time it finishes an iteration. Finally, if there are no Alans, it will execute the last time with i equal to Collection.Count, which is one larger than the maximum valid index into the collection. So it will throw an exception for sure, as you try to access Collection[i] when i is out of range.
Maybe you want this:
foreach (var item in Collection.Where(i => i.Name != "Alan"))
{
// item is not an "Alan"
}
You can think of the Where extension method as a way of filtering a collection.
If this seems obscure, you can achieve the same thing with the continue keyword (as you guessed):
foreach (var item in Collection)
{
if (item.Name == "Alan")
continue;
// item is not an "Alan"
}
Or you can just put the code in the if's block:
foreach (var item in Collection)
{
if (item.Name != "Alan")
{
// item is not an "Alan"
}
}
Do you mean like this?
for (int i = 0; i < 100; ) {
if (!condition1) {
i++;
}
if (!condition2) {
i++;
}
if (!condition3) {
i++;
}
}
Do you want the incrementor for finishing the for loop to be in the body of the loop?
I am not sure I understand correctly. You have a for loop something like this
for (int i = 0; i < 10; i++)
{
// do something
if (!b1)
i++
// do something
}
Edit:
If you use continue it increments i for only once. If you use i++ in loop it increments twice obviously. If you only want to icrement on a condition, Use the for loop like this
for (int i = 0; i < 10) // and this is very similar to a while loop.
From your sample code, I think you are searching for the name "Alan".
Is this correct?
If so, structure your loop like:
for (int i = 0; i < Collection.Count; i++)
{
if (Collection[i].Name == "Alan")
{
break; // We found the name we wanted!
}
// Otherwise: Keep going to look for the name further on.
}
if (i == Collection.Count)
{
Console.WriteLine("Alan is not found");
}
else
{
Console.WriteLine("Alan found at position {0}", i);
}
In the following C# code snippet
I have a 'while' loop inside a 'foreach' loop and I wish to jump to the next item in 'foreach' when a certain condition occurs.
foreach (string objectName in this.ObjectNames)
{
// Line to jump to when this.MoveToNextObject is true.
this.ExecuteSomeCode();
while (this.boolValue)
{
// 'continue' would jump to here.
this.ExecuteSomeMoreCode();
if (this.MoveToNextObject())
{
// What should go here to jump to next object.
}
this.ExecuteEvenMoreCode();
this.boolValue = this.ResumeWhileLoop();
}
this.ExecuteSomeOtherCode();
}
'continue' would jump to the beginning of the 'while' loop not the 'foreach' loop.
Is there's a keyword to use here, or should I just use goto which I don't really like.
Use the break keyword. That will exit the while loop and continue execution outside it. Since you don't have anything after the while, it would loop around to the next item in the foreach loop.
Actually, looking at your example more closely, you actually want to be able to advance the for loop without exiting the while. You can't do this with a foreach loop, but you can break down a foreach loop to what it actually automates. In .NET, a foreach loop is actually rendered as a .GetEnumerator() call on the IEnumerable object (which your this.ObjectNames object is).
The foreach loop is basically this:
IEnumerator enumerator = this.ObjectNames.GetEnumerator();
while (enumerator.MoveNext())
{
string objectName = (string)enumerator.Value;
// your code inside the foreach loop would be here
}
Once you have this structure, you can call enumerator.MoveNext() within your while loop to advance to the next element. So your code would become:
IEnumerator enumerator = this.ObjectNames.GetEnumerator();
while (enumerator.MoveNext())
{
while (this.ResumeWhileLoop())
{
if (this.MoveToNextObject())
{
// advance the loop
if (!enumerator.MoveNext())
// if false, there are no more items, so exit
return;
}
// do your stuff
}
}
The following should do the trick
foreach (string objectName in this.ObjectNames)
{
// Line to jump to when this.MoveToNextObject is true.
this.ExecuteSomeCode();
while (this.boolValue)
{
if (this.MoveToNextObject())
{
// What should go here to jump to next object.
break;
}
}
if (! this.boolValue) continue; // continue foreach
this.ExecuteSomeOtherCode();
}
The break; keyword will exit a loop:
foreach (string objectName in this.ObjectNames)
{
// Line to jump to when this.MoveToNextObject is true.
while (this.boolValue)
{
// 'continue' would jump to here.
if (this.MoveToNextObject())
{
break;
}
this.boolValue = this.ResumeWhileLoop();
}
}
Use goto.
(I guess people will be mad with this response, but I definitely think it's more readable than all other options.)
You can use "break;" to exit the innermost while or foreach.