Arithmetic Operators Flashcards

1
Q

= (assignment operator)

A

= assignment operator (single equal sign)

Stores the value to the right of the equal sign in the variable to the left of the equal sign.
The single equal sign in the C programming language is called the assignment operator. It has a different meaning than in algebra class where it indicated an equation or equality. The assignment operator tells the microcontroller to evaluate whatever value or expression is on the right side of the equal sign, and store it in the variable to the left of the equal sign.
Example

int sensVal; // declare an integer variable named sensVal
sensVal = analogRead(0); // store the (digitized) input voltage at analog pin 0 in SensVal
Programming Tips

The variable on the left side of the assignment operator ( = sign ) needs to be able to hold the value stored in it. If it is not large enough to hold a value, the value stored in the variable will be incorrect.
Don’t confuse the assignment operator [ = ] (single equal sign) with the comparison operator [ == ] (double equal signs), which evaluates whether two expressions are equal.
See Also

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

+ (addition)

A

Addition, Subtraction, Multiplication, & Division

Description

These operators return the sum, difference, product, or quotient (respectively) of the two operands. The operation is conducted using the data type of the operands, so, for example, 9 / 4 gives 2 since 9 and 4 are ints. This also means that the operation can overflow if the result is larger than that which can be stored in the data type (e.g. adding 1 to an int with the value 32,767 gives -32,768). If the operands are of different types, the “larger” type is used for the calculation.
If one of the numbers (operands) are of the type float or of type double, floating point math will be used for the calculation.
Examples

y = y + 3;
x = x - 7;
i = j * 6;
r = r / 5;

Syntax

result = value1 + value2;
result = value1 - value2;
result = value1 * value2;
result = value1 / value2;

Parameters:

value1: any variable or constant
value2: any variable or constant
Programming Tips:

Know that integer constants default to int, so some constant calculations may overflow (e.g. 60 * 1000 will yield a negative result).
Choose variable sizes that are large enough to hold the largest results from your calculations
Know at what point your variable will “roll over” and also what happens in the other direction e.g. (0 - 1) OR (0 - - 32768)
For math that requires fractions, use float variables, but be aware of their drawbacks: large size, slow computation speeds
Use the cast operator e.g. (int)myFloat to convert one variable type to another on the fly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  • (subtraction)
A

Addition, Subtraction, Multiplication, & Division

Description

These operators return the sum, difference, product, or quotient (respectively) of the two operands. The operation is conducted using the data type of the operands, so, for example, 9 / 4 gives 2 since 9 and 4 are ints. This also means that the operation can overflow if the result is larger than that which can be stored in the data type (e.g. adding 1 to an int with the value 32,767 gives -32,768). If the operands are of different types, the “larger” type is used for the calculation.
If one of the numbers (operands) are of the type float or of type double, floating point math will be used for the calculation.
Examples

y = y + 3;
x = x - 7;
i = j * 6;
r = r / 5;

Syntax

result = value1 + value2;
result = value1 - value2;
result = value1 * value2;
result = value1 / value2;

Parameters:

value1: any variable or constant
value2: any variable or constant
Programming Tips:

Know that integer constants default to int, so some constant calculations may overflow (e.g. 60 * 1000 will yield a negative result).
Choose variable sizes that are large enough to hold the largest results from your calculations
Know at what point your variable will “roll over” and also what happens in the other direction e.g. (0 - 1) OR (0 - - 32768)
For math that requires fractions, use float variables, but be aware of their drawbacks: large size, slow computation speeds
Use the cast operator e.g. (int)myFloat to convert one variable type to another on the fly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  • (multiplication)
A

Addition, Subtraction, Multiplication, & Division

Description

These operators return the sum, difference, product, or quotient (respectively) of the two operands. The operation is conducted using the data type of the operands, so, for example, 9 / 4 gives 2 since 9 and 4 are ints. This also means that the operation can overflow if the result is larger than that which can be stored in the data type (e.g. adding 1 to an int with the value 32,767 gives -32,768). If the operands are of different types, the “larger” type is used for the calculation.
If one of the numbers (operands) are of the type float or of type double, floating point math will be used for the calculation.
Examples

y = y + 3;
x = x - 7;
i = j * 6;
r = r / 5;

Syntax

result = value1 + value2;
result = value1 - value2;
result = value1 * value2;
result = value1 / value2;

Parameters:

value1: any variable or constant
value2: any variable or constant
Programming Tips:

Know that integer constants default to int, so some constant calculations may overflow (e.g. 60 * 1000 will yield a negative result).
Choose variable sizes that are large enough to hold the largest results from your calculations
Know at what point your variable will “roll over” and also what happens in the other direction e.g. (0 - 1) OR (0 - - 32768)
For math that requires fractions, use float variables, but be aware of their drawbacks: large size, slow computation speeds
Use the cast operator e.g. (int)myFloat to convert one variable type to another on the fly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

/ (division)

A

Addition, Subtraction, Multiplication, & Division

Description

These operators return the sum, difference, product, or quotient (respectively) of the two operands. The operation is conducted using the data type of the operands, so, for example, 9 / 4 gives 2 since 9 and 4 are ints. This also means that the operation can overflow if the result is larger than that which can be stored in the data type (e.g. adding 1 to an int with the value 32,767 gives -32,768). If the operands are of different types, the “larger” type is used for the calculation.
If one of the numbers (operands) are of the type float or of type double, floating point math will be used for the calculation.
Examples

y = y + 3;
x = x - 7;
i = j * 6;
r = r / 5;

Syntax

result = value1 + value2;
result = value1 - value2;
result = value1 * value2;
result = value1 / value2;

Parameters:

value1: any variable or constant
value2: any variable or constant
Programming Tips:

Know that integer constants default to int, so some constant calculations may overflow (e.g. 60 * 1000 will yield a negative result).
Choose variable sizes that are large enough to hold the largest results from your calculations
Know at what point your variable will “roll over” and also what happens in the other direction e.g. (0 - 1) OR (0 - - 32768)
For math that requires fractions, use float variables, but be aware of their drawbacks: large size, slow computation speeds
Use the cast operator e.g. (int)myFloat to convert one variable type to another on the fly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

% (modulo)

A

% (modulo)

Description

Calculates the remainder when one integer is divided by another. It is useful for keeping a variable within a particular range (e.g. the size of an array).
Syntax

result = dividend % divisor
Parameters

dividend: the number to be divided
divisor: the number to divide by
Returns

the remainder
Examples

x = 7 % 5;   // x now contains 2
x = 9 % 5;   // x now contains 4
x = 5 % 5;   // x now contains 0
x = 4 % 5;   // x now contains 4

Example Code

/* update one value in an array each time through a loop */

int values[10];
int i = 0;

void setup() {}

void loop()
{
  values[i] = analogRead(0);
  i = (i + 1) % 10;   // modulo operator rolls over variable  
}

Tip

The modulo operator does not work on floats.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly