In this section, we will explore practical examples of control flow in Objective-C. Control flow statements allow you to dictate the order in which statements are executed in your program. We will cover examples using conditional statements, loops, and switch statements.

  1. Conditional Statements

Example 1: Simple if Statement

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        int number = 10;
        
        if (number > 5) {
            NSLog(@"The number is greater than 5.");
        }
    }
    return 0;
}

Explanation:

  • We declare an integer variable number and assign it a value of 10.
  • The if statement checks if number is greater than 5.
  • If the condition is true, the message "The number is greater than 5." is printed to the console.

Example 2: if-else Statement

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        int number = 3;
        
        if (number > 5) {
            NSLog(@"The number is greater than 5.");
        } else {
            NSLog(@"The number is 5 or less.");
        }
    }
    return 0;
}

Explanation:

  • The if statement checks if number is greater than 5.
  • If the condition is false, the else block is executed, printing "The number is 5 or less."

Example 3: if-else if-else Statement

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        int number = 5;
        
        if (number > 5) {
            NSLog(@"The number is greater than 5.");
        } else if (number == 5) {
            NSLog(@"The number is exactly 5.");
        } else {
            NSLog(@"The number is less than 5.");
        }
    }
    return 0;
}

Explanation:

  • The if statement checks if number is greater than 5.
  • If the first condition is false, the else if statement checks if number is exactly 5.
  • If both conditions are false, the else block is executed.

  1. Loops

Example 1: for Loop

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        for (int i = 0; i < 5; i++) {
            NSLog(@"Iteration %d", i);
        }
    }
    return 0;
}

Explanation:

  • The for loop initializes i to 0, checks if i is less than 5, and increments i by 1 after each iteration.
  • The loop prints "Iteration" followed by the current value of i for each iteration.

Example 2: while Loop

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        int i = 0;
        
        while (i < 5) {
            NSLog(@"Iteration %d", i);
            i++;
        }
    }
    return 0;
}

Explanation:

  • The while loop continues to execute as long as the condition i < 5 is true.
  • The loop prints "Iteration" followed by the current value of i and increments i by 1 in each iteration.

Example 3: do-while Loop

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        int i = 0;
        
        do {
            NSLog(@"Iteration %d", i);
            i++;
        } while (i < 5);
    }
    return 0;
}

Explanation:

  • The do-while loop executes the block of code at least once before checking the condition.
  • The loop prints "Iteration" followed by the current value of i and increments i by 1 in each iteration.

  1. Switch Statements

Example: Using switch Statement

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        int number = 2;
        
        switch (number) {
            case 1:
                NSLog(@"The number is 1.");
                break;
            case 2:
                NSLog(@"The number is 2.");
                break;
            case 3:
                NSLog(@"The number is 3.");
                break;
            default:
                NSLog(@"The number is not 1, 2, or 3.");
                break;
        }
    }
    return 0;
}

Explanation:

  • The switch statement evaluates the value of number.
  • Depending on the value, it executes the corresponding case block.
  • The break statement prevents fall-through to the next case.
  • The default case is executed if none of the specified cases match.

Practical Exercises

Exercise 1: FizzBuzz

Write a program that prints the numbers from 1 to 20. For multiples of 3, print "Fizz" instead of the number, and for multiples of 5, print "Buzz". For numbers that are multiples of both 3 and 5, print "FizzBuzz".

Solution:

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        for (int i = 1; i <= 20; i++) {
            if (i % 3 == 0 && i % 5 == 0) {
                NSLog(@"FizzBuzz");
            } else if (i % 3 == 0) {
                NSLog(@"Fizz");
            } else if (i % 5 == 0) {
                NSLog(@"Buzz");
            } else {
                NSLog(@"%d", i);
            }
        }
    }
    return 0;
}

Exercise 2: Factorial Calculation

Write a program that calculates the factorial of a given number using a while loop.

Solution:

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        int number = 5;
        int factorial = 1;
        int i = number;
        
        while (i > 0) {
            factorial *= i;
            i--;
        }
        
        NSLog(@"Factorial of %d is %d", number, factorial);
    }
    return 0;
}

Conclusion

In this section, we covered practical examples of control flow in Objective-C, including conditional statements, loops, and switch statements. These examples should help you understand how to control the flow of your programs effectively. Practice these examples and exercises to reinforce your understanding before moving on to the next module.

© Copyright 2024. All rights reserved