Looping, Repetition Statements, Iteration Control Statements

Repetition is the process of executing a statement or a block of statements enclosed between braces more than one time as long as some condition remains true.

Repetition in C can be implemented using three control statements: for, while, and do-while statements. These statements are also known as iteration or looping statements.

Looping Repetition Statements Iteration Control Statements - INFORMERGURU

For loop statement

When we have the fixed number of iterations known then we use for loop (normally, while loop is also possible). Its basic syntax is:

for(expr1; expr2; expr3)

First, before the loop starts, the initialization (expr1) is executed that initializes the variable or variables in the loop. Then, condition (expr2) is evaluated, if it is true, the statement in the body of for loop will be executed.

Finally, the increment/decrement (expr3) will be executed, and the condition (expr2) will be evaluated again, this continues until the condition (expr2) is false, at which point control passes to the next statement outside of the loop.

While loop statement

When we do not know the number of iterations in advance then we use while loop (normally, for loop is also possible). Its basic syntax is:

initialization;
while(condition)
{
statement;
increement/decrement;
}

Here while is executed as long as the expression/condition is true. If the expression is true, then body statements are executed and control is passed back to the beginning of the while statement, otherwise, control passes to the next statement outside of the while block.

The do-while loop statement

Do-while is similar to the while statement but condition is tested after the body of the loop is performed.

Its basic syntax is:

do
{
statement1;
increement/decrement;
} while(expression);

At first body of do-while is executed, and then the expression is evaluated. If the value of the expression is true, then control passes back to the beginning of the do statement and repeats itself. Else control passes to the next statement outside of the do-while statement block.

Nested loops

Like selection statements, a loop can be nested within another. The inner and outer loops need not be the same. It is essential, however, that one loop be completely embedded within the other, so that there can be no overlap.

When one loop is nested inside another loop, the inner loop is first terminated and again restarted when the first loop starts for the next incremented value. The outer loop is terminated last.


Examples on Looping, Repetition Statements, Iteration Control Statements

Write a program to enter any positive number & calculate the sum of its digits

/*WAP to enter any positive number & calculate the sum of its digits*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n, sum=0;
printf("Enter any number: ");
scanf("%d",&n);
while(n!=0)
{
	sum=sum+n%10;
	n=n/10;
}
printf("The sum of digits is %d",sum);
getch();
}

Write a program to display the multiplication table of any given number using do-while

/*WAP to display the multiplication table of any given number upto 10th term */
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
printf("Enter a number to show multiplcation table: ");
scanf("%d",&n);
i=1;
do
{
	printf("\n %d",n*i);
	i++;
}while(i<=10);
getch();
}

Write a program to find factorial of any given number

/*WAP to find factorial of any given number*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,fact=1;
printf("Enter a number to find factorial: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
	fact=fact*i;
}
printf("\n The factorial is %d",fact);
getch();
}

Write a program to find the reverse of a given number & check if the number is palindrome or not

/*WAP to find reverse of a given number & check if the number is palindrome or not*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n,temp,rev=0,pal;
printf("Enter any number: ");
scanf("%d",&n);
pal=n;
while(n!=0)
{
	temp=n%10;
	rev=rev*10+temp;
	n=n/10;
}
printf("\n Reverse of the given number is %d",rev);
if(rev==pal)
{
	printf("\n The number is palindrome");
}
else
{
	printf("\n The number is not palindrome");
}
getch();
}

Write a program to find Fibonacci series up to ‘n’ terms

/*WAP to find fibonancci series up to 'n' terms*/
#include<stdio.h>
#include<conio.h>
void main()
{
	int a=0,b=1,c,i,n;
	printf("Enter a number for fibonnanci series: ");
	scanf("%d",&n);
	printf("%d \t %d",a,b);
	for(i=3;i<=n;i++)
	{
		c=a+b;
		printf("\t%d",c);
		a=b; b=c;
	}
	getch();
}

Print Friendly, PDF & Email

Leave a Comment

Hey, wait!Get Regular Update's

Subscribe Stay Connected, Keep Learning!