Prasanth Madhavan
Prasanth Madhavan

Reputation: 13309

Comparing integer to a range

Is there a simple way to see if an integer falls in a range?

like

int x = 15;
if(x==1x)
{
std::cout << "Yes it falls in the range 10-19" << std::endl;
}

As far as I understand, the closest thing to that is

((x>9) && (x<20))?(std::cout << "Yes" << std::endl):(std::cout << "No" << std::endl);

Or something like that.

Is there something like the first way?

Upvotes: 0

Views: 178

Answers (2)

Sean
Sean

Reputation: 62472

If you're going for an inclusive range the I'd use >= and <= as (in my opinion) it reads better for a range check. However, there's no clever way in C++ to check for a range other than a conditional using if or ?: and and && expression.

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409166

No there is no other way than the second version you have.

Upvotes: 1

Related Questions