Do While loop in Objective-C
Last Updated :
28 Apr, 2025
Just like other programming languages Objective-C also supports do..while loop. do..while loop is also known as an inverted while loop because, in a while loop, the expression is evaluated before executing the code present inside the body of the while loop, if the expression is false, then this loop doesn't execute the body of the while loop. Whereas in the do..while loop, the expression is evaluated at the end of the loop body. In the do..while loop, the body of the loop will execute at least once irrespective of the test expression.
Syntax:
do
{
// Loop Body
// Update Statement
}while(expression)
do..while loop contains the following parts:
- Loop Body: The loop body is a collection of statements such as variables, expressions, functions, etc.
- Update Statement: This statement works after executing the loop body. It updates the loop variable either by incrementing or decrementing its value.
- Expression: It is a test condition. If the value of the expression is true, then the body of the loop will execute and update the value of the update statement. If the value of the expression is false, then the controls come out from the do..while loop.
Working of the do..while loop
The following steps will show the working of the do..while loop:
Step 1: Control encounter do..while loop.
Step 2: Execute the statements present inside the do..while loop.
Step 3: Updation takes place.
Step 4: Controls reach the test expression.
Step 5: Test the given expression:
- If the expression returns true, the control flow goes back to Step 2.
- If the expression returns false, the control flow goes outside the do..while loop.
Step 6: The do..while loop has been ended and the control flow has gone outside the loop.
Flow chart
Following is the flow chart of do..while loop:
Example 1:
ObjectiveC
// Objective-C program to demonstrate do..while loop
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int num = 8;
// Do while loop
do{
// Body of the loop
NSLog(@"GeeksforGeeks\n");
// Update statement
num++;
}
// Test condition
while (num < 10);
[pool drain];
return 0;
}
Output:
GeeksforGeeks
GeeksforGeeks
Here, in the above program, the num is initialized with 8. Now control enters into the do..while loop and print "GeeksforGeeks". Also, update num = 9. Now check the condition present in the while(num < 10) statement. Here the condition is true and controls again enter into the loop and print "GeeksforGeeks" and update num = 10. Now again check the condition(num < 10). This time condition is false and control comes out from the loop.
Example 2:
ObjectiveC
// Objective-C program to demonstrate do..while loop
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int num = 1;
int res;
// Printing multiplication table of 16 till 16
// Using do..while loop
do{
// Loop body
res = 16 * num;
NSLog(@"16 * %d = %d\n", num, res);
// Update statement
num++;
}while(num <= 16);
[pool drain];
return 0;
}
Output:
16 * 1 = 16
16 * 2 = 32
16 * 3 = 48
16 * 4 = 64
16 * 5 = 80
16 * 6 = 96
16 * 7 = 112
16 * 8 = 128
16 * 9 = 144
16 * 10 = 160
16 * 11 = 176
16 * 12 = 192
16 * 13 = 208
16 * 14 = 224
16 * 15 = 240
16 * 16 = 256
Similar Reads
While loop in Objective-C Just like other programming languages Objective-C also supports while loop. While loop is a loop that is used to repeat a statement or a group of statements till the given condition is true. Every time while loop checks the condition before executing its body. Or we can say that we can use a while l
3 min read
for loop in Objective-C Like most programming languages, Objective-C also has a repeating statement called for loop. It is a repetition control statement that allows you to repeatedly execute a single instruction or a block of instructions into a specified number of times. unlike other loops like while-loop or do-while loo
9 min read
while and do while Loop in Scala Looping in programming languages is a feature which facilitates the execution of a set of instructions/functions repeatedly while some condition evaluates to true. Loops make the programmers task simpler. Scala provides the different types of loops but in this article we understand while and do-whil
4 min read
Nested loops in Objective-C Like most programming languages, Objective-C also gives three basic repetition control statements, namely loops, which help programmers to execute a single or block of statements repetitively till the given condition is satisfied. Furthermore, these loops can be used one inside another which is call
10 min read
Variables in Objective-C A variable is a place for data storage that our programs can access or manipulate. In Objective-C, variables have a defined type that specifies their shape and layout. They also cover the gamut of values and operations that we are capable of performing. Before moving further, we understand the guide
4 min read
Log Handling in Objective-C Objective-C is a programming language commonly used for developing applications on Apple's macOS, iOS, and iPadOS platforms. One important aspect of programming in Objective-C is handling logs. Logging is the process of recording information about the state of a program or system in a log file, whic
6 min read