C plus plus programming
June 3, 2026 1 min read

Complete Guide to Loops in C++: For, While, Do-While, and Nested Loops

HS
HEMANT SINGH
Technical Writer & Educator

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<iostream>
#include<conio.h>
using namespace std;
int main(){
	int n;
	for(n=1; n<=10; n=n+1){
		cout<<n;
	}
	getch();
	return 0;
}

while loop

#include<iostream>
#include<conio.h>
using namespace std;
int main(){
	int n;
	n=1;
	while(n<=10){
		cout<<n;
		n=n+1;
	}
	getch();
	return 0;
}

do while loop

#include<iostream>
#include<conio.h>
using namespace std;
int main(){
	int n;
	n=1;
	do{
		cout<<n<<endl;
		n=n+1;
	}
	while(n<=10);
	getch();
	return 0;
}

Nested loop

#include<iostream>
#include<conio.h>
using namespace stdl
int main(){
	int row, col;
	for(row=1; row<=3; row=row+1){
		for(col=1; col<=3; col=col+1){
			cout<<"*";
		}
		cout<<endl;
	}	
	getch();
	return 0;
}

In this category