Is there a short cut to this manner of if statement

Greetings.

I am wondering if this is correct and/or if there's a cleaner/shorter way to code this manner of if statement. This compiles so it should work although I haven't tested it yet.

  if (
        (timer1ValHrs<=97 && timer1ValMin<=99) || 
        (timer1ValHrs<=98 && timer1ValMin<=60) || 
        (timer1ValHrs<=99 && timer1ValMin<=60)
      )
  {
     //then do something here
  }

This is an example of me trying to test three different multi-argument situations all in the same line. The actual contents may vary but the idea is the same. Does the parenthesis operate like I think they do and isolate each section between the "or" statements? Again this does compile but is untested.
I haven't tested it yet due to it being part of a larger mass and if it doesn't operate like I think it should it will be very difficult to debug where the issue actually is.

Thank for your attention
amachinetech

  • Code that compiles, doesn’t mean it is correct.

“ I am wondering if this is correct and/or if there's a cleaner/shorter way to code this manner of if statement. This compiles so it should work although I haven't tested it yet.”

  • Try it to see if it works as you expect.

That way is fine if you understand it. You have to get your logic symbols correct, the && and the ||.

A lot of people struggle with that.

The second condition is already included in the third. So why test?

exactly my point. The IDE says yea I'll take that but will it work like I think it will.
Shy of writing an entire sketch specifically to test just this line, I was hoping for a "yes the parenthesis will isolate each argument from each other" If I have to brute force it and do the work then it is what it is.

amachinetech

That’s how we learn.

No, they don't. Ok, yes that is what they would normally do, but in this case they are unnecessary and don't actually do anything.

Operators like <= have a higher precedence than && which in turn have a higher precedence than ||.

So adding those brackets don't cause a problem, and may even help you understand how the expression will be evaluated without having to remember the operator precedence rules, but in this case you could leave them out.

1 Like

yes. I just threw somethings in to the parenthesis as an example. The thrust of the question is will this work like this:

if (
(one thing <= another thing && a third thing == something else) ||
(a different thing == some random thing && the color blue == green) ||
(the cookies == are chocolate chip && me == hungry)
)
{
//if any one of these things are true then do this
}

amachinetech

true but Im feeling lazy and am asking a question instead of doing a lot of work.
amachinetech

So you want volunteers to do your heavy lifting.

:thinking:

I do not see how this laziness will bring your answer with less of your effort than testing in wokwi.

Ok thats the first time I've heard of that. So let me restate to make sure I have it.
<,>== are looked at first
then &&
then || correct?
is there a list somewhere that shows in what order these things happen. I had it in my head to operate like algebra.
You know, PEMDAS
perenthesis
exponents
multiply
divide
add
subtract
apparently this is not the case?

amachinetech

part of it is that every time I ask a question that I think should be a simple yes or no I get to chat with you good people and stumble onto things I didn't know existed and learn something new. So the effort is never wasted
amachinetech

No, it is the case. But where do logical/Boolean operators like <=, &&, || come in that list? They are not there.

But there is a precedence for every operator defined by the C language. Just Google for "C operator precedence".

2 Likes

Well if OP’s expectations were ease of reading and not worrying about operator precedence then yes, tick in the box.

It’s actually a good practice to have them .

2 Likes

If asking a yes or no question is a heavy lift I think I may be asking the question to the wrong folks. The entire point of the forum is a place for people that are having questions about how to do something to reach out to others with more expertise for guidance and reassurance. I'm not asking you to do the project for me. I am asking if the way that I am doing the project will blow up in my face from people that already have scars from exploded projects.
Amachinetech

1 Like

In case of doubt: add some brackets...
It may even improve readability, as sometimes the presedence can be quite surprising (I think it is with pointers).

1 Like

So yes it will work as I expect although the actual inclusion of the parenthesis are not required but are a good idea for ease of readability. Thank you for your insight.
amachinetech

We may have to agree to disagree about that :wink:

@amachinetech take a look at this page:
https://en.cppreference.com/w/c/language/operator_precedence

You can see that <= has a precedence of 6. && has precedence of 11 and || has precedence of 12. All you other PEDMAS operators are there too, with the relative precedences you expected. (However, it can get confusing when you see that there are things like unary and binary + and - operators listed!)

1 Like

Look, if you would have tested things yourself rather than being lazy (your words) this whole thread could have been avoided.

We learn by doing not by being lazy.

Volunteers here spend a great amount of time and effort to help with valid newcomers problems.
I don't think there is a forum on the internet that is dedicated to people that are lazy or at least I have not seen one :thinking: