Conditionals and Logic
If and If/else
https://www.arduino.cc/en/Tutorial/ifStatementConditional
If the condition, defined in parentheses, is true, the action, in brackets {} is done, otherwise it does what is in the second pair of brackets.
if (condition is true)
{ do this action;}
otherwise
{ do something else; }
https://www.arduino.cc/en/Tutorial/ifStatementConditional
If the condition, defined in parentheses, is true, the action, in brackets {} is done, otherwise it does what is in the second pair of brackets.
if (condition is true)
{ do this action;}
otherwise
{ do something else; }
While
https://www.arduino.cc/en/Tutorial/WhileStatementConditional
While a condition is true, does something until the condition is no longer true.
Can also be used to wait until a condition is no longer true, by doing nothing {...} until the the condition changes.
https://www.arduino.cc/en/Tutorial/WhileStatementConditional
While a condition is true, does something until the condition is no longer true.
Can also be used to wait until a condition is no longer true, by doing nothing {...} until the the condition changes.
For Loop
https://www.arduino.cc/en/Tutorial/ForLoopIteration
A for loop is used when you want something to repeat a set number of times.
In the example below,the local integer variable index is used as a loop counter. It is set to 0, and incremented by one( i++) every time the loop runs.
When index= 10, the loop terminates.
https://www.arduino.cc/en/Tutorial/ForLoopIteration
A for loop is used when you want something to repeat a set number of times.
In the example below,the local integer variable index is used as a loop counter. It is set to 0, and incremented by one( i++) every time the loop runs.
When index= 10, the loop terminates.
Boolean Expressions- AND, NOT, OR
https://www.arduino.cc/reference/en/language/structure/boolean-operators/logicaland/
https://www.arduino.cc/reference/en/language/structure/boolean-operators/logicaland/
&& (AND)
DescriptionLogical AND results in true only if both operands are true.
Example CodeThis operator can be used inside the condition of an if statement.
if (digitalRead(2) == HIGH && digitalRead(3) == HIGH) { // if BOTH the switches read HIGH // statements }
! ( NOT)
Description Logical NOT results in a true if the operand is false and vice versa.
Example Code: This operator can be used inside the condition of an if statement.
if (!x) { // if x is not true // statements }It can be used to invert the boolean value.
x = !y; // the inverted value of y is stored in x
|| ( OR) "pipe"
DescriptionLogical OR results in a true if either of the two operands is true.
Example CodeThis operator can be used inside the condition of an if statement.
if (x > 0 || y > 0) { // if either x or y is greater than zero // statements }
DescriptionLogical AND results in true only if both operands are true.
Example CodeThis operator can be used inside the condition of an if statement.
if (digitalRead(2) == HIGH && digitalRead(3) == HIGH) { // if BOTH the switches read HIGH // statements }
! ( NOT)
Description Logical NOT results in a true if the operand is false and vice versa.
Example Code: This operator can be used inside the condition of an if statement.
if (!x) { // if x is not true // statements }It can be used to invert the boolean value.
x = !y; // the inverted value of y is stored in x
|| ( OR) "pipe"
DescriptionLogical OR results in a true if either of the two operands is true.
Example CodeThis operator can be used inside the condition of an if statement.
if (x > 0 || y > 0) { // if either x or y is greater than zero // statements }