01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

300 APPENDIX B The basics of C<br />

x but each clearly had a different impact on the larger expression they’re contained in<br />

because both y and z evaluate to the value 16 even though the value of x changes<br />

value after each statement is executed.<br />

The expression y = ++x + 10 can be rewritten as y = (x = x + 1) + 10, which is similar<br />

in nature to the expressions discussed in the previous section. Variable x (which starts<br />

off as the value 5) is assigned a new value of x + 1, resulting in x equaling 6. This<br />

updated value is then added to 10 to produce the result of 16, which is then stored in<br />

variable y.<br />

The next expression, y = x++ + 10, performs the same set of operations but in a different<br />

order. In this case, the current value of x (which at the start of executing this<br />

line is 6) has 10 added to it. This results in variable z being assigned the value 16, and<br />

it’s only after this assignment that variable x is increased by 1.<br />

The pre-increment operator (++x) increases the value of the variable and then<br />

uses the result in the larger expression, while the post-increment operator (x++)<br />

uses the current value of the variable in the larger expression and then increments<br />

the variable after the fact. Similar --x and x-- operators perform pre- and postdecrement<br />

operations.<br />

B.2.5<br />

Operator precedence<br />

Some expressions are ambiguous and can be interpreted in multiple ways. For example,<br />

should the following statement result in variable x having the value 20 or 14?<br />

int x = 2 + 3 * 4;<br />

int y = (2 + 3) * 4;<br />

int z = 2 + (3 * 4);<br />

C implements a set of precedence rules that specify the order in which expressions<br />

should be calculated (multiplication and division before addition and subtraction, for<br />

example). These rules mean that variable x will have the value 14.<br />

If you need to override the precedence rules, you can use parentheses to explicitly<br />

control the order in which operations are calculated, as demonstrated by variables<br />

y and z.<br />

This concludes our look at expressions and how to specify them in a C-based<br />

application. One type of expression that deserves greater attention is the conditional<br />

expression. Expressions of this form enable you to check the state of the data<br />

stored in an application and come up with a single Boolean (true or false) value<br />

that represents the truth of a potentially complex situation. What we haven’t covered<br />

is how to use such a value to change the behavior and flow of execution in an<br />

application. C provides the answer in a set of statements collectively called the conditional<br />

statements.<br />

B.3 Conditional statements<br />

Applications that execute a fixed sequence of commands are not the most exciting or<br />

practical. Invariably, an application of any complexity must make conditional decisions

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!