Reputation: 13309
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
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
Reputation: 409166
No there is no other way than the second version you have.
Upvotes: 1