• Home
  • Troubleshoot
  • #Example
    • C
    • C++
    • Python
    • R-Programming
  • DSA
  • Quiz
  • Tutorial Videos
  • Home
  • Troubleshoot
  • #Example
    • C
    • C++
    • Python
    • R-Programming
  • DSA
  • Quiz
  • Tutorial Videos
  • #Deals
  • #News
  • #WiKi
  • #APPS
  • #Events
    • #WWDC
    • #I/O
    • #Ignite
  • #Let’s Talk

MyCodeTips mycodetips-newlogocopy1

  • Home
  • Troubleshoot
  • #Example
    • C
    • C++
    • Python
    • R-Programming
  • DSA
  • Quiz
  • Tutorial Videos
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 🙂

  • 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)
  • Click to share on Pocket (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
Written by Ranjan - 1837 Views
Tags | IOS, objective-c, Tips & Tricks
AUTHOR
Ranjan

I m Ranjan and Sharing my years of experience in Software Development. Love to code in Mobile apps (IOS, Android, Power Apps, Xamarin, Flutter), Machine Learning ( Beginner ), Dot Net, Databases ( SQL Server, MySql, SQLite), WordPress, Cloud Computing ( AWS, Azure, Google, MongoDB) and many more as required on project-specific. Besides this love to travel and cook.

You Might Also Like

comment-styles

Comment Styles in Different Programming Languages.

August 12, 2021
objective-c-datatypes1

Objective-C Data Types

November 12, 2021
optional-swift

Optional in Swift !

October 2, 2021
Next Post
Previous Post

Subscribe for updates

Join 5,733 other subscribers

whiteboard

Whiteboard(PRO)

whiteboard

Whiteboard(lite)

alphabets

Kids Alphabet

techlynk

Techlynk

techbyte

Do2Day

techbyte

Techbyte

Latest Posts

  • 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
  • swift-concurrency-await
    Concurrency in Swift
  • time-complexity-dsa
    The Term Time Complexity in DSA
  • objective-c-datatypes1
    Objective-C Data Types
  • Convert-jpeg-word
    Convert JPG to Word – Tips You Should Try!
  • objective-c-control-statements2
    Objective-C control statements and loops !

Quick Links

  • #about
  • #myapps
  • #contact
  • #privacy

Other Websites

  • #myQuestions
  • #myBhojanalaya
  • #gadgetFacts
  • #ifscCodesDB

Tag Cloud

Android Android Studio API APP Programming Apps ARC asp.net blogging Browser Config CSS DATABASE DFD error Features GUI HTML HTML5 IDE IIS installation Interview Questions IOS iPhone javascript Mac objective-c OneDrive OS Programming quicktips SDK SEO Settings SMO SQL swift swiftUI Teams Tips & Tricks Tools UI Web Wordpress Xcode

©mycodetips.com