## Evaluate expressions until one returns true

I have a `savePotentiometerState(...)`

-function, which returns `true`

if the there were changes to save, and `false`

if nothing was done. Further, I know that on any single pass through my main loop, at most one of the potentiometers may have changed (due to the way they're read out).

This is on a very time-constrained embedded platform, so it's important (or at least matters) that I don't call `savePotentiometerState`

more often than I have to. However, the code I come up with naturally seems silly, something likely to end up at thedailywtf:

if (!savePotentiometerState(pot1)) if (!savePotentiometerState(pot2)) ... if (!savePotentiometerState(potn));

Another way to do this would be to use short-circuit evaluation:

const bool retval = savePotentiometerState(pot1) || savePotentiometerState(pot2) || ... || savePotentiometerState(potn);

I suppose I could even drop the assignment here. But this doesn't feel like good style either, since I'm abusing the short circuiting of the `||`

operator.

The various `potn`

objects are member variables of the containing class, so there's no obvious way to write this as a loop.

I feel like I'm missing something obvious here, so my question is: is there an idiomatic/easy to read way to do this, which doesn't sacrifice efficiency? If it matters, I'm using C++17.

Since you can use C++17 you can leverage fold expressions and write a helper function to do the evaluation for you.

template<typename... Args> bool returtnPotentiometerState(Args&&... args) { return (... || savePotentiometerState(args)); }

and then you would call it like

if (returtnPotentiometerState(pot1, pot2, ..., potn))

This means you don't have a loop, and you get short circuiting.

**Evaluation of Expressions—Wolfram Language Documentation,** If you had made the definition x=5, then the Wolfram Language would use an expression, the Wolfram Language evaluates the expression, then returns of evaluation involve using definitions such as f[x_]=x^2 which transform one This is true in particular if a complete symbolic form for f valid for any i cannot be found. Each expression is evaluated in turn until one results in TRUE; the corresponding set of statements is then executed. If none of the expressions returns TRUE, and the optional ELSE clause is present, the ELSE clause's statements are executed. UNKNOWN and FALSE are treated the same: the next condition expression is evaluated. ELSEIF is one word with no space between the ELSE and the IF.

Loop seems the way to go:

for (auto& pot : {std::ref(pot1), std::ref(pot2), /*..,*/ std::ref(potn)}) { if (savePotentiometerState(pot)) { break; } }

**Mathematica as a Tool: An introduction with practical examples,** evaluate the test, from the left to the right, until one gives True; return the to the right); return the value n corresponding to the first matching pattern Expression Each expression is evaluated in turn until one results in TRUE; the corresponding set of statements is then executed. If none of the expressions returns TRUE, and the optional ELSE clause is present, the ELSE clause's statements are executed. UNKNOWN and FALSE are treated the same: the next condition expression is evaluated.

Personally - I'd avoid the algorithm you're using.

I'd save the state for every pot all the time; and track the current and previous values; and then only call a given callback if if the value had changed.

This way, `savePotState`

is always as fast as it needs to be for a given pot; and you'll never get into the state where pot1 to pot(n-1) can block potn from being read.

**In JavaScript, does an if with multiple or's evaluate all statements ,** You're right. It's called short circuit evaluation, and it applies to both || and && . For || , evaluation of the right hand side takes place only if the left hand side Boolean Expressions Boolean expressions are Python expressions that return a truth value (True or False) when the interpreter evaluates them. They’re commonly used in if statements to check if some condition is true or false. Since Boolean expressions aren’t compound statements, you can use eval () to evaluate them:

**ANNA A Language for Annotating Ada Programs: Reference Manual,** 4.1 2 CONDITIONAL EXPRESSIONS A conditional expression seleGts one of a following an elsif are evaluated in sequence until one evaluates to TRUE or the defined as follows: function CHECK_P_EXIST return BOOLEAN is begin for X C++ will evaluate logical ors from left to right, but only until it finds a term that evaluates to true. Once it encounters that true, none of the remaining expressions in the or will get evaluated. This code leverages that behavior in a subtle and important way. The or expression in the if first calls power().

**Machine Conversations,** Figure 9 also illustrates the use of a probabilistic expression to generate ex:v, es:v3) where each expression en is evaluated in turn until one is found to be Joke:20m2) returns true when first evaluated, and then returns false anytime it is If the result is not empty, it becomes the return value for the function. If the result is empty, the second expression is evaluated and is the return value for the function. Both expressions must evaluate to the same type. The expression can be numeric, time, or Boolean.

**The MATHEMATICA ® Book, Version 4,** Unless expr is manifestly True, InstóJ:= Trueq[x == y] Trueq[exprl effectively assumes always returns True or False, whereas == can leave its input in symbolic form, until one of the expr is found to be True Evaluation of logical expressions. The Format function is used to apply a format to an identifier and the results of another function. Format ([Date],"ww")=Format (Now (),"ww")-1 The IIf function is used to evaluate an expression as true or false, and then return one value if the expression evaluates as true, and a different value if the expression evaluates as false.