0
int max_range = 100;
// I do not want to add more else if.. Actually max range is still 100 with more else ifs.
// Range can take values from 0 to 100

if (range <= 10){
    a[0]= value;
}

else if (range > 10 && range <= 20){
    a[1]= value;
}

else if (range> 20 && range <= 30){
    a[2]= value;
}

else if (range > 30 && range <= 40){
    a[3]= value;
}

else if (range> 40 && <= max_range){
    a[4]= value;
}

It is simple code. I would like it to remove the nested else if's and use a for loop. How can I convert this into a for loop?

11
  • 1
    if-else is one operation - what is your for loop going to loop round? Commented Sep 26, 2017 at 11:42
  • I don't really see how you could replace it with a loop. You could loop over the index into a I guess, but then you would still need some kind of condition for the range anyway. Would it be more readable and easier to understand? Probably not. Commented Sep 26, 2017 at 11:43
  • Changing if to switch is something I've seen, but it looks like you can make a loop that goes from 0 to max_range / 10. The loop value (say i) could be used as an index, like if (range > i*10 && range <= i*10+10){ a[i] = value; }. Commented Sep 26, 2017 at 11:43
  • @GolezTrol that fails if max_range is 50 or more Commented Sep 26, 2017 at 11:45
  • @Caleth Depends how the array is defined. My guess is that this is actually dynamic and OP wants his code to be dynamic too.. Commented Sep 26, 2017 at 11:46

3 Answers 3

8

You don't need a loop, you are doing one action.

a[std::max(0, std::min(4, (range - 1) / 10))] = value;
Sign up to request clarification or add additional context in comments.

5 Comments

Yeah. It would be (range-1)/10. 30 would lead to 2, 20 would lead to 1. Only range=0 would go wrong, if that would be a possibility. Could optionally be coveredby adding a max as well.
@AndyG on balance, neither can I :p
Methinks you need a floor of 0 in there too.
And from C++17, that's a job for std::clamp :)
Good solution, but, in few weeks, even you will have difficulties understanding what this single line of code does.
0

My other answer is very particular to the boundaries being multiples of 10. A runtime modifiable version would be something like

std::set<int> boundaries = { 10, 20, 30, 40, max_range };

// ... potentially modify `a` and `boundaries`, keeping the number of elements equal

a[std::distance(boundaries.begin(), boundaries.lower_bound(range))] = value

Comments

0

I would like it to remove the nested else if's and use a for loop Probably incorrect expression: There's no nested if-else in your example.

  • Loops, if-else and switch work differently however they all handle conditions:

Loops do iteration; Checking for some condition n times whereas if, switch they check once. So you cannot convert a if-else into a loop.

  • It's really a good programming trying to make the code effective and as smaller as possible but it is not always the case. If so why such experts build a huge programs with maybe millions of code lines.

  • Your code works fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.