IF statement doesn’t work properly

For some reason I always get that the first condition is true and it prints
waterB = 389
when I want
waterA = 89

Your post was MOVED to it's current location as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

Posting pictures of code is a waste of time

The easier you make it to read and copy your code the more likely it is that you will get help

Please follow the advice given in the link below when posting code , use code tags and post the code here

If you get errors when compiling please copy them from the IDE using the "Copy error messages" button and paste the clipboard here in code tags

You have the syntax of you range based if statements wrong. It should be of the form

if (variable >= 123 && variable <= 345)
{
  //conditional code here
}

"IF statement doesn’t work properly"

Try to imagine how many posts there would be here if that statement were really true.

Occam's Razor suggests that how you wrote the "if" is at fault.

Bob beat me to it.

Thank you so much

I just dont get why it didnt count as error

Because it is perfectly syntactically correct.

"My hovercraft is full of eels"

You get a warning if you have them turned on in the IDE preferences, which you probably should have, but as AWOL says it is perfectly good syntax

This is one of the disadvantages of C/C++, as a language. Lots of statements that are "obviously wrong" are in fact perfectly legal statements.
In this case, you have if (0<=var<100), and the C language says that logical expressions evaluate to 0 (for false) and 1 (for true). And complex logical expressions obey a defined order of operations.
So... 0<=var evaluates to either 0 or 1, and then both 0<100 and 1<100 are clearly true...

If you had turned on "More" or "All" "compiler Warnings" in the preferences dialog, you would have gotten a warning message:

 warning: comparisons like 'X<=Y<=Z' do not have their mathematical meaning [-Wparentheses]
   73 |     if (0<=PORTB<=100) {

But it would have scrolled by quickly, not been very readable, and would probably not been noticed anyway.

:laughing: :sweat_smile: :rofl: :joy: Those newbies say the darnedest things. Moreover, imagine how useful the BILLIONS of lines of C / C++ code that have been written over the past 50 years would be if the 'if' statement didn't "work properly".

Be nice. THEIR if statement was not doing what THEY wanted it to - it was indeed not working properly.
They never really implied that they thought the compiler was broken.
There was a simple explanation, and it's all fine now.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.