Arithmetic inside an if statement

Maybe a silly question, but is arithmetic okay inside of an if statement?

ex:

if ((lastmillis - millis()) >= 1000)

ex:

if (vehiclevelocity <= (setspeed - 10))

Or would I have to perform the math outside of the statement and then assign it to a variable to place inside of the if statement?

Thanks!

The concept you need is "expression". A comparison is already an expression, so incorporating arithmetic into the expression is also allowed.

By the way, comparison operators have low precedence, so the subtractions don't need parentheses.

Thank you!

Would the parenthesis then be necessary if Boolean operands were included? Or are they in the same category as comparison operators?

ex:

if (vehiclevelocity <= (setspeed - 10) && enginestatus == 1)

doublec4:
Thank you!

Would the parenthesis then be necessary if Boolean operands were included? Or are they in the same category as comparison operators?

ex:

if (vehiclevelocity <= (setspeed - 10) && enginestatus == 1)

The entire story is here:
http://en.cppreference.com/w/cpp/language/operator_precedence

if (vehiclevelocity <= setspeed - 10 and enginestatus == 1)

The designers were smart and assigned the precedence in accordance with common mathematical language. C++ was updated to allow the use of "and" and "or" keywords in the place of "&&" and "||".

doublec4:
Would the parenthesis then be necessary if Boolean operands were included? Or are they in the same category as comparison operators?

Bear in mind you pay no price for using superfluous parenthesis. Judicious use of parenthesis allows you to forget / ignore mundane precedence rules which frees your brain for more important things.

With the emphasis on "judicious" - it can be overdone.

Great, thanks again! Helpful link :slight_smile: