Learn Objective-C | C | How-To Use the if...else Logical Expressions
By klanguedoc
Logical operations in Objective-C are performed using the logical operators from the C language. The most common and practical, the if logical operator, allows an app to make choices using two or more Boolean expressions. The syntax is as follows:
In the preceding example, if the condition is true then the code immediately following the Boolean statement will be executed. Another version of the above would check to see if the two values to not match by including the exclamation mark, which denotes a negative like this:
The next form of the if logical operations would include the else condition which would be evaluated if the first if statement was either true or false depending on the intended use:
The above version evaluates the Boolean expression. If the evaluation is true, then execute a set of instructions, otherwise the alternate, or false instructions will be executed. The if statement includes curly braces {} to group the code to executed together. The next version will specify many more Boolean operations using the else if conditional argument:
This type of logical operation can have as many Boolean expression as required for the program to work effectively. As with the other logical operations, the program will transfer the execution of the flow based on the positive evaluation of an expression.
To further expand on the basic model, the Boolean expression can involved several Boolean expressions using the || “or” operator or the && “and” operator:
Using the || operator, the first Boolean expression is evaluated. If the expression is true, then the flow stops and the expression is executed. Otherwise the second Boolean expression is evaluated in turn. Alternatively the && “and” can be used:
Since the order of preference is always left to right, the order can be changed using parentheses:
In the above example, the Boolean expressions 2 and 3 will be evaluated first because they are surrounded by parentheses.
Xcode examples
To actually see how the if conditional operators work and behave, it is best to actually write some code and execute the code. To that end, here are some sample code in Xcode that can be reproduced to try out the if statements yourself.
Example - 1 : Simple if
Create an Xcode Single View project
Name it: Ifs or something else to your liking
Once created, add a new Objective-C Class
Name: Fruit
In the header file add two NSString instance variables
Name: fruitName
Name: fruitType
Also add the getter and setter properties in the header and implementation files
We will use this class to evaluate our if operations
To implement this Fruit class, we will need another class. So create another subclass of type NSObject
Name it: TheIfOperations
Declare the following instance variables and methods:
Name it: displaySomeIfs, return type is void or no return type
In the implementation file, add the following code to the displaySomeIfs.
In the implementation file, add the following code to the displaySomeIfs.
To make this method, also call this method in the kvlAppDelegate implementation file in the didFinishLaunchingWithOptions method:
The method creates two Fruit objects and set their fruitName properties. The if statement evaluates the Boolean expression, apple == orange and writes to the Console using the NSLog utility class from the cocoa framework:
Example 2 - Complex if
This example will demonstrate an if else if scenario. We will use the same Fruit class but will add a new method to the TheIfOperations class:
Name: ComplexIf
Return type: void
This method demonstrate how to test for multiple Boolean conditions. If none satisfy any the conditions, the else clause if executed. Finally, the method’s call must be added to the same method in thr kvlAppDelegate method above.
Example 3 - Multiple Boolean Conditions
Define yet another method to evaluate multiple conditions simultaneously. As describe above, this method will show the use of using || or and && logical operators.
Add another method to the TheIfOperations class and add the code as below
Name: multipleConditions
Return type: void
As the above example demonstrates, you can also use a method with a BOOL return type to evaluate the Boolean expression.
In Summary
The if logical expression can have many forms to suit many types of scenarios. It is a very versatile logical expression.
Fruit.h
//
// Fruit.h
// Ifs
//
// Created by Kevin Languedoc on 1/22/12.
// Copyright (c) 2012 kCodebook. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Fruit : NSObject{
NSString *fruitName;
NSString *fruitType;
}
@property(strong,nonatomic)NSString *fruitName;
@property(strong,nonatomic)NSString *fruitType;
-(BOOL)isFruit:(NSString *)fruitOne:(NSString *)fruitTwo;
@end
Fruit.m
//
// Fruit.m
// Ifs
//
// Created by Kevin Languedoc on 1/22/12.
// Copyright (c) 2012 kCodebook. All rights reserved.
//
#import "Fruit.h"
@implementation Fruit
@synthesize fruitName = _fruitName;
@synthesize fruitType = _fruitType;
-(BOOL)isFruit:(NSString *)fruitOne:(NSString *)fruitTwo{
if(fruitOne == fruitTwo){
return TRUE;
}
return FALSE;
}
@end
TheIfOperations.h
// // TheIfOperations.h // Ifs // // Created by Kevin Languedoc on 1/21/12. // Copyright (c) 2012 kCodebook. All rights reserved. // #import <Foundation/Foundation.h> @interface TheIfOperations : NSObject -(void)displaySomeIfs; -(void)ComplexIf; -(void)MultipleConditions; @end
TheIfOperations.m
//
// TheIfOperations.m
// Ifs
//
// Created by Kevin Languedoc on 1/21/12.
// Copyright (c) 2012 kCodebook. All rights reserved.
//
#import "TheIfOperations.h"
#import "Fruit.h"
@implementation TheIfOperations
-(void)displaySomeIfs{
Fruit *apple = [[Fruit alloc] init];
apple.fruitName=@"macintosh";
Fruit *orange = [[Fruit alloc] init];
orange.fruitName = @"navel";
if (apple == orange) {
NSLog(@"These fruits are the same");
}else{
NSLog(@"These fruits are not the same");
}
}
-(void)ComplexIf{
Fruit * grape = [[Fruit alloc] init];
Fruit * lemon = [[Fruit alloc] init];
Fruit * grapefruit = [[Fruit alloc] init];
grape.fruitType=@"grape";
lemon.fruitType = @"citrus";
grapefruit.fruitType = @"citrus";
if(grape.fruitType == lemon.fruitType)
{
NSLog(@"grapes and lemons are the same type of fruit.");
}else if(grape.fruitType == grapefruit.fruitType){
NSLog(@"Ok, grapes and grapefruits are the same kind of fruit.");
}else if(lemon.fruitType == grapefruit.fruitType){
NSLog(@"Ok, these two fruits are both citrus");
}else{
NSLog(@"none of these fruits are the same.");
}
}
-(void)MultipleConditions{
Fruit * fruits;
Fruit * tomatoes;
Fruit * apples;
fruits = [[Fruit alloc] init];
tomatoes = [[Fruit alloc] init];
apples = [[Fruit alloc] init];
fruits.fruitType = @"food";
tomatoes.fruitType = @"food";
apples.fruitType = @"apple";
if(apples.fruitType == tomatoes.fruitType && fruits.fruitType == apples.fruitType)
{
NSLog(@"These fruits are the same");
}else if([fruits isFruit:fruits.fruitType :tomatoes.fruitType]){
NSLog(@"These fruits are the same");
}else{
NSLog(@"These fruits are not the same");
}
}
@end
kvlAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
TheIfOperations *testOne = [[TheIfOperations alloc] init];
// [testOne displaySomeIfs];
[testOne ComplexIf];
return YES;
}|
|
5-in-1 Camera Connection Kit USB AV Video Cable Accessories For iPad 1 2 EA511
Current Bid: $19.89
|
|
|
6 ACCESSORY BUNDLE FOR IPAD 2 3G LEATHER CASE+STYLUS+HEADSET+PROTECTOR+Plug Cap
Current Bid: $15.99
|
|
|
5-in-1 Camera Connection Kit USB AV Video Cable Accessories For iPad 1 2 EA511
Current Bid: $16.98
|
|
|
8 Accessory Bundle Leather Case Cover For Apple iPad 1
Current Bid: $12.59
|
|
|
USB Camera Connection Kit Accessories 5 in 1 for iPad 2 iPad 3 G sd CARD READER
Current Bid: $.99
|
Comments
Thanks. computer programming can be a lot of fun and can also let you be very creative, much like writing and drawing. I hope these tutorials help with you pursuit.
Reegan777 4 months ago
Very informative, I've been interested on computer programming for some time now and this is a good example of it.