Decision Making in C

The real power of a mc is its ability to read data and take action(s) based on that data. Stated differently, a mc has the ability to make decisions based on the information provided to it. In this chapter, you will learn
the various expressions that enable your program to make decisions based on the state of some set of data.
Relational Operators
As you might guess, a decision is often based on comparing the state of two or more pieces of data. You
make such decisions all the time, probably without thinking much about the process that is involved in making the decision. The phone rings and you get up to answer it. Implicitly, you make a decision whether to answer the call or not. Further, that decision involved comparing the expected benefits from answering
the call
(e.g., it might be someone you want to talk with) versus the expected costs of not answering the
call (i.e., I may miss out on talking to someone important).
Some decisions are better than others. Indeed,
the definition of a dilemma is when you have two or more choices and they are all bad.
Table 4-1 presents the relational operators available to you in Arduino C. All of the operators in the
table are binary operators and require two operands.
Table 4-1.
Relational Operators
Operator Interpretation
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
== Equal to
!= Not equal to

The result of all relational operations is either logic true (non-zero) or logic false (zero).
For example:
5 > 4 // Logic true
5 < 4 // logic false
5 == 4 // logic false
5 != 4 // logic true

Clearly, you can also use variables in the expressions.
If a = 5 and b = 4, then:

a > b // Logic true
a < b // logic false
a == b // logic false
a != b // logic true

These expressions are exactly the same as the previous set, only we substituted variables for the
numeric constants. Now let’s see how to use the relational operators with some C statements.
The if Statement
In a computer program, unless the central processing unit (CPU) is told to do otherwise, the CPU
processes the source code program instructions in a linear, top-to-bottom manner. That is, program
execution starts at whatever is designated as the starting point for the program and plows through the
source code from that point to the next statement until all of the statements have been processed.
In an Arduino C program, the starting point for the program is the function named setup(). The
program processes all of the statements in the setup() function block starting with the first statement and
marches through the statements from statement 1 to statement 2 to statement 3...until it reaches the
closing parentheses of the setup() function block. You can, however, alter this linear processing flow by
using an if statement.
The syntax for an if statement is:
if (expression1 is logic true) {
// execute this if statement block if true
}
// statements following the if statement block
An if statement consists of the if keyword followed by a set of opening and closing parentheses.
Within those parentheses is an expression that evaluates to either logic true or logic false. After the closing
parenthesis of the if test is an opening brace character ({). The opening brace is followed by one or more
program statements that are to be executed if the if test is logic true. Almost every programmer on the
planet indents these statements one tab stop. (I use the smallest number of spaces for the indent. You can
change the indent size by using the Edit menu and clicking the Increase Indent or Decrease Indent option.
A smaller indent decreases the need for horizontal scrolling.) The if block statements are then followed
by a closing brace (}), which marks the end of the if statement block.
Consider the following code fragment:
int b = 10;
// some more program statement...
if (b < 20) {
b = doSomethingNeat();
}
doSomethingElse(b);
The code fragment begins by defining b and initializing it to 10. Then some unspecified statements are
executed followed by an if test. If b has remained unchanged by the unknown statements, its value is still
10. Because b is less than 20, the expression is logic true, the if statement block is executed, and function
doSomethingNeat() is called and its return value is assigned into b. Then the statement following the if
statement block is executed, and doSomethingElse(b) is called.
the if test is false, then the if statement block is skipped and the call to doSomethingNeat()is not
made. Therefore, after the (false) test, the next statement to be executed is doSomethingElse(b).
You can
see the path of program execution more clearly in Figure 4-1. A logic true result of the relational test causes program flow to execute the statement(s) in the if statement block. If the relational test result is logic false, then the if statement block is skipped and the program resumes execution at the first statement following
the closing brace of the if statement block. As you can see, a decision has been made in the program
based on the program’s data.
false
. . . . . .
. . . . . .
}
{
true
if (relational test)
statements
statements
Figure 4-1. Execution Paths for if Test.
Note that the relational test (expression1) expects a true or false result. In this case, however, we used
a single equal sign for the relational expression rather than the proper “is equal to” operator (==). This
means the code performs an assignment statement, not a relational test. This is what I call a Flat Forehead
Mistake (FFM). You know, the kind of mistake where you slam the heel of your hand into your forehead
while muttering: “How could I make such a stupid mistake!” Relax. All good programmers have a slightly
flattened forehead and you should expect your fair share of such hammerings. The good news is that
although you might make a FFM mistake a second time, you will find the error more quickly the second
time. Anytime you end up in an if statement’s statement block when you know you shouldn’t be there,
check for this type of error. It is pretty easy to forget the second equal sign character.
If the if statement block consists of a single program statement, then the braces defining the
statement block may be omitted. For example:
if (b == 10)
b = doSomethingNeat();
doSomethingElse();
works exactly the same as it did before. If the two versions behave the same, then why the extra keystrokes?
There are several reasons why you should always use braces for if statement blocks. First, always using
braces adds consistency to your coding style, and that’s always a good thing. Second, adding the braces
delineates the if statement and makes it stand out more while you are reading the code. Finally, although
you may think only one statement is needed right now, subsequent testing and debugging may show you
need to add another statement to the if (b == 10)
b = doSomethingNeat();
doBackupNow();
doSomethingElse();
Although the programmer wanted to call both doSomethingNeat() and doBackupNow() only when b
equals 10, the way the code is written the call to doBackupNow() is always called, because what the
programmer actually has written is:
if (b == 10) {
b = doSomethingNeat();
}
doBackupNow();
doSomethingElse();
Always remember that, without braces, the if statement block default to a single statement being
controlled by the if test.
Arduino is a open source IDE it gives the best out of your mind and great tool in making your inovation
Decision Making in C Decision Making in C Reviewed by JAMCrux on August 26, 2013 Rating: 5
Accrux. Powered by Blogger.