Friday, July 12, 2013

Objective-C > Condition expression

  • if - else-if - else statement:
#import < Foundation/Foundation.h>

int main(int argc, const char * argv[]){
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    NSString *output = @"\n\n";     [output autorelease];     if ( 3 > 5) {         NSLog(@”3 is larger than 5”);     } else if (4 > 5) {         NSLog(@”4 is larger than 5”);     } else if (5 > 5) {         NSLog(@”5 is larger than 5”);     } else if (6 > 5) {         NSLog(@”6 is larger than 5”);     } else {         NSLog(@”None of the above can be accepted”);     }     output = [output stringByAppendingString: @"\n\n"];     NSLog(output);     [pool drain];     return 0;
}

  • switch ... case ... break ... default statement:
switch (6) {
    case 3:
        System.out.println("選擇是3...");         NSLog(@”cute3”);         break;     case 4:         System.out.println("選擇是4...");         NSLog(@”cute4”);         break;     case 5:         System.out.println("選擇是3...");         NSLog(@”cute5”);         break;     case 6:         System.out.println("選擇是6...");         NSLog(@”cute6”);         break;     default:        System.out.println("以上沒有符合的條件:");         NSLog(@”None cute”);         default;

  • If no break within default, the logic inside default will be executed everytime.
#import <Foundtation/Foundation.h>int main (int argc, const char * argv[]){
    NSAutoreleasePool *pool = [NSAutoreleasePool alloc] init];
    NSString *output = @”\n\n”;     [output autorelease];
   
switch (6) {
    case 3:         output = [output stringByAppendingString: @”Choice is 3...”];         break;     case 4:         output = [output stringByAppendingString: @”Choice is 4...”];         break;     case 5:         output = [output stringByAppendingString: @”Choice is 5...”];         break;     case 6:         output = [output stringByAppendingString: @”Choice is 6...”];         break;     default:         output = [output stringByAppendingString: @”Choice is default”];     }     output = [output stringByAppendingString: @”\n\n”];     NSLog(output);     [pool drain];     return 0;
}

  • while loop:
    • Control variable i has to be defined before while loop.
  int i = 10;
 
  while (i>0){
     System.out.println(i);
     NSLog(i);
     i--;
 }
  • for loop:
 for (int i = 0; i > 0 ; i --){
      output = [output stringByAppendingFormat: @"%i\n", i];
 }
 int i = i;

for (; i> 0){
    output = [output stringByAppendingFormat: @"%i\n", i];
}
  • do-while loop:

No comments:

Post a Comment