Relational Operators in Arduino

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 1

Relational operators in Arduino

Equal to (==): CHecks if two values are equal and returns true if they are, false otherwise

Exp: if (a==b){…}
int a = 5;
int b = 5;

Not equal to (!=): Checks if two values are not equal and returns true if they are not, false otherwise

Exp : if (a!=b){…}
int a = 5;
int b = 10;

Greater than (>): Checks if the left operand is greater than the right operand and returns true if it is,
false otherwise.

Exp: if (a>b){…}
int a = 10;
int b = 5;

Less than or equal to (<=): Checks if the left operand is less than or equal to the right operand and
returns true if it is, false if otherwise.

Exp: if (a<=b){…}
int a = 5; or int a = 1;
int b = 10

///////////////////////////////////////////////////////////////////////////////////////////////////////////

The “%” operator in Arduino (and in many programming languages) is the modulus operator.

Int a = 17;
Int b = 5;
Int c = 2;
Int result1 = a % b; // result will be 2
Int result2 = a % c; // result will be 1

//You can also use it directly in a if-statement


If (a % b ==0) {
// Code to be executed if a is divisible by b
} else {
// Code to be executed if a is not divisible by b
}
In the if-statement, the condition a % b == 0 checks if a is divisible by b without any remainder. If the
condition is true (I.e. the remainder is 0), the code within the if block will be executed. Otherwise, the
code within the else block will be executed

You might also like