C++ Programming

C++ Programming

C++ Loops


Introduction

C++ Loops are also referred to as iteration statements. The iteration (for, while, and do-while loop) statements allows a set of instruction to be performed repeatedly until a certain condition is fulfilled. The iteration statements are also called loops or looping statements. C++ provides three kinds of loops :

  • for loop
  • while loop
  • do-while loop

All three loop constructs of C++ repeat a set of statements as long as a specified condition remains true. This specified condition is generally referred to as a loop control. For all three loop statements, a true condition is any nonzero value. A zero value indicates a false condition.

Loop Parts

Every loops has its elements that control and govern its execution. Generally, a loop has four elements that have different purposes. These elements are as :

  • Initialization Expression(s)
  • Test Expression
  • Update Expression(s)
  • Loop’s Body

Let’s discuss these parts of a loop one by one.

Initialization Expression(s)

Before entering in a loop, its control variable(s) must be initialized. The initialization of the control variable(s) takes place under initialization expression(s). The initialization expression(s) give(s) the loop variable(s) their first value(s). The initialization expression(s) is executed only once, in the beginning of the loop.

Test Expression

The test expression is an expression whose truth value decides whether the loop-body will be executed or not. If the test expression evaluates to true i.e., 1, the loop-body gets executed, otherwise the loop is terminated.

In an entry-controlled loop, the test-expression is evaluated before exiting from the loop. In C++, the for loop and while loop are entry-controlled loops and do-while loop is exit-controlled loop.

Update Expression(s)

The update expression(s) change the value(s) of loop variable(s). The update expression(s) is executed; at the end of the loop after the loop-body is executed.

Loop’s Body

The statements that are executed repeatedly (as long as the test-expression is nonzero) from the body of the loop. In an entry-controlled loop, first test-expression is evaluated and if it is nonzero, the body-of-the-loop is executed; if the test-expression evaluates to be zero, the loop is terminated. In an exit-controlled loop, the body-of-the-loop is executed first and then the test-expression is evaluated. If it evaluates to be zero, the loop is terminated otherwise repeated. Now let’s discuss the C++ loops: for, while and do-while.

C++ for Loop

The for loop is the easiest to understand of the C++ loops. All its loop-control elements are gathered in one place (on the top of the loop), while in the other loop construction of C++, they (top-control elements) are scattered about the program.

C++ for Loop Syntax

The general form (syntax) of the for loop statement is :

for(initialization expression(s); test-expression; update expression(s))
{
	body-of-the-loop ;
}

C++ for Loop Example

The following example program illustrates the use of for statement :

/* C++ Iteration Statements - C++ for Loop */

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	int i;
	for(i=1; i<=10; i++)
	{
		cout<<i<<" ";
	}
	getch();
}

he following lines explain the working of the above given for loop program :

  • Firstly, initialization expression is executed, i.e., i=1 which gives the first value 1 to variable i.
  • Then, the test expression is evaluated i.e., i <= 10 which results into true i.e., 1.
  • Since, the test expression is true, the body-of-the-loop i.e., cout << i << ” “;
    is executed which prints the current value of i then a single space.
  • After executing the loop-body, the update expression i.e., i++ is executed which increments the value of i. (After first execution of the loop, the value of i becomes 2 after the execution of i++, since initially i was 1).
  • AFter the update expression is executed, the test-expression is again evaluated. If it is true the sequence is repeated from the step no 3, otherwise the loop terminates.

Note – Use for loop when you have to repeat a block of statements specific number of times.

C++ Infinite Loop

Although any loop statement can be used to create an infinite loop (endless loop) yet for is traditionally used for this purpose. An infinite for loop can be created by omitting the test-expression as shown below :

for(i=25; ; i--)
{
	cout << "This loop runs forever..!!" ;

Similarly, the following for loop is also an infinite loop :

for( ; ; )
{
	cout << "I will not stop..!!" ;
}

Here is an example of infinite loop in C++

/* C++ Loops - C++ Infinite Loop */

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	for( ; ; )
	{
		cout<<"This loop will run forever..!!";
	}
	getch();
}

When the conditional expression is absent, it is assumed to be true. You may have an initialization and increment expression, but C++ programmers more commonly use the for(;;) construct to signify an infinite loop

C++ Empty Loop

If a loop does not contain any statement in its loop-body, it is said to be an empty loop. In such cases, a C++ loop contains an empty statement i.e., a null statement. Following for loop is an empty loop:

for(i=20; (i); i--) ;

See, the body of the above for loop contains just (a null statement). It is an empty loop. An empty for loop has its applications in pointer manipulations where you need to increment or decrement pointer position without doing anything else.

Time delay loops are often used in programs. The following code shows how to create one by using for :

for(t = 0; t < 3000; t++) ;

That means if you put a semicolon after for’s parenthesis it repeats only for counting the control variable. And if put a block of statements after such a loop, then it is not part of for loop. For example, consider the following :

for(i=0; i<10; i++) ;
{
	cout << "i = " << i << endl;
}

Now it’s time to do it yourself and feel free to mess around with the program to understand in-depth what each statement does. Don’t forget to save your program.

That’s it for today, see you next week!!!