Objective-C loops

After finding another hour to read some more ‘Objective-C’ instruction, I’ve covered some looping techniques which are all again valid in Perl and ANSI C. My text covers the for, while and do loops, and heres the sample program…

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

    @autoreleasepool {
        
        int counter;
        
        // for loop
        for(counter=1;counter<=10;counter++){
            printf("[for loop] Counting up:%i\n", counter);
        }
        counter = 10;
        printf("\n");
        
        // while loop
        while(counter > 0){
            printf("[while loop] Counting down:%i\n",counter);
            counter--;
        }
        printf("\n");
        
        // do loop
        do {
            printf("[do loop] Counting up:%i\n",counter);
            counter ++;
        
        }
        while (counter <= 10);
    }
    return 0;
}

All nice and straightforward, the output is as follows:

[for loop] Counting up:1
[for loop] Counting up:2
[for loop] Counting up:3
[for loop] Counting up:4
[for loop] Counting up:5
[for loop] Counting up:6
[for loop] Counting up:7
[for loop] Counting up:8
[for loop] Counting up:9
[for loop] Counting up:10

[while loop] Counting down:10
[while loop] Counting down:9
[while loop] Counting down:8
[while loop] Counting down:7
[while loop] Counting down:6
[while loop] Counting down:5
[while loop] Counting down:4
[while loop] Counting down:3
[while loop] Counting down:2
[while loop] Counting down:1

[do loop] Counting up:0
[do loop] Counting up:1
[do loop] Counting up:2
[do loop] Counting up:3
[do loop] Counting up:4
[do loop] Counting up:5
[do loop] Counting up:6
[do loop] Counting up:7
[do loop] Counting up:8
[do loop] Counting up:9
[do loop] Counting up:10

Lets see what the next couple of chapters bring!

This entry was posted in C and tagged , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *