And (&&) not caring about both operators

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi.

I have a weird....bug I think.

while ((off1 != 1) && (off2 != 1) && (off3 != 1))

The code above doesn't run until all "off"s are 1. As soon as one of them becomes 1, the loop breaks.

while (/*(off1 != 1) && (off2 != 1) &&*/ (off3 != 1))

If I do this (in the current itteration, I know that off3 is the slowest, but that is not always the case), then the loop goes to it's end.

Any combination of 2 or all 3 exits as soon as any one off gets the value 1.

Something similar happens with the following code

if (inter == 3 && dec1 == 0 && pas == 100)

If any 2 of the 3 conditions are met, it enters the statement. I know this because the if toggles an led and "pas == 7" and "pass == 100" both toggle at the same intervals.

Is this a bug? Or am I doing something wrong?

Thanks in advance.

Last Edited: Sat. May 26, 2018 - 02:21 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Shirley it is doing exactly what you have asked it to do.

 

The trick is to write logical expressions that are intuitive for humans.

The compiler does not care how obtruse your expressions look.

 

Most people would write expressions with positive logic e.g.

while (on1 && on2 && on3) ;

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Uhmm......Actually it's not.

Unless conditionals / operators have changed and I didn't the memo.

&& is true only when both operators are true, otherwise it's false.

So "while ((off1 != 1) && (off3 != 1))" should iterate through the while until both off1 AND off2 have the values one.

Now, it exits when either off1 OR off3.

In case you missed that: && means AND, || means OR. I am using && (AND).

The same for "if ((inter == 3 && dec1 == 0) && pas == 100)": it should enter the if statement only if all 3 operators are true (meaning inter equals 3 AND dec1 equals 0 AND pas equals 100). Right now it enters if any one of the operators is true.

 

Note: I have tried it your way, but there's no difference: it exits the while loop if one variable get written to 1.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Where do you  mention what do you WANT it to do???!

 

You don't mention what values can "off"  take...  only 0 or 1??....247 or 39928 or 1 or 1596? 

 

Remember && is a logical operator (only true /false considered)...so all three portions must be true for the while to go.

Therefore each portion must not be one

 

The code above doesn't run until all "off"s are 1. As soon as one of them becomes 1, the loop breaks.

THE CODE RUNS WHEN THEY ARE ALL 1, BUT THE CODE STOPS WHEN ANY BECOME ONE???  HUH?

 

Unless conditionals / operators have changed and I didn't the memo.

I  suspect you didn't read the memo/manual

When in the dark remember-the future looks brighter than ever.

Last Edited: Sat. May 26, 2018 - 03:45 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
while ((off1 != 1) && (off2 != 1) && (off3 != 1))

As David say it does what you ask it to do

it will only run if ALL are 0 as soon one ==1 it will break because now one is false, meaning the while is false. (with && it ask for all 3 to be true)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yeah. Sorry. you're right.

I actually forgot to increment a variable, which in turn would set all offs at the same time.

Thanks for the brain storm :) .