C# for loop - How do I run the loop body with different variables?
Make Height
an array.
Something like this:
var result = false;
decimal[] height = { 0.1, 0.2 }; // use your values here
for (var ii = 0; ii < height.Length; ii++) {
if (0.3 + height[ii] > 2) {
result = true;
break;
}
}
Arrays and lists
Whenever you deal with variables with a numbered suffix, nine times out of ten you should be using an array or a list. So instead of:
float height1 = 0.1;
float height2 = 1.8;
You do:
float[] heights = new float[] { 0.1, 1.8 };
//OR
List<float> heights = new List<float>() { 0.1, 1.8 };
Which allows you to use loops to process all elements of the array/list:
for(int i = 0; i < heights.Length; i++) // use heights.Count for a List<float>
{
// do something with heights[i]
}
Note that for processing each element of a collection, you can instead use a foreach
, which means you don't have to manually juggle the index value:
foreach(var height in heights)
{
// do something with height
}
You can also use LINQ here, which I will get to.
Evaluation logic
I'm not sure why you're doing this:
0.3 + height >= 2
instead of the more sensical:
height >= 1.7
There are cases where it makes sense to write out a calculation (e.g. when expressing a timespan in milliseconds), but this doesn't seem to be one of those cases.
This is just an educated guess though, I can't know for a fact if it makes sense in your case to explicitly express 2 and 0.3 as numbers by themselves (it depends on your domain). However, the answer will continue under the assumption that 1.7 is equally meaningful. If it isn't, then you can substitute your original evaluation back into the code samples provided.
Whenever you deal with logic of this shape:
if( a == b )
{
myBoolean = true;
}
else
{
myBoolean = false;
}
This can always be reduced to:
myBoolean = (a == b);
Your logic is slightly different because you have a fallback evaluation, but the same principle applies here, giving you:
result = (height1 >= 1.7) || (height2 >= 1.7);
result
will be true
only if either of the heights is 1.7 or more.
That being said, if you start using an array, this logic isn't necessary anymore, but I wanted to point it out anyway as it dramatically improves readability and it's a basic skill you should improve to become a better developer.
LINQ
I'm going to skip the full introduction (you can find tutorials online), but what LINQ does for you is essentially give you premade loop functions that perform a particular operation on every element of a collection (list/array).
That's actually what you want here: you want to check every height in the array/list, and you want to know if any of these heights pass a specific check (i.e. >= 1.7
).
I highlighted any because the appropriate LINQ method here is called Any()
. It takes in a lambda method which describes the evaluation that needs to be performed, and it returns a boolean that is true if at least one element in the collection passes this evaluation.
Your entire code block can be condensed into:
float[] heights = new float[] { 0.1, 1.8 }; // you could use the list as well here
var result = heights.Any(height => height >= 1.7);
Now I want to refer back to the optimized logic that we established in the previous section. What LINQ's Any
method does is essentially the same, except that it repeats the evaluations for each element in the collection.
If your collection contains two elements, Any
basically does the same as:
var result = (heights[0] >= 1.7) || (heights[1] >= 1.7);
If your collection contains 5 elements, it does the same as:
var result = (heights[0] >= 1.7) || (heights[1] >= 1.7) || (heights[2] >= 1.7) || (heights[3] >= 1.7) || (heights[4] >= 1.7);
This is where the power of LINQ (and loops in general) shows itself: your code will work regardless of how many elements there are in the collection, so you don't need to manually handle every individual element yourself.
This will save you a boatload of effort.
I won't delve into the specifics of LINQ and lambda methods, but I highly advise you look this up in a knowledge repository of your choice as it is a powerful tool that will dramatically simplify any collection juggling you otherwise would have to write out manually.
I would add your properties in a list or array and use Linq
Something like:
List<double> allDoubles = new List<double>() { Hight1, Hight2, ...};
return allDoubles.Any(number => number + 0.3 >= 2);
The Any()
method will return true if any of the items in the list meets the condition.