Hardware Reference
In-Depth Information
You can use those math operations with an assignment operator to make a
math operation and update the value of a variable in one fell swoop using
compound operators. These two lines of code all do the exact same thing:
doubleThis = doubleThis * 2;
doubleThis *= 2;
There's also shorthand syntax to increment and decrement integers by using
++
or
--
. The following three lines of code all do the exact same thing:
countUp = countUp + 1;
countUp += 1;
countUp++;
And these lines of code all do the same thing as well:
countDown = countDown - 1;
countDown -= 1;
countDown--;
if Statements
Another new programming concept introduced in
Example 3-2
is the
if
statement, which you'll likely find yourself using a lot. The concept is fairly
simple: if something is true, then do something.
The syntax looks like this:
if (condition) {
execute this code if condition is true
}
There are a few different conditions you can test for, as laid out in
Table 3-1
.
Table 3-1.
Comparison operators
Operator
Evaluates to True if…
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
Let's take a look at a few simple examples of
if
statements:
int n = 10;
if (n > 10) {