Loop Control Statements

A loop (also referred to as an iteration or repetition) is a sequence of statements which is specified once but which may be carried out several times in succession. The code “inside” the loop (the body of the loop) is obeyed a specified number of times, or once for each of a collection of items, or until some condition is met.

A block or group of statements executed repeatedly until some condition is satisfied is called Loop. The group of statements enclosed within curly brace is called block or compound statement.

We have two types of looping structures. One in which condition is tested before entering the statement block called entry controlled loop. The other in which condition is checked at exit called exit controlled loop.

Loop statements can be divided into three categories as given below:

  1. while loop statement (Pre conditional)
  2. do while loop statement (Post conditional)
  3. for loop statement (Pre conditional)

Condition-Controlled loops

Condition-controlled loops are divided into two categories Pre-conditional or Entry-Condition that place the test at the start of the loop, and Post-conditional or Exit-Condition iteration that have the test at the end of the loop. In the former case the body may be skipped completely, while in the latter case the body is always executed at least once.

1. While Loop (Pre conditional)

Syntax:

While Loop Structure
While Loop Structure
While(test condition)
{
          body of the loop
}

It is an entry controlled loop. Semantic of Loop is as below:

  1. The condition is evaluated and if it is true then body of loop is executed.
  2. After execution of body the condition is once again evaluated and if is true body is executed once again.
  3. This goes on until test condition becomes false.

Example:

#include <iostream>
using namespace std;
int main()
{
	int i=0;
	while (i<10) 
	{
		cout << "The value of i is " << i << endl;
		i++;
	}
	cout << "The final value of i is : " << i< < endl;
	return 0;
}

Output:

The value of i is 0
The value of i is 1
The value of i is 2
The value of i is 3
The value of i is 4
The value of i is 5
The value of i is 6
The value of i is 7
The value of i is 8
The value of i is 9
The final value of i is 10

2. do-while loop statement (Post-conditional)

The do – while loop is similar in syntax and purpose to the while loop. The construct moves the test that continues condition of the loop to the end of the code block so that the code block is executed at least once before any evaluation.

Syntax:

Do While Structure
do 
{
	statement(s)
} while (condition);

The do while is an exit controlled loop and its body is executed at least
once. Semantic of the loop is as below:

  1. statement(s) are executed.
  2. condition is evaluated.
  3. if condition is true goes to (1).
  4. if condition is false continues with statement2.

Example:

#include<iostream>
using namespace std;
int main()
{
	int i=0;
	do 
	{
		cout << "The value of i is " << i << endl;
		i++;
	}while (i<10);
	cout << "The final value of i is : " << i << endl;
	return 0;
}

Output:

The value of i is 0
The value of i is 1
The value of i is 2
The value of i is 3
The value of i is 4
The value of i is 5
The value of i is 6
The value of i is 7
The value of i is 8
The value of i is 9
The final value of i is 10

3. FOR Loop (Pre-conditional and Counter-controlled loop)

A special case of a Pre-conditional loop that supports constructors for repeating a loop only a certain number of times in the form of a step- expression that can be tested and used to set a step size (the rate of change) by incrementing or decrementing it in each loop.

Syntax:

For Loop Structure
For Loop Structure
for(initialization; test expression; increment/decrement)
{
      statements;
}

For statement is divided into three expressions each is separated by semi colon; All three sections are :

  1. initialization expression is used to initialize variables
  2. test expression is responsible of continuing the loop. If it is true, then the program control flow goes inside the loop and executes the block of statements associated with it .If test expression is false loop terminates
  3. increment/decrement expression consists of increment or decrement operator This process continues until test condition satisfies.

Example:

/*A program to find sum of n natural numbers */
#include<stdio.h>
int main()
{
	int i ,sum = 0,n;
		cout<<”Enter N";
		cin>>n;
	for(i=1;i<=n;i++)
	{
		sum = sum + i;
	}
	cout<<“Sum of first”<<n<<” natural numbers is:%d”<<sum; 
	return 0;
}

Nested Loops:

Writing one loop control statement within another loop control statement is called nested loop statement.

Nested loops Structure
Nested loops Structure

Syntax:

for(i=1;i<=10;i++)
{
     for(j=1;j<=10;j++)
     {
         cout<<i<<j;
     }
}

Example:

/*program to print prime numbers upto a given number*/
#include<stdio.h>
#include<conio.h>
void main()
{
	int n,i,fact,j;
		cout<<"enter the number:";
		cin>>n
	for(i=1;i<=n;i++)
	{
		fact=0;
		//THIS LOOP WILL CHECK A NO TO BE PRIME NO.OR NOT
		for(j=1;j<=i;j++)
		{
			if(i%j==0)
				fact++;
		}
	if(fact==2)
		cout<<i<<”\t”;
	}
	getch( );
}
Output:
Enter the number : 5
2 3 5

Related posts