IF Statement Doesn't Work Properly - Arduino Forum

IF statement doesn’t work properly Projects Programming May 29, 2021, 9:35pm 1

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

3c06b91b8b5432e4e2c76ef6fe6e1303 3c06b91b8b5432e4e2c76ef6fe6e1303491×679 9.9 KB

May 29, 2021, 9:36pm 2

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

How to get the best out of this forum IDE 1.x
See also FAQ - Arduino Forum for general rules on forum behaviour and etiquette. Hello, Welcome to the Arduino Forum. This guide explains how to get the best out of this forum. Please read and follow the instructions below. Being new here you might think this is having rules for the sake of rules, but that is not the case. If you don’t follow the guidelines all that happens is there is a long exchange of posts while we try to get you to tell us what we need in order to help you, which is fru…

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

May 29, 2021, 9:39pm 3

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 } May 29, 2021, 9:41pm 4

"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.

May 29, 2021, 9:48pm 5

Thank you so much

I just dont get why it didnt count as error

May 29, 2021, 9:49pm 6 afek:

I just dont get why it didnt count as error

Because it is perfectly syntactically correct.

"My hovercraft is full of eels"

May 29, 2021, 10:09pm 7 afek:

I just dont get why it didnt count as error

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

May 29, 2021, 10:21pm 8 anon73444976:
afek:

I just dont get why it didnt count as error

Because it is perfectly syntactically correct.

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.

May 29, 2021, 11:46pm 9 anon73444976:

"IF statement doesn’t work properly"

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

: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".

May 30, 2021, 12:06am 10 gfvalvo:

newbies say the darnedest things.

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.

September 27, 2021, 12:07am 11

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

Topic Replies Views Activity
If else statement General Guidance 4 2569 May 5, 2021
IF statements LEDs and Multiplexing 15 5021 May 6, 2021
If and Else if problem Bugs & Suggestions 3 99298 May 6, 2021
If statements... Programming 16 1455 May 5, 2021
if else if bug? Programming 5 736 May 5, 2021
Unfortunately, your browser is unsupported. Please switch to a supported browser to view rich content, log in and reply.

Tag » Arduino If Else Statement Not Working