Hardware Reference
In-Depth Information
The if statement
As you have seen, your programs normally start with the first line of code and then contin-
ue executing the next line until your program runs out of code. This is fine, but what if you
want to decide between two different courses of action? We can do this in C using an if
statement. The following screenshot shows some example code:
You'll need to make several changes this time. The first is to add another global variable,
int whichLED = 1; at the top of your program. Then, you'll need to add several state-
ments to your loop() function. The line-by-line details are as follows:
if (whichLED == 1) : This is the if statement. The if statement evaluates
the expression inside the parentheses. This is the check statement. If it is true, it ex-
ecutes the next statement or a set of statements enclosed by {} . Note that we use
the == operator instead of a single = operator. A single = operator in C is the as-
signment operator, which means the storage location on the right is assigned the
value on the left. The == operator is a comparison operator, and returns true if the
two values are equal and false if they are not.
Search WWH ::




Custom Search