Sunday, June 20, 2010

Arduino learning-3

Arithmetic operators include addition, subtraction, multiplication, and division. They return the sum, difference, product, or quotient (respectively) of two operands.
y = y + 3;
x = x - 7;
i = j * 6;
r = r / 5;
The operation is conducted using the data type of the operands, so, for example, 9 / 4 results in 2 instead of 2.25 since 9 and 4 are ints and are incapable of using decimal points. This also means that the operation can overflow if the result is larger than what can be stored in the data type.
If the operands are of different types, the larger type is used for the calculation. For example, if one of the numbers (operands) are of the type float and the other of type integer, floating point math will be used for the calculation.
Choose variable sizes that are large enough to hold the largest results from your calculations. Know at what point your variable will rollover 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 and slow computation speeds.
Note: Use the cast operator e.g. (int)myFloat to convert one variable type to another on the fly. For example, i = (int)3.6 will set i equal to 3.
compound assignments
Compound assignments combine an arithmetic operation with a variable assignment. These are commonly found in for loops as described later. The most common compound assignments include:
x ++ // same as x = x + 1, or increments x by +1
x -- // same as x = x - 1, or decrements x by -1
x += y // same as x = x + y, or increments x by +y
x -= y // same as x = x - y, or decrements x by -y
x *= y // same as x = x * y, or multiplies x by y
x /= y // same as x = x / y, or divides x by y
Note: For example, x *= 3 would triple the old value of x and re-assign the resulting value to x.
comparison operators
Comparisons of one variable or constant against another are often used in if statements to test if a specified condition is true. In the examples found on the following pages, ?? is used to indicate any of the following conditions:
x == y // x is equal to y
x != y // x is not equal to y
x < y // x is less than y
x > y // x is greater than y
x <= y // x is less than or equal to y
x >= y // x is greater than or equal to y
logical operators
Logical operators are usually a way to compare two expressions and return a TRUE or FALSE depending on the operator. There are three logical operators, AND, OR, and NOT, that are often used in if statements:
Logical AND:
if (x > 0 && x < 5) // true only if both
// expressions are true
Logical OR:
if (x > 0 || y > 0) // true if either
// expression is true
Logical NOT:
if (!x > 0) // true only if
// expression is false
Related Posts Plugin for WordPress, Blogger...
Disclaimer: All the information in this blog is just gathered from different sites in the web and placed here and I am not the owner for these content

Popular Projects


My Blog List

Give Support

Give Support
Encourage me Through Comments & by Following