2 pairs of 2 if conditions

Hi guys,
I just wondering it's possible to make it shorter and if can then how?

example:
Want to set range time range, to do something if time will be in the gap from x till y. Now now code look like this:

//range 15:50-16:10
if (hour == 15 && minute > 50) {...}
else if (hour ==16 && minute < 10) {...}

I wonder that can be something like this?

If ((hour == 15 && minute > 50) && (hour == 16 && minute < 10))

Please let me know that this way (2 pairs of 2 conditions) are possible, or have to use current (first) way. Or maybe you have any other faster/more clear way?

Welcome to the forum

if (hour = 15 && minute > 50)

That statement sets hour to 15, it does not compare it to 15

= for assignment
== for comparison

Fix that and 2 (or more) comparisons in an if are possible but take note the test will never return true if hour has to equal 15 in one part of it and 16 in another part of it

Thanks for checking. Of course I used "==" just at the forum I type "manually" instead ctrl+c and ctrl+v. Post edited

Keep in mind the ‘hour’ can’t be 15 -AND- 16…
Maybe OR

Another solution might be to express the time in minutes since midnight (msm).

uint32_t  msm = hour * 60 + minute;
if ((950 < msm) && (msm < 970)) ...

Right, that's the point, I confused myself. Now should work. Thanks :slight_smile:

Also good idea. Means my case is easy, just my brain don't want to cooperate today. Time for break :stuck_out_tongue:

Why all the unnecessary parentheses:

if ((950 < msm) && (msm < 970)) ...

can be

if (950 < msm && msm < 970) ...

The || and && operators are lower precedence precisely to help readability here.
And modern C++ allows 'or' and 'and':

if (950 < msm and msm < 970) ...

Why the unnecessary answer after 6 months? :slight_smile:

I always use the "unnecessary parentheses" in such cases, I believe it improve the readability of the code.

And I know it :nerd_face:

Ah, I got caught out by following a "related post" link, they are usually completely out of date. My (point about) readability (still) stands, reduce clutter...