01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

296 APPENDIX B The basics of C<br />

You may like to get into the habit of prefixing your own publicly visible identifiers<br />

with a prefix of your own choosing, perhaps based on the initials of your name<br />

or company.<br />

With the proper naming of variables and other elements out of the way and no<br />

longer producing compile-time errors, let’s explore how to use one or more variables<br />

to calculate new values or to answer questions.<br />

B.2 Expressions<br />

It’s uncommon for an application to be developed without requiring at least one mathematical<br />

calculation, so C provides a rich set of operators for mathematical expressions.<br />

B.2.1<br />

Arithmetic operators<br />

The most obvious operators are the arithmetic ones that perform the basic mathematical<br />

operations we’re all familiar with. These operators are listed in table B.2.<br />

Table B.2<br />

Arithmetic operators available in C<br />

Operator<br />

Description<br />

+ Addition<br />

- Subtraction<br />

* Multiplication<br />

/ Division<br />

% Modulus (remainder)<br />

These arithmetic operators all have their expected behavior. For example, the expression<br />

4 + 3 results in the value 7 being calculated. The modulus operator returns the<br />

remainder of an integer division operation, so 9 % 2 equals 1 because 2 goes into 9<br />

four times with 1 left over.<br />

B.2.2<br />

Comparison operators<br />

Once you’ve calculated one value, you may want to compare it against another. Relational<br />

operators allow this comparison, which results in a Boolean value being calculated<br />

that indicates the truth of the statement. Table B.3 outlines the available<br />

relational operators.<br />

As an example, the expression x >= 5 * 10 determines if the value of variable x is<br />

greater than or equal to 50 (the value of the expression on the right side of the >=<br />

operator).<br />

Sometimes comparing a single value isn’t enough. For example, you may want to<br />

check if a person’s age is greater than 25 years and weight is less than 180 pounds. In<br />

such scenarios, you must use compound conditional statements.

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

Saved successfully!

Ooh no, something went wrong!