Okay, well let’s say I have a variable list of items. It can be any number of items. Each item can be either 0,1,2,3, or 4. So I make a loop.
foreach(item in allitems)
{
if (item == 0) continue;
do stuff for items 1-4.
}
Let’s say that every single item it goes through is 0. Well what if I want to execute a specific line of code in that case? Of course I could do something like
int count = 0
foreach(item in allitems)
{
if (item == 0) {count++; continue;}
do stuff for items 1-4.
}
if(count == allitems.Count())
{
do stuff
}
But I always felt cheap using count variables to do something like this. Is there any thing I can do that doesn’t feel like duct-taping a solution together?
There’s no need to use a count here – just keep a flag which is set if you get past the check, rather than within the check: