2

How to do if i want to get this code into one line?

if (count >= 0 && count <= 199) {
        return 1;
    } else if (count >= 200 && count <= 399) {
        return 2;
    } else if (count >= 400 && count <= 599) {
        return 3;
    } else if (count >= 600 && count <= 799) {
        return 4;
    } else {
        return 5;
    }

I'm just figuring is there any shortcut for this few line of codes.

fdermishin
  • 3,519
  • 3
  • 24
  • 45
rickysy
  • 119
  • 4
  • 10
  • I'd recommend a switch statement to eliminate the multiple if/else. – devlin carnate Dec 08 '15 at 16:49
  • literally `return 1` as in value 1?? – Naman Dec 08 '15 at 16:53
  • 1
    @devlincarnate: o'really? how?? – Karoly Horvath Dec 08 '15 at 18:01
  • @KarolyHorvath - Google switch statement? For example: http://www.tutorialspoint.com/cplusplus/cpp_switch_statement.htm – devlin carnate Dec 08 '15 at 18:23
  • @devlincarnate: If you didn't get the point of my comment, try to write this code with switches. I'm serious. – Karoly Horvath Dec 08 '15 at 18:31
  • @KarolyHorvath - two comments. first, when I made my original comment, this question didn't have a tag and no indication of language. So my comment was a general one. second, I fail to see what the trouble would be in using a switch statement with that code. switch(count) followed by the comparisons and returns. In the languages I'm familiar with, [it is preferable to avoid multiple if/else and use a switch statement instead](http://stackoverflow.com/questions/449273/why-the-switch-statement-and-not-if-else). Apparently you have a differing opinion, which is fine. – devlin carnate Dec 08 '15 at 18:40
  • @devlincarnate: You fail to see it because you didn't write code and test it. There's an opportunity to learn something here. – Karoly Horvath Dec 08 '15 at 18:43
  • @devlincarnate C++ switch statements don't work the way you seem to think. – Rob K Dec 08 '15 at 20:03
  • @devlincarnate "No indication of language"... Try reading the title sometime. – Ben Voigt Dec 08 '15 at 20:04
  • As a side note, the structure he's given could leave out all of the `else`s since each clause `return`s – Rob K Dec 08 '15 at 20:06
  • @BenVoigt - you're right. it was in the title and i missed it. oh well... with less than 4 hours of sleep last night, perhaps an oversight is warranted. – devlin carnate Dec 08 '15 at 20:55

3 Answers3

6

return ( count >= 0 && count <= 799 ) ? (1 + count / 200) : 5;

That is: if count is in range, you are returning successive values for each span of 200, and if it is out of range, you are returning 5.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
1

If you can't compute the range directly from the count as shown in Scott Hunter's answer (e.g., if the ranges aren't uniformly sized or the values they map to don't form a simple pattern), you can encapsulate a little table lookup like this:

#include <algorithm>
#include <utility>
#include <vector>

int FindRange(int count) {
  static const std::pair<int, int> ranges[] = {
    {   0, 5 },
    { 200, 1 },
    { 400, 2 },
    { 600, 3 },
    { 800, 4 }
  };
  const auto it = std::find_if(std::begin(ranges), std::end(ranges),
                               [=](const std::pair<const int, int> &range) {
                                 return count < range.first;
                               });
  return (it == std::end(ranges)) ? ranges[0].second : it->second;
}

You can then change the table values and, as long as you keep them sorted, this function will continue to work.

It's a linear search through the table, so it should be on-par with the performance of a cascaded if-else.

Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175
0
return 1 + std::min(count, 800) / 200;

should do. The if is hidden in std::min. If count is larger than 800, it is replaced by 800, and std::min(count, 800) / 200 is equal to 4.

Eric Duminil
  • 52,989
  • 9
  • 71
  • 124