Control Flow in Objective C

Control flow is a fundamental concept in programming that allows you to dictate the order in which your code executes based on certain conditions and loops. For a junior Swift developer learning Objective-C, understanding control flow is crucial.

article

Control flow is a fundamental concept in programming that allows you to dictate the order in which your code executes based on certain conditions and loops. For a junior Swift developer learning Objective-C, understanding control flow is crucial. In this article, we will explore what control flow is, how to use different control flow statements in Objective-C, and provide examples to illustrate these concepts.


What is Control Flow?


Control flow refers to the order in which individual statements, instructions, or function calls are executed or evaluated in a program. By using control flow statements, you can create decision-making structures (like if-else statements), loops (like for and while), and other control structures that affect the flow of execution in your program.


Control Flow Statements in Objective C


If-Else Statements


If-else statements allow you to execute a block of code based on whether a condition is true or false. The Syntax looks as followed:


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

Example:


    
int temperature = 30;
if (temperature > 25) {
    NSLog(@"It's hot outside!");
} else {
    NSLog(@"It's not too hot outside.");
}
    

In this example, if the temperature is greater than 25, the first message is logged; otherwise, the second message is logged.


Switch Statements

Switch statements provide a way to execute different parts of code based on the value of a variable or expression.


    
int day = 3;
switch (day) {
    case 1:
        NSLog(@"Monday");
        break;
    case 2:
        NSLog(@"Tuesday");
        break;
    case 3:
        NSLog(@"Wednesday");
        break;
    default:
        NSLog(@"Another day");
        break;
}
    

Here, the switch statement checks the value of day and logs the corresponding day of the week.


Switch statements are in the general sense actually faster and use less processing power than If-Else statements when we are working with multiple conditions.


Looping Statements in Objective C


Loops allow you to execute a block of code multiple times.


For Loops


For loops are used when you know in advance how many times you want to execute a statement or a block of statements.


    
for (int i = 0; i < 5; i++) {
    NSLog(@"Iteration %d", i);
}
    

In this example, the loop runs five times, logging the current iteration number each time.


While Loops


While loops are used to repeat a block of code as long as a specified condition is true.


    
while (condition) {
    // Code to execute
}
    


    
int count = 0;
while (count < 5) {
    NSLog(@"Count is %d", count);
    count++;
}
    

This loop runs until count is no longer less than 5, incrementing count each time.


Do-While Loops


Do-while loops are similar to while loops, but they execute the block of code at least once before checking the condition.


    
do {
    // Code to execute
} while (condition);
    


    
int count = 0;
do {
    NSLog(@"Count is %d", count);
    count++;
} while (count < 5);
    

In this example, the loop executes the code block once and then checks the condition.



Break and Continue Statements in Objective C


In this example, the loop executes the code block once and then checks the condition.


Break Statement


The break statement terminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch.


    
for (int i = 0; i < 10; i++) {
    if (i == 5) {
        break;
    }
    NSLog(@"%d", i);
}
    

This loop prints numbers from 0 to 4 and then exits when i equals 5.


Continue Statement


The continue statement skips the remaining code inside the loop for the current iteration and jumps to the next iteration of the loop.


    
for (int i = 0; i < 10; i++) {
    if (i % 2 == 0) {
        continue;
    }
    NSLog(@"%d", i);
}
    

This loop prints only odd numbers between 0 and 9, skipping the even numbers.


Conclusion


Control flow statements in Objective-C, such as if-else, switch, for, while, and do-while loops, are essential tools for controlling the execution of your code based on conditions and repetitions. Understanding these structures will help you write more efficient and effective programs. While the syntax differs slightly from Swift, the underlying concepts are the same.

instructor

Exodai INSTRUCTOR!

Johan t'Sas

Owner and Swift developer!