IF Statement Doesn't Work Properly - Arduino Forum
Maybe your like
For some reason I always get that the first condition is true and it prints waterB = 389 when I want waterA = 89
3c06b91b8b5432e4e2c76ef6fe6e1303491×679 9.9 KB
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
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
UKHeliBob May 29, 2021, 9:39pm 3You have the syntax of you range based if statements wrong. It should be of the form
if (variable >= 123 && variable <= 345) { //conditional code here } anon73444976 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.
afek May 29, 2021, 9:48pm 5Thank you so much
I just dont get why it didnt count as error
anon73444976 May 29, 2021, 9:49pm 6I just dont get why it didnt count as error
Because it is perfectly syntactically correct.
"My hovercraft is full of eels"
UKHeliBob May 29, 2021, 10:09pm 7I 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
westfw May 29, 2021, 10:21pm 8afek:
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.
gfvalvo May 29, 2021, 11:46pm 9"IF statement doesn’t work properly"
Try to imagine how many posts there would be here if that statement were really true.
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".
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.
system Closed September 27, 2021, 12:07am 11This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.
Related topics
| 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 |
Tag » Arduino If Else Statement Not Working
-
If And Else Statements, If Statement Not Working - Arduino Forum
-
[SOLVED] If Statement Not Working With String - Arduino Forum
-
If Statement Does Not Work! What Am I Doing Wrong? - Arduino Forum
-
If Else Statements Not Executing As Expected - Arduino Forum
-
Why Does "if...else If...else" Not Work, But "if...if...else" Does?
-
2nd If Statement Not Working - Arduino Forum
-
If Statement Not Working - Programming Questions - Arduino Forum
-
If Statement Not Working - Programming Questions - Arduino Forum
-
If-else Statement Not Working - Arduino Stack Exchange
-
Arduino IDE: Conditional(if-else-if) Statements - STEMpedia
-
Arduino Project Issues With IF Statements - Stack Overflow
-
Arduino If-else Statement - Linux Hint
-
2.7 Understanding If / Else And While Statement In Arduino
-
Tutorial 11: If Statement (and Else-if), Comparison Operators And ...