Home / C languaege / Types of Loops in C Language

Types of Loops in C Language

There are three types of loops in c language .The repeatedly execution of statement or set of statement is called loop. In loop statement the statement or set of statement execute for a fix number of time or until the condition remains true .The loop structure is also called repetitive or iterative structure .There are three types of loop

  1. While Loop        2.Do while loop                   3. For loop
  2. loops in c language

#. While loop in c language

It is a conditional loop it executes a statement or set of statement repeatedly until the given condition remains true.

The syntax of while loop is

While (condition)

{

statement;

increment/decrement;

}

Condition: The conditional may be logical or relational expression after the evaluation the result will be true or false.

Statement:  It is the statement that will execute again and again until the condition remains true.

while loop

Write a program that display Hello five time using while loop.

#include <stdio.h>

#include <conio.h>

void main

{

int n;

n=1;

while (n<=5)

{

printf(“Hello\n”);

n =n+1;

}

getch ();

}

Output

1

2

3

4

5

Explanation:

In this program the initial value of n is 1 the program will print hello until the value of is equal to or smaller than 5.

First it initialize the value then check either condition Is true or false  if condition is true then Hello will be printed , the value of n is incremented by 1 and if condition is false then the program will be terminated .

2#. Do while loop

Ans: It is a type of loop that executes a statement or set of statement repeatedly until the given condition remains true.The working of do while loop is same to while loop but condition comes after the body of loop.

Syntax

do

{

statement:

increment/decrement;

 

}

While (condition)

Do:  It is a keyword that indicates the beginning of the loop body.

Condition: The conditional may be logical or relational expression after the evaluation the result will be true or false.

Statement:  It is the statement that will execute again and again until the condition remains true.

do while loop in c

Example

Write a program that display Hello five time using do while loop .

#include <stdio.h>

#include <conio.h>

void main

{

int n;

n=1;

do {

printf(“Hello\n”);

n =n+1;

}

while (n<=5)

; getch ();

}

Explanation .

In this program the initial value of n is 1 the program will print hello until the value of is equal to or smaller than 5.

First it check the value then check either condition Is true or false  if condition is true then Hello will be printed the value of n is incremented by 1 if condition is false then the program will be terminated.

3#.For loop

  For loop is used to execute statement or set of statement for a specific number of times it is also called counter loop.

Syntax: for (initialization; condition; increment/decrement)

{

Statement;

}

Initialization: In this part a variable is given a value more than one variable can also be used in this portion .If more than one variable are used then each variable is separated by coma.

Condition: The conditional may be logical or relational expression after the evaluation the result will be true or false.

Increment /Decrement: In this part the value of variable is increased or decreased.

Statement:  It is the statement that will execute again and again until the condition remains true.

for loop in C

Example

Write a program that display Hello five time using for loop .

#include <stdio.h>

#include <conio.h>

void main

{

int n;

for ( n=1;n<=5;n++)

printf(“Hello”);

getch();

}

Explanation:

In this program the initial value of n is 1 the program will print hello until the value of is equal to or smaller than 5.

First it check the value then check either condition Is true or false  if condition is true then Hello will be printed the value of n is incremented by 1 if condition is false then the program will be terminated .

4#.Nested Loop

The loop written within the body of another loop us called nested loop .Any loop can be written with in the body of any other loop

Syntax

while (condition )

{

While condition {

Statement;

}

}

nested loop in c

Example.

Write a program in C language that  Show the working of nested loop.

#include <stdio.h>

#include <conio.h>

void mian()

{

int x,y ;

x=1;

while(x<=3)

{

printf(“ Hello\n “);

for(y=1;y<=2,y++)

printf (“pakistan”);

x=x+1;

}

getch();

}

Output

Hello

Pakistan

pakistan

Hello

Pakistan

pakistan

Hello

Pakistan

pakistan

Explanation .:

First it check the value then check either condition Is true or false  if condition is true then Hello will be printed and control will be transfer to inner loop the inner loop will it will be executed until the  condition of inner loop  remain true if it get false the control will be transferred  to outer loop. This procedure will be repeated until the condition of outer loop remains true.

6#. Goto statement in C language

Goto statement is used to transfer flow of control to named label. It is unconditional statement .The label should be in same function .

Program .

Write a program in C language that takes a positive number and calculate its square .It does not accept negative number.

#include <stdio.h>

#include <conio.h>

void main()

{int x;

positive:

scanf(“%d”,&x);

If(x<o)

goto Positive

else

printf(“\n is %d”,x*x);

getch();

}

 

The following two tabs change content below.
Hi I am Ch Shahbaz from Pakistan.I love to writing different type of informative Articles. By profession i am a Pro Blogger, Web Developer, Graphic Designer and Creative writer.

Latest posts by Ch Shahbaz (see all)

Check Also

GW BASIC MCQS in Urdu and English

 GW BASIC MCQ,s in Urdu and English for class 10

 Often people feeling difficulty while making paper of GW BASIC MCQS in Urdu and English …

Leave a Reply

Your email address will not be published. Required fields are marked *