8_Objective-C Loops

Some times we  need to execute a block of code several number of times.

In general, statements are executed sequentially.

A loop statement allows us to execute a statement or group of statements multiple times.

Types of loops

  • while loop
  • for loop
  • do…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.

Syntax:

while(condition)
{
   statement(s);
}

Example

For loop

  • Execute a sequence of statements multiple times
  • abbreviates the code that manages the loop variable.

Syntax:

for ( init; condition; increment )
{
   statement(s);
}

Example

Do…while loop

  • Like a while statement
  • Except that it tests the condition at the end of the loop body.

Syntax:

do
{
   statement(s);

}while( condition );

Example

Loop Control Statements:

Loop control statements change execution from its normal sequence.

  • break statement : Terminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch.

Example

  • continue statement : Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.

Example

Relational & Logical Operators

In chapter of Objective-C Operators we give some discripe of Relational & Logical Operators but without examples because the examples base on Objective-C Decision Making

Example of Relational Operator

Example of Logical Operator

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: