-
Notifications
You must be signed in to change notification settings - Fork 228
Solutions: If Else Statements and the Ternary Operator
Aaron Brager edited this page Jun 9, 2014
·
1 revision
If/else exercise:
- (NSString *) itemMaryCanPurchaseForDollars:(NSInteger)dollars {
NSString *itemToReturn;
if (dollars >= 100000000) {
itemToReturn = @"have The Big Apple";
} else if (dollars >= 1000) {
itemToReturn = @"have an Apple computer";
} else if (dollars >= 6) {
itemToReturn = @"have an apple";
} else if (dollars >= 5) {
itemToReturn = @"have some gum";
} else {
itemToReturn = @"get out of my store";
}
NSLog(@"For $%d, Mary can: %@", dollars, itemToReturn);
return itemToReturn;
}
Ternary exercise:
Two possible solutions:
return self.getsDiscount ? 18 : 24;
NSUInteger cost = 24;
return cost * (self.getsDiscount ? .75 : 1);