Reputation: 493
I have this while loop:
while (abs(B-left)<INFSMALL | abs(B-right)<INFSMALL) {
...
}
I want it to run while the above condition is FALSE. Is there an easy way to do this?
Upvotes: 1
Views: 523
Reputation: 160437
Use one of:
Negate the entire conditional by wrapping it with !(.)
, as in
while (!(abs(B-left) < INFSMALL || abs(B-right) < INFSMALL)) {
...
}
Negate the components inside, and change from OR to AND:
while (abs(B-left) >= INFSMALL && abs(B-right) >= INFSMALL) {
...
}
You should be using ||
instead of |
, see Boolean operators && and ||. The flow-control functions if
and while
need the conditional to be length-1 exactly, not 0, no more than 1. While |
will produce a vector of length 1 if all of the arguments are length 1, there are two reasons this is still a bad idea:
Short-circuiting: in general, if the first (of multiple, chained) conditional suggests that the second (and beyond) conditional does not matter, than short-circuiting means it will not evaluate them. Without predefining aa
, for instance, compare
exists("aa") && aa > 0
# [1] FALSE
exists("aa") & aa > 0
# Error: object 'aa' not found
Declarative code: the control-flow while
must only take length-1, make sure you know this going in. It's perhaps a little bit of style, so more-so #1 above.
Upvotes: 1