if (x <= 22.5f)
            {            

            }
            if (x >= 22.5f && x <= 67.5f)
            {

            }
            if (x >= 67.5f && x <= 112.5f)
            {

            }
            if (x >= 112.5f && x <= 157.5f)
            {

            }
            if (x >= 157.5f && x <= 180.0f)
            {

            }

My code seems too complex in its current form. I reckon there must be a more efficient way of doing the above. Can someone lend me their ideas?

Dani AI

Generated

Nice catch by — switching to an else if chain fixes the overlap and avoids checking every condition once one matches. A few additional, practical alternatives and cautions that are useful when those ranges grow or need to be cleaner:

Use a small lookup by mapping the input to a bucket index. This is O(1) and keeps the logic compact and data-driven; set min and binWidth for your partitioning and clamp the index to the handler array bounds.

var handlers = new Action[] { /* one handler per bucket */ };

float min = minValue;       // lowest value covered
float binWidth = bucketWidth; // width of each bucket
int idx = (int)Math.Floor((x - min) / binWidth + 0.5f);

if (idx < 0) idx = 0;
if (idx >= handlers.Length) idx = handlers.Length - 1;

handlers[idx]();

If the cut points are irregular, keep them in a sorted array and find the insertion point with Array.BinarySearch; that gives O(log n) lookup and is simple to update at runtime.

float[] cutoffs = new float[] { /* sorted cut points */ };
int pos = Array.BinarySearch(cutoffs, x);
if (pos < 0) pos = ~pos; // insertion index: first cutoff > x
// pos is now the bucket index you can use to dispatch

Keep these final notes in mind: pick a consistent interval rule (left-inclusive/right-exclusive avoids double hits), be aware of float rounding (use an epsilon if you need tolerance), and prefer the simple else if for just a handful of static ranges. For modern C#, switch expressions with relational patterns are also very readable — see the docs for switch expressions and Array.BinarySearch for details: switch expressions and Array.BinarySearch.

Recommended Answers

All 2 Replies

Two things

if (x <= 22.5f)
{
}
if (x >= 22.5f && x <= 67.5f)
{
}

In statements like these, if the value is exactly 22.5f, then you're allowing control to enter each of these if statement. One way of stating intent is if one statement is <=, then the next one should simply be >. We could fix that up and call it a day. But we won't.

The second thing is that you are using seperate if statements for each check. Even if the value fits the first if statement, the way your program is constructed means that every following statement is still going to be evaluated. Rewrite the remaining statements as "else if". That way, when control finds a valid condition, none of the others are checked. Also, doing so means you can drop the first part of the conditional. if x <= 22.5f, you know control will enter the first block. So the second check does not need to worry about x being > 22.5, only if it is less than 67.5f.

if (x <= 22.5f)
{
}
else if (x <= 67.5f)
{
}
else if (x <= 112.5f)
{
}
else if (x <= 157.5f)
{
}
else if (x <= 180.0f)
{
}
commented: I gues my age must make me slower, I was on the point of posting exactly the same! +6

Thank you apegram.

Time for me to look at "else if" in detail!

Happy Easter. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.