In programming, as in life, you have to make decisions and act on them. Objective-C provides control statements and loops to help your program take action. You may want to repeat a set of instructions based on some condition or state, for example, or even change the program execution sequence. Here is the basic syntax for Objective-C control statements and loops.
for loop
A for loop executes a sequence of statements multiple times with a loop variable.
Syntax
The syntax of a for loop in Objective-C programming language is:
for ( init; condition; increment )
{
statement(s);
}
init is executed first, and only once. We can declare and initialize loop control variables here.
Then condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow control jumps to the next statement just after the for loop.
After the body of the for loop executes each time, increment statement is execusted. We can update loop control variables here.
while Loop
while loop repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.
do…while loop is like a while statement, except that it tests the condition at the end of the loop body.
Syntax
The syntax of a while loop in Objective-C programming language is:
while(condition)
{
statement(s);
}
statement(s) may be a single statement or a block of statements.
condition may be any expression, and true is any nonzero value.
The loop iterates while the condition is true.
The syntax of a do...while loop in Objective-C programming language is:
do
{
statement(s);
}while( condition );
break Statement
break statement terminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch.
Example
#import <Foundation/Foundation.h>
int main ()
{
/* local variable definition */
int a = 10;
/* while loop execution */
while( a < 20 )
{
NSLog(@"value of a: %d\n", a);
a++;
if( a > 15)
{
/* terminate the loop using break statement */
break;
}
}
return 0;
}
continue Statement
continue statement causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.
Example
#import <Foundation/Foundation.h>
int main ()
{
/* local variable definition */
int a = 10;
/* do loop execution */
do
{
if( a == 15)
{
/* skip the iteration */
a = a + 1;
continue;
}
NSLog(@"value of a: %d\n", a);
a++;
}while( a < 20 );
return 0;
}
if Statement
An if statement contains a boolean expression followed by one or more statements.
An if statement can be followed by an optional else statement, which executes when the boolean expression is false.
We nest one if or if else statement inside another if or else if statement(s).
Syntax
The syntax of an if statement in Objective-C programming language is:
if(boolean_expression)
{
/* statement(s)*/
}
If the boolean expression is true, then the statement(s) will be executed.
If boolean expression is false, then the first statement after the if statement closing curly brace will be executed.
The syntax of an if...else statement:
if(boolean_expression)
{
/* statement(s) */
}
else
{
/* statement(s) */
}
The syntax of an if...else if...else statement is:
if(boolean_expression 1)
{
...
}
else if( boolean_expression 2)
{
...
}
else if( boolean_expression 3)
{
...
}
else
{
...
}
switch Statement
A switch statement tests a variable for equality against a list of values.
Syntax
The syntax for a switch statement in Objective-C programming language is as follows:
switch(expression){
case constant-expression :
statement(s);
break; /* optional */
case constant-expression :
statement(s);
break; /* optional */
case ...
...
break;
case ...
...
break;
default : /* Optional */
statement(s);
}
Blocks
Blocks in Objective-C are similar to closures or lambdas in other programming languages
Syntax
Here is the syntax to declare a block in Objective-C.
returntype (^blockName)(argumentType);
The following is the syntax we can use to implement the block.
returntype (^blockName)(argumentType)= ^{
};
Example
The following code defines a block.
void (^simpleBlock)(void) = ^{
NSLog(@"This is a block");
};
We can invoke the block using.
simpleBlock();
Happy Coding 🙂
Discover more from mycodetips
Subscribe to get the latest posts sent to your email.