• Home
  • MAD
    • IOS Series
    • Android Series
    • Flutter Series
    • Xamarin Series
  • Concept Series
    • Software Design
    • Software Arch
    • GIT & Github
    • System Design
    • Cloud
    • Database Integration
    • Push Notification
    • API Integration
    • Cocoa PODS
  • DSA
  • Interview
  • Tips&Tricks
  • YT
  • Home
  • MAD
    • IOS Series
    • Android Series
    • Flutter Series
    • Xamarin Series
  • Concept Series
    • Software Design
    • Software Arch
    • GIT & Github
    • System Design
    • Cloud
    • Database Integration
    • Push Notification
    • API Integration
    • Cocoa PODS
  • DSA
  • Interview
  • Tips&Tricks
  • YT
  • #News
  • #APPS
  • #Events
    • #WWDC
    • #I/O
    • #Ignite
  • #Let’s Talk

MyCodeTips mycodetips-newlogocopy1

  • Home
  • MAD
    • IOS Series
    • Android Series
    • Flutter Series
    • Xamarin Series
  • Concept Series
    • Software Design
    • Software Arch
    • GIT & Github
    • System Design
    • Cloud
    • Database Integration
    • Push Notification
    • API Integration
    • Cocoa PODS
  • DSA
  • Interview
  • Tips&Tricks
  • YT
Objective-c

Objective-C control statements and loops !

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 🙂

Liked it? Take a second to support Ranjan on Patreon!
become a patron button
  • Click to share on Reddit (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • More
  • Click to share on Pocket (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
Written by Ranjan - 4886 Views
Tags | IOS, objective-c, Tips & Tricks
AUTHOR
Ranjan

Namaste, My name is Ranjan, I am a graduate from NIT Rourkela. This website is basically about of what i learnt from my years of experience as a software engineer on software development specifically on mobile application development, design patterns/architectures, its changing scenarios, security, troubleshooting, tools, tips&tricks and many more.

You Might Also Like

mycodetips-newlogo2

How to move UP the UIVIEW when keyboard is display or typing in UITEXTFIELD in IOS !!!

December 18, 2014
Productive Web Developer

6 Ways to Be a More Productive Web Developer

September 17, 2021
troubleshooting-mac

Troubleshooting your Mac of SMC and PRAM/NVRAM

October 9, 2021
Next Post
Previous Post

Support us

mycodetips
mycodetips

Follow us @ LinkedIn 2850+

Subscribe for updates

Join 8,213 other subscribers

Latest Posts

  • YT-Featured-solidprinciples
    SOLID Principles of Software Design
  • IOS 16 Features
    Latest features in IOS 16
  • r-language
    How can R language be used for data analysis?
  • wordpress-coding-blog
    Guide To WordPress Coding Standards
  • YT-Featured-Algorithm
    What is Algorithm?
  • Frameworks of IOS
    Frameworks of IOS – Part ( I )
  • NSFileManager or NSPathUtilities
    NSFileManager or NSPathUtilities in Objective-C
  • Passing data between view controllers in Objective-C
    Passing data between view controllers in Objective-C
  • structures-classes-enum
    Structures and Classes in swift !
  • control-system-swift
    Control Flow in Swift
whiteboard

Whiteboard(PRO)

whiteboard

Whiteboard(lite)

alphabets

Kids Alphabet

techlynk

Techlynk

techbyte

Do2Day

techbyte

Techbyte

  • #about
  • #myapps
  • #contact
  • #privacy
  • #Advertise
  • #myQuestions

Android Android Studio API APP Programming Apps blogging CSS DATABASE dsa Features HTML HTML5 installation Interview Questions IOS iPhone javascript Mac objective-c OS Programming quicktips SDK SEO SQL swift Tips & Tricks Tools UI Web Wordpress Xcode

  • SOLID Principles of Software Design
  • Latest features in IOS 16
  • How can R language be used for data analysis?
  • Guide To WordPress Coding Standards
  • What is Algorithm?

©mycodetips.com