Loops in C Programming



Loops in C ProgrammingLOOP:

  •  A statement or a set of statements that is executed repeatedly is called a loop.
  •  Loop is executed until the given condition remains true.
  •  Three loops in C are While Loop Do-While Loop For Loop

While Loop:

  •  Used to execute a statement or set of statements as long as the given condition remains true.
  •  Syntax of the “While” loop is

while (condition)

{

statement (s);
}

Example:

#include <stdio.h> #include <conio.h> main ( )

{
int c=1;

while (c<6)

{

printf (“I Love Pakistan\n”); c=c+1;

}

getche( );

}

Do-While Loop:

 Used to execute a statement or set of statements.
 Just like while loop but in this loop the condition is tested after executing the statements of the loop.

 Syntax of the “While” loop is

do
{

statement (s);
}

while (condition);

Example:

#include <stdio.h> #include <conio.h> main ( )

{

int c=1; do

{

printf (“I Love Pakistan\n”); c=c+1;

}
while (c<6); getche( );

}

For Loop:

  •  Used to execute a statement or set of statements as long as the given condition remains true.
  •  Also called Counter loop.
  •  It has following parts

1. Initialization

2. Condition

3. Increment or Decrement

4. Body of the Loop

  •  General syntax of For Loop is for (initialization; condition; increment / decrement)

{

Statements;

}

 Example:

#include <stdio.h> #include <conio.h> main ( )

{
int a;

for (a=5; a<15; a++)

{
printf (“I Love Pakistan\n”);

}
getche( );

}

NESTED LOOPS:

 A loop can be declared inside another loop called nested loop.
 Loops can be nested For-Loop, While Loop, Do-While Loop or mixture of these.

Example:

#include <stdio.h> #include <conio.h> main ( )
{

int a, b;

for (a=1; a<5; a++)
for (b=1; b<3; b++)

{

printf (“I Love Pakistan\n”);
}

getche( );
}

 

About Saweel Ur Raheem

Check Also

MCSE Notes Lecture # 4 : Applying Security to Folders

LECTURE 4 APPLYING SECURITY ON FOLDERS: Open Folder Options. Go to View Tag and uncheck ...

© Copyrights 2014. All rights are reserved www.latestcontents.com