Stuck on simple stupid C/C++ logic statement -
i have 2 switches both need activated before move onto next part of program. normally, either switch can on or off. want run process until both switches engage. seem unable conceptualize logic need write statement.
i can use 2 statements want, such as:
start_process(); while(!sw1){ read_switches(); delay(); } while(!sw2){ read_switches(); delay(); } end_process();
but sure there better way it. best guess far has been this:
while(!(sw1 || sw2)){ read_switches(); delay(); }
but not confident correct.
and reason doesn't right either.
while(!sw1 && !sw2){ read_switches(); delay(); }
i've done far more complicated logic statements this, reason, not seeing way through simple statement. thank offered.
you need expression evalualtes true
till 1 or both of variables sw1
, sw2
false
. try this
while(!sw1 || !sw2) { read_switches(); delay(); }
or
while(!(sw1 && sw2)) { read_switches(); delay(); }
remember expressions !(sw1 && sw2)
, !sw1 || !sw2
equivalent according de-morgan's law.
Comments
Post a Comment