Skip to main content

c++ loops

Looping in C++


#include <iostream>
using namespace std;

int main()
{

	//Do-While Loop
    //Exit controlled loop
	int k = 1;
	do
	{
		cout << k << endl;
		k++;
	} while (k < 11);

	//For Loop
    //Pre-test loop
	for (int i = 1; i < 11; i++)
	{
		cout << i << endl;
	}

	//While Loop
    //Pre-test loop
	int j = 1;
	while (j < 11) 
	{
		cout << j << endl;
		j++;
	}
	return 0;
}

You can have a loop within a loop this is called nested loop. Here is an example of nested loop.


#include <iostream>
using namespace std; 

int main() {
	int j = 12;
	while (j <= 15) 
	{
		int i = 1;
		while (i <= 12)
		{
			cout << i << " * " <<j<< " = " << i * j << "\n";
			i++;
		}
		j++;
		cout << endl;
	}
	return 0;
}

Comments