Conditional statements are fundamental in programming as they allow you to execute different code blocks based on certain conditions. In Objective-C, the primary conditional statements are if, else if, else, and switch. This section will cover these statements in detail, providing examples and exercises to solidify your understanding.

  1. The if Statement

The if statement is used to execute a block of code if a specified condition is true.

Syntax

if (condition) {
    // Code to execute if condition is true
}

Example

int age = 20;

if (age >= 18) {
    NSLog(@"You are an adult.");
}

Explanation

  • age >= 18 is the condition.
  • If the condition is true, the message "You are an adult." will be printed.

  1. The else Statement

The else statement is used to execute a block of code if the condition in the if statement is false.

Syntax

if (condition) {
    // Code to execute if condition is true
} else {
    // Code to execute if condition is false
}

Example

int age = 16;

if (age >= 18) {
    NSLog(@"You are an adult.");
} else {
    NSLog(@"You are a minor.");
}

Explanation

  • If age >= 18 is false, the message "You are a minor." will be printed.

  1. The else if Statement

The else if statement allows you to check multiple conditions.

Syntax

if (condition1) {
    // Code to execute if condition1 is true
} else if (condition2) {
    // Code to execute if condition2 is true
} else {
    // Code to execute if none of the above conditions are true
}

Example

int score = 85;

if (score >= 90) {
    NSLog(@"Grade: A");
} else if (score >= 80) {
    NSLog(@"Grade: B");
} else if (score >= 70) {
    NSLog(@"Grade: C");
} else {
    NSLog(@"Grade: F");
}

Explanation

  • The code checks multiple conditions to determine the grade based on the score.

  1. The switch Statement

The switch statement is used to execute one block of code among many based on the value of a variable.

Syntax

switch (variable) {
    case value1:
        // Code to execute if variable == value1
        break;
    case value2:
        // Code to execute if variable == value2
        break;
    // More cases...
    default:
        // Code to execute if none of the above cases are true
}

Example

char grade = 'B';

switch (grade) {
    case 'A':
        NSLog(@"Excellent!");
        break;
    case 'B':
        NSLog(@"Good job!");
        break;
    case 'C':
        NSLog(@"Well done");
        break;
    case 'D':
        NSLog(@"You passed");
        break;
    case 'F':
        NSLog(@"Better try again");
        break;
    default:
        NSLog(@"Invalid grade");
}

Explanation

  • The switch statement checks the value of grade and executes the corresponding block of code.

Practical Exercises

Exercise 1: Age Category

Write a program that categorizes a person based on their age.

Task

  • If the age is less than 13, print "Child".
  • If the age is between 13 and 19, print "Teenager".
  • If the age is 20 or above, print "Adult".

Solution

int age = 15;

if (age < 13) {
    NSLog(@"Child");
} else if (age >= 13 && age <= 19) {
    NSLog(@"Teenager");
} else {
    NSLog(@"Adult");
}

Exercise 2: Day of the Week

Write a program that prints the name of the day based on a number (1 for Monday, 2 for Tuesday, etc.).

Task

  • Use a switch statement to print the day of the week.

Solution

int day = 3;

switch (day) {
    case 1:
        NSLog(@"Monday");
        break;
    case 2:
        NSLog(@"Tuesday");
        break;
    case 3:
        NSLog(@"Wednesday");
        break;
    case 4:
        NSLog(@"Thursday");
        break;
    case 5:
        NSLog(@"Friday");
        break;
    case 6:
        NSLog(@"Saturday");
        break;
    case 7:
        NSLog(@"Sunday");
        break;
    default:
        NSLog(@"Invalid day");
}

Common Mistakes and Tips

Common Mistakes

  • Forgetting the break statement in switch cases: This can cause the program to execute the next case as well.
  • Using = instead of == in conditions: = is an assignment operator, while == is a comparison operator.

Tips

  • Always use curly braces {} even for single-line statements to avoid errors and improve readability.
  • Use meaningful variable names to make your code more understandable.

Conclusion

In this section, you learned about conditional statements in Objective-C, including if, else if, else, and switch. These statements allow you to control the flow of your program based on different conditions. Practice the exercises provided to reinforce your understanding, and remember to watch out for common mistakes. In the next section, we will cover loops, which are essential for executing repetitive tasks efficiently.

© Copyright 2024. All rights reserved