Is there an equivalent method of performing the job of redo in C#? i.e. going back to the top of the loop and re-execute without checking conditions or increasing the loop counter. Thanks.
for (int i = 0; i < 100; i++)
{
do
{
DoYourStuff();
} while (ShouldWeDoThatAgain());
}
Do...while is like a standard while loop, except instead of checking its conditional before each iteration, it checks after. That way, the code inside the loop will always execute at least once. Stick that inside a for or foreach loop, and that should get you the behavior your want. This is a bit simpler than Simon's answer, as it doesn't require an extra variable, doesn't use continue, and doesn't mess with the loop counter at all.
Why not simply:
Although goto is not really everyone's favourite, it's quite readable in this case...
for(...)
{
redo:
//...
if (...)
goto redo;
}
No. The closest you'll get is something like this:
bool redoCalled = false:
for (int i = 0; i < 10; i++) {
if (redoCalled) {
i--;
redoCalled = false;
}
// other stuff here
if (redoWanted) {
redoCalled = true;
continue;
}
}
Related
As we all know, most for loops take on this form:
for (int i = 0; i < whatever; i++)
{
//do stuff
}
I am writing a method at the moment where it is not appropriate to include "i++" as in the format above because i is being updated in a loop nested within this loop. So for now, I have this:
List<Item> itemList = new List<Item>();
PopulateListOfItems(itemList);
List<List<Item>> itemSubListList = new List<List<Item>>();
for (int i = 0; i < itemList.Count; //i doesn't need updating here)
{
List<Item> subList = new List<Item>();
for (int j = 0; j < subListSize && i < itemList.Count; j++)
{
subList.Add(itemList[i++]);
}
itemSubListList.Add(subList;
}
Is there a standard way to write such a loop, perhaps including some sort of dummy code where i++ would normally go to make it clear no update is needed? Perhaps something like...
for (int i = 0; i < whatever; true)
{
//do stuff
}
... or am I just at the end of a long week and my brain is too fried to realize I really should just be using a while loop or something?
By the way, I've written this in C#, yes, but this can apply to a variety of languages.
Just omit it:
for (int i = 0; i < whatever; )
{
//do stuff
}
In fact, all parts of the for-loop are optional (in C# at least, this may not be the same for all C-like languages). The following creates an infinite loop:
for (;;)
{
//do stuff
}
Of course, you could also accomplish this with a while-loop, as the other answers have suggested. Both solutions are perfectly valid and which one you choose is mostly a matter of style. In general, you should pick the solution which you find most clearly communicates to the reader what the code is trying to do.
Just use a while loop instead of a for loop.
var i = 0;
while (i < whatever)
{
// Do stuff including updating i.
}
The for loop construct is just syntactic sugar for a while loop, and all for loops decompose into while loops.
for(int i = 0; i < whatever; i++)
{
//do stuff
}
ultimately means:
{
int i = 0;
while(i < whatever)
{
//do stuff
i++;
}
}
(note the additional curly braces for scoping of the integer)
Any parts of the for loop that you omit are also omitted from the decomposed while loop and thus you have to handle them manually. If you don't use all three parts, you really may as well just write a while loop (especially if one of the parts you omitted was the variable initializer, because of the variable scope I mentioned).
I want to make a loop using an already-defined iterator.
At present I am using
int i;
while (i<10)
{
Console.Writeline(i);
i++;
}
This is ugly because someone else might later remove the i++. If it is separated from the while statement by a large block of code, it will not be clear what it is for.
What I'd really like is something like
int i;
for (i<10; i++)
{
Console.Writeline(i);
}
This makes it clear what's going on, but it's not valid C#.
The best I've come up with so far is
int i;
for (int z; i<10; i++)
{
Console.Writeline(i);
}
But that's ugly. Does C# give me an elegant way of doing it?
Well, you don't have to have anything inside the first part of the loop:
for (; i < 10; i++)
{
Console.WriteLine(i);
}
Is that what you're looking for? Personally I would typically try to avoid this sort of situation anyway though - I find it pretty unusual to want a loop without a new variable.
Just use an empty first part of the "for":
int i;
for (; i<10; i++)
{
Console.Writeline(i);
}
You can also assign the variable to itself in the first part of the loop like this:
int i;
for (i=i; i<10; i++)
{
Console.Writeline(i);
}
I find that it's slightly clearer than leaving the first part of the loop blank, however Visual Studio will give you a warning "Assignment made to the same variable; did you mean to assign it to something else?"
JavaScript supports a goto like syntax for breaking out of nested loops. It's not a great idea in general, but it's considered acceptable practice. C# does not directly support the break labelName syntax...but it does support the infamous goto.
I believe the equivalent can be achieved in C#:
int i = 0;
while(i <= 10)
{
Debug.WriteLine(i);
i++;
for(int j = 0; j < 3; j++)
if (i > 5)
{
goto Break;//break out of all loops
}
}
Break:
By the same logic of JavaScript, is nested loop scenario an acceptable usage of goto? Otherwise, the only way I am aware to achieve this functionality is by setting a bool with appropriate scope.
My opinion: complex code flows with nested loops are hard to reason about; branching around, whether it is with goto or break, just makes it harder. Rather than writing the goto, I would first think really hard about whether there is a way to eliminate the nested loops.
A couple of useful techniques:
First technique: Refactor the inner loop to a method. Have the method return whether or not to break out of the outer loop. So:
for(outer blah blah blah)
{
for(inner blah blah blah)
{
if (whatever)
{
goto leaveloop;
}
}
}
leaveloop:
...
becomes
for(outer blah blah blah)
{
if (Inner(blah blah blah))
break;
}
...
bool Inner(blah blah blah)
{
for(inner blah blah blah)
{
if (whatever)
{
return true;
}
}
return false;
}
Second technique: if the loops do not have side effects, use LINQ.
// fulfill the first unfulfilled order over $100
foreach(var customer in customers)
{
foreach(var order in customer.Orders)
{
if (!order.Filled && order.Total >= 100.00m)
{
Fill(order);
goto leaveloop;
}
}
}
leaveloop:
instead, write:
var orders = from customer in customers
from order in customer.Orders;
where !order.Filled
where order.Total >= 100.00m
select order;
var orderToFill = orders.FirstOrDefault();
if (orderToFill != null) Fill(orderToFill);
No loops, so no breaking out required.
Alternatively, as configurator points out in a comment, you could write the code in this form:
var orderToFill = customers
.SelectMany(customer=>customer.Orders)
.Where(order=>!order.Filled)
.Where(order=>order.Total >= 100.00m)
.FirstOrDefault();
if (orderToFill != null) Fill(orderToFill);
The moral of the story: loops emphasize control flow at the expense of business logic. Rather than trying to pile more and more complex control flow on top of each other, try refactoring the code so that the business logic is clear.
I would personally try to avoid using goto here by simply putting the loop into a different method - while you can't easily break out of a particular level of loop, you can easily return from a method at any point.
In my experience this approach has usually led to simpler and more readable code with shorter methods (doing one particular job) in general.
Let's get one thing straight: there is nothing fundamentally wrong with using the goto statement, it isn't evil - it is just one more tool in the toolbox. It is how you use it that really matters, and it is easily misused.
Breaking out of a nested loop of some description can be a valid use of the statement, although you should first look to see if it can be redesigned. Can your loop exit expressions be rewritten? Are you using the appropriate type of loop? Can you filter the list of data you may be iterating over so that you don't need to exit early? Should you refactor some loop code into a separate function?
IMO it is acceptable in languages that do not support break n; where n specifies the number of loops it should break out.
At least it's much more readable than setting a variable that is then checked in the outer loop.
I believe the 'goto' is acceptable in this situation. C# does not support any nifty ways to break out of nested loops unfortunately.
It's a bit of a unacceptable practice in C#. If there's no way your design can avoid it, well, gotta use it. But do exhaust all other alternatives first. It will make for better readability and maintainability. For your example, I've crafted one such potential refactoring:
void Original()
{
int i = 0;
while(i <= 10)
{
Debug.WriteLine(i);
i++;
if (Process(i))
{
break;
}
}
}
bool Process(int i)
{
for(int j = 0; j < 3; j++)
if (i > 5)
{
return true;
}
return false;
}
I recommend using continue if you want to skip that one item, and break if you want to exit the loop. For deeper nested put it in a method and use return. I personally would rather use a status bool than a goto. Rather use goto as a last resort.
anonymous functions
You can almost always bust out the inner loop to an anonymous function or lambda. Here you can see where the function used to be an inner loop, where I would have had to use GoTo.
private void CopyFormPropertiesAndValues()
{
MergeOperationsContext context = new MergeOperationsContext() { GroupRoot = _groupRoot, FormMerged = MergedItem };
// set up filter functions caller
var CheckFilters = (string key, string value) =>
{
foreach (var FieldFilter in MergeOperationsFieldFilters)
{
if (!FieldFilter(key, value, context))
return false;
}
return true;
};
// Copy values from form to FormMerged
foreach (var key in _form.ValueList.Keys)
{
var MyValue = _form.ValueList(key);
if (CheckFilters(key, MyValue))
MergedItem.ValueList(key) = MyValue;
}
}
This often occurs when searching for multiple items in a dataset manually, as well. Sad to say the proper use of goto is better than Booleans/flags, from a clarity standpoint, but this is more clear than either and avoids the taunts of your co-workers.
For high-performance situations, a goto would be fitting, however, but only by 1%, let's be honest here...
int i = 0;
while(i <= 10)
{
Debug.WriteLine(i);
i++;
for(int j = 0; j < 3 && i <= 5; j++)
{
//Whatever you want to do
}
}
Unacceptable in C#.
Just wrap the loop in a function and use return.
EDIT: On SO, downvoting is used to on incorrect answers, and not on answers you disagree with. As the OP explicitly asked "is it acceptable?", answering "unacceptable" is not incorrect (although you might disagree).
I'm trying to run a basic loop that will find a specific value in a dataview grid. I cannot figure out whats going on with the code, since the for loop exits before evaluating its basic condition.
private void SearchDataViewGrid(string FileName)
{
//finds the selected entry in the DVG based on the image
for (int i = 0; i == dataPartsList.Rows.Count ; i++)
{
if(FileName == dataPartsList.Rows[i].Cells[3].Value.ToString())
{
dataPartsList.Rows[i].Selected = true;
}
}
}
The program doesn't crash, but i get an error on my 'i' variables declaring that it has been optimised away. Tried a few easy fixes i found online but nothing seems to keep it.
I have verified that the string i am passing is the correct one, and my 'dummy' DVG returns a value of 14 for the number of rows contained. Even if i remove the 'if' statement inside of the for loop, i still get the same error.
The condition cond in the middle of for(init; cond; update) is not an until condition but a while condition.
So you need to change it to
for (int i = 0; i < dataPartsList.Rows.Count ; i++)
The problem is your conditional is i == dataPartsList.Rows.Count so the body will only execute when these two values are equal. This guarantees your loop will never execute. You need to change your conditional to be < instead of ==
for (int i = 0; i < dataPartsList.Rows.Count ; i++) {
...
}
This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Breaking out of a nested loop
How to break out of 2 loops without a flag variable in C#?
Hello I have a function that has nested loops. Once the condition has been met, I want to break out of nested loops. The code is something like this below:
foreach (EmpowerTaxView taxView in taxViews)
{
foreach (PayrollEmployee payrollEmployee in payrollEmployees)
{
//PayStub payStub = payrollEmployee.InternalPayStub;
IReadOnlyList<PayrollWorkLocation> payrollWorkLocations = payrollEmployee.PayrollWorkLocations;
foreach (PayrollWorkLocation payrollWorkLocation in payrollWorkLocations)
{
Tax tax = GetTaxEntity(payrollWorkLocation, taxView.BSITypeCode, taxView.BSIAuthorityCode,
paidbyEr, resCode);
if (tax != null && tax.Rate.HasValue)
{
taxRate = tax.Rate.Value;
break;
}
}
}
}
Unfortunately, break comes out of only one loop. I want to break out of the whole thing. Please, I know some people have suggested goto: statement. I am wondering is there any other way around, such writing some LINQ queries to the same effect.
Any ideas and suggestions are greatly appreciated !
Two options suggest themselves as ways of getting out without having an extra flag variable to indicate "you should break out of the inner loop too". (I really dislike having such variables, personally.
One option is to pull all of this code into a separate method - then you can just return from the method. This would probably improve your code readability anyway - this really feels like it's doing enough to warrant extracting into a separate method.
The other obvious option is to use LINQ. Here's an example which I think would work:
var taxRate = (from taxView in taxViews
from employee in payrollEmployees
from location in employee.PayrollWorkLocations
let tax = GetTaxEntity(location, taxView.BSITypeCode,
taxView.BSIAuthorityCode,
paidbyEr, resCode)
where tax != null && tax.Rate.HasValue
select tax.Rate).FirstOrDefault();
That looks considerably cleaner than lots of foreach loops to me.
Note that I haven't selected tax.Rate.Value - just tax.Rate. That means the result will be a "null" decimal? (or whatever type tax.Rate is) if no matching rates are found, or the rate otherwise. So you'd then have:
if (taxRate != null)
{
// Use taxRate.Value here
}
Well, you could use the dreaded goto, refactor your code, or this:
// anon-method
Action work = delegate
{
for (int x = 0; x < 100; x++)
{
for (int y = 0; y < 100; y++)
{
return; // exits anon-method
}
}
};
work(); // execute anon-method
You could use a flag variable.
bool doMainBreak = false;
foreach (EmpowerTaxView taxView in taxViews)
{
if (doMainBreak) break;
foreach (PayrollEmployee payrollEmployee in payrollEmployees)
{
if (doMainBreak) break;
//PayStub payStub = payrollEmployee.InternalPayStub;
IReadOnlyList<PayrollWorkLocation> payrollWorkLocations = payrollEmployee.PayrollWorkLocations;
foreach (PayrollWorkLocation payrollWorkLocation in payrollWorkLocations)
{
Tax tax = GetTaxEntity(payrollWorkLocation, taxView.BSITypeCode, taxView.BSIAuthorityCode,
paidbyEr, resCode);
if (tax != null && tax.Rate.HasValue)
{
taxRate = tax.Rate.Value;
doMainBreak = true;
break;
}
}
}
}