Cpp Tutorial: C++ Looping Statements

1. While loop
2. Do .. while loop
3. For loop
4. Break
5. Continue
6. Nested loops
Looping statements are used when we want to repeat the same set of statements again and again. Those set of statements will be executed until a particular condition is satisfied. Below we discuss about various types of looping constructs with simple examples.

1. While loop

Syntax:
while (test_condition)
{
	//statements
}

Working of while loop:

1. Test condition is evaluated, if it is true then the statements inside while block is executed.
2. In the execution, update the variable and again check the condition.
3. If the test condition fails, it will come out of the loop.
4. No semicolon at the end of the while statement.

Example for “while” loop.

#include<iostream>
using namespace std;

int main()
{
	int j = 0;
	int count = 2;

	while (j < count)
	{
		cout<<" J = "<<j << endl;
		j++;// don’t forget to increment. Otherwise it will become infinite loop
	}

return 0;
}
Output:
J = 0
 J = 1

2. Do .. while loop

Syntax:
do
{
	//statements
} while (test_condition);
Working of do .. while loop:
1. It is guaranteed that all the statements inside the do .. while statements are executed once.
2. The condition is checked while coming out of the loop.
3. Notice the semicolon at the end of the while() statement.
Example for “do .. while” loop:
#include<iostream>
using namespace std;

int main()
{
	int j = 0;
	int count = 2;

	
	{
		cout<<" J = "<<j << endl;
		j++;// dont forget to increment. Otherwise it will become infinate loop
	}while (j == 2)
 // Condition is false, but statements are executed atleast once.
return 0;
}
Output
J = 0

3. For loop:

Like many languages, C++ also provides for loop. C++ provides for loop with more flexibility that we shall look in the program example.
Below is the syntax.
	for(initialization; condition-checking; increment/decrement)
	{
		//statements
	}
Points to remember:
1. There is no semicolon at the end of for loop syntax.
2. Initialization is done only once.
3. Then it will check for condition, if true it will execute the statements then increments the value.
4. There can be multiple condition and increment/decrement statements.
5. Initialization can be done outside for loop also.
Example to show variants of for loop:
#include<iostream>
using namespace std;

int main()
{
	int j = 0;
	int count = 5;

	for (int i = 0; i < count; ++i)
	{
		cout<<i<<endl;
	}

	for(; ; ) // initialization is done on top
	{
		if (j == 5) // condition is checked here
			cout << "J is 5"<<endl;

		j++; // incremented here
	}

	for( ; ; ) //infinite for loop
	{
		// dangerous!!
		// your program will run indefinitely
	}

return 0;
}

4. Break

Break statement is used to come out of the loop. When it is encountered, the statement after the loop will be executed. If there is a break statement in the inner loop, then it will come to the outer loop.
Example for break statement:
#include<iostream>
using namespace std;

int main()
{
	int j = 0;
	int count = 5;

	while (j < count)
	{
		cout<<" J = "<<j << endl;
		j++;

		if ( j == 3)
			break; // it will come out of the loop, when j is 3
	}

return 0;
}
Output:
J = 0
 J = 1
 J = 2

5. Continue

“continue” is similar to break, but instead of coming out of the loop, it will skip the current iteration and continue with the next iteration.
Example for continue statement:
#include<iostream>
using namespace std;

int main()
{
	int j = 0;
	int count = 5;

	while (j < count)
	{
		j++;
		if ( j == 3)
			continue;
		// when j is 3, it will skip execution
		cout<<" J = "<<j << endl;
	}

return 0;
}
Output:
J = 0
 J = 1
 J = 2
 J = 3

6. Nested loops

A loop can be inside of another loop. Then it is called as Nested Loops.
Example for nested loop
#include<iostream>
using namespace std;

int main()
{
	int j = 1;
	int count = 3;

for (int i = 0; i < count; i++)
{
	j = i+1;
	while (j < count)
	{
		cout<<"I = "<< i<<" J = "<<j << endl;
		j = j + 1;
	}
}	
return 0;
}
Output:
I = 0 J = 1
I = 0 J = 2
I = 1 J = 2
Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *