Objective-C Flow Control with if and else

From Techotopia
Jump to: navigation, search
PreviousTable of ContentsNext
Commenting Objective-C CodeThe Objective-C switch Statement

Purchase the full edition of this Objective-C book in Print ($14.99) or eBook ($12.99) format
Objective-C 2.0 Essentials Print and eBook (ePub/PDF/Kindle) editions contain 31 chapters.

Buy Print

In the chapter titled Objective-C Operators and Expressions we looked at how to use logical expressions in Objective-C to determine whether something is true or false. Since programming is largely an exercise in applying logic, much of the art of programming involves writing code that makes decisions based on one or more criteria. Such decisions define which code gets executed and, conversely, which code gets by-passed when the program is executing. This is often referred to as flow control since it controls the flow of program execution.

In previous chapters the if statement has been used in some examples. In this chapter of Objective-C 2.0 Essentials we are going to look at if statements in a little more detail.


Contents


Using the if Statement

The if statement is perhaps the most basic of flow control options available to the Objective-C programmer. Programmers who are familiar with C, C++ or Java will immediately be comfortable using Objective-C if statements.

The basic syntax of the Objective-C if statement is as follows:

if (boolean expression) {

// Objective-C code to be performed when expression evaluates to true

}

Note that the braces ({}) are only required if more than one line of code is executed after the if expression. If only one line of code is listed under the if the braces are optional. For example, the following valid code:

int x = 10;

if (x > 10)
       x = 10;

Essentially if the boolean expression evaluates to 1 (true) then the code in the body of the statement is executed (see Objective-C Operators and Expressions for more details of this type of logic). The body of the statement is enclosed in braces ({}). If, on the other hand, the expression evaluates to 0 (false) the code in the body of the statement is skipped.

For example, if a decision needs to be made depending on whether one value is greater than another, we would write code similar to the following:

int x = 10;

if ( x > 9 )
{
         NSLog (@"x is greater than 9!");
}

Clearly, x is indeed greater than 9 causing the message to appear in the console window.

Using if ... else .. Statements

The next variation of the if statement allows us to also specify some code to perform if the expression in the if statement evaluates to false. The syntax for this construct is as follows:

if (boolean expression) {

// Code to be executed if expression is true

} else {

// Code to be executed if expression is false

}

Using the above syntax, we can now extend our previous example to display a different message if the comparison expression evaluates to be false:


int x = 10;

if ( x > 9 )
{
         NSLog (@"x is greater than 9!");
}
else
{
         NSLog (@"x is less than 9!");
}

In this case, the second NSLog statement would execute if the value of x was less than 9.


Using if ... else if .. Statements

So far we have looked at if statements which make decisions based on the result of a single logical expression. Sometimes it becomes necessary to make decisions based on a number of different criteria. For this purpose we can use the if ... else if ... construct, the syntax for which is as follows:

int x = 9;

if (x == 10)
{
       NSLog (@"x is 10");
}
else if (x == 9)
{
NSLog (@"x is 9");
}
else if (x == 8)
{
         NSLog (@"x is 8");
}

This approach works well for a moderate number of comparisons, but can become cumbersome for a larger volume of expression evaluations. For such situations, the Objective-C switch statement provides a more flexible and efficient solution. For more details on using the switch statement read the chapter entitled The Objective-C switch Statement.

Summary

In this chapter we looked at the use of Objective-C if statements including if, if ... else and if ... else if ... constructs. In the next chapter of this book we will look at using the Objective-C switch statement as an alternative to more complex if statements.

Purchase the full edition of this Objective-C book in Print ($14.99) or eBook ($12.99) format
Objective-C 2.0 Essentials Print and eBook (ePub/PDF/Kindle) editions contain 31 chapters.

Buy Print



PreviousTable of ContentsNext
Commenting Objective-C CodeThe Objective-C switch Statement