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.

Expressions<br />

297<br />

Table B.3<br />

Relational operators available in C<br />

Operator<br />

Description<br />

== Equal to<br />

!= Not equal to<br />

> Greater than<br />

< Less than<br />

>= Greater than or equal to<br />

25 &&<br />

weight < 180. The ! is a special logical operator in that it requires only one Boolean<br />

value and it reverses it (true becomes false, false becomes true).<br />

Although it makes little difference to the expression just shown, it’s important to<br />

note that C implements short-circuit Boolean evaluation: an expression will stop<br />

being evaluated as soon as the end result can be determined without any level of<br />

doubt. The following logical expression involving the && operator demonstrates how<br />

this feature can make the difference between your application running correctly or<br />

randomly crashing:<br />

struct box *p = get_some_pointer_value();<br />

BOOL result = (p != NULL) && (p->width == 20);<br />

The expression on the second line evaluates to true if p is a non-NULL pointer and the<br />

width of the box it points to has a width of 20.<br />

Without short-circuit Boolean evaluation, the expression would crash whenever p<br />

was NULL. This would occur because after evaluating the left side of the && operator (p<br />

!= NULL), execution would then evaluate the right side (p->width == 20) and crash<br />

while attempting to dereference the NULL pointer. The right side is evaluated even<br />

though the final result of the statement is known to be false because false && x for<br />

any value of x will always be false.

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

Saved successfully!

Ooh no, something went wrong!