Loop in C

Repeatation of statements or code is called a loop, when there is need to repeat some statements or code in the program loop is used.

for loop

#include<stdio.h>
#include<conio.h>
int main(){
	int n;
	for(n=1; n<=10; n=n+1){
		printf("%d \n", n);
	}
	getch();
	return 0;
}

while loop

#include<stdio.h>
#include<conio.h>
int main(){
	int n;
	n=1;
	while(n<=10){
		printf("%d \n", n);
		n=n+1;
	}
	getch();
	return 0;
}

do while loop

#include<stdio.h>
#include<conio.h>
int main(){
	int n;
	n=1;
	do{
		printf("%d \n",n);
		n=n+1;
	}
	while(n<=10);
	getch();
	return 0;
}

Nested loop

#include<stdio.h>
#include<conio.h>
int main(){
	int row, col;
	for(row=1; row<=3; row=row+1){
		for(col=1; col<=3; col=col+1){
			printf("*");
		}
		printf("\n");
	}	
	getch();
	return 0;
}
© 2016 - 2022, All Rights are Reserved.