Loops in C programming

 

 

Looping

The computer has the ability to perform a set of instructions repeatedly. This involves repeating some portion of a program either for a specified number of times or till a given condition is satisfied This repetitive operation is done by a loop control statement. There are three methods for generating repetition of a certain part of the program
. They are:
  • for statement
  • while statement
  • do...while statement

For loop

The for loop is the most commonly used statement in C. This loop consists of three expressions. The first expression issued to initialize the index value, the second to check whether the loop is to be continued again and third to change the index value for further repetition.

Syntax
for (initialization; condition;  increment/decrement)
{
statement(s);
}

First initialization is executed. Then condition is evaluated. If condition is nonzero (true), then statement (s) is executed, after statement(s) increment/ decrement is executed.

Example:

/*A C program to print C programming 10 times using for loop.*/

#include <stdio.h>
#include <conio.h>
void main()
{
int i;
clrscr();
for(i=1;i<=10; i++)
{
printf("C programming\n");
}
getch();
}

Output

C programming

C programming 

C programming

C programming

C programming

C programming

C programming

C programming 

C programming

Flowchart

Nested for loop


If a body of for loop is declared inside another for loop then it is called nested for loop or Using a for loop within another for loop is said to be nested for loop.

Syntax

for (initialization; condition; increment/decrement)
{
for (initialization; condition; increment/decrement)
}

Example:

/*A C program to create the multiplication table of all the numbers from 1 to 5.*/

#include <stdio.h>

#include <conio.h>

void main()

{
 int a,b,c;clrscr();
for (a=1 ; a<=5;a++) { printf("\nMultiplication table no. of \%d",",a) ;

for (b=1; b<=10;b++)
{
c = ab ;
printf f("/n\%d*\%d=\%d^ prime prime ,a,b,c);
}
}
getch();
}

Output:
Multiplication table no. of 1

1* 1=1

1*2=2

1*3=3

1* 4=4

1 *5=5

1 * 6=6

1 * 7=7

1*8=8

1*9=9

1*10=10

while loop statement

The second type of loop statement is while loop. While loop first checks whether the initial condition is true or false and finding it to be true, it will enter the loop and execute the statement.

Syntax

initialization;

while (condition)

{
Statement_1;
...........
Statement_n;
increment/decrement;
}

Example:

/*A C program to print first 10 natural numbers using while loop.*/

#include <stdio.h>

#include <conio.h>

void main()
{
int number=1;
clrscr(); 
while (number<=10)
{
printf("%d\t", number);
number++;
}
getch();

}

Output:
1 2 3 4 5 
6 7 8 9 10

do.. while loop

Ans: do...while loop is another repetitive loop used in C programs. In case of do..while loop, as it enters the loop at least once and then checks whether the give condition is true or false. As long as the test condition is true, statements will be repeated again and again, otherwise, loop will terminate.

Syntax

initialization;
do 
{
statement1;
••••••••••••••
statement n;
increment/decrement;
}
while (condition);

Example

/*A C program to find the sum of odd numbers up to n using do..while loop where the value of n should be entered by user.*/

#include <stdio.h>
#include <conio.h>
void main()
{
int n, sum=0, i = 1 ;
clrscr();
printf("Enter the value of n:") ;
scanf("%d", &n);
do
{
sum+=1; / * it is same as sum sum + 1 */
i +=2:
}
while (i <= n)
printf("The sum of odd number is \%d", sum);
getch();
}

Output

Enter the value of n:7 
The sum of odd number is 16




Author Spotlight

Santosh Chapagain
Gmail: chapagainsantoshcs@gmail.com
Phone no: +977-9863512955

Post a Comment

1 Comments