System ProgrammingLearn Do While loop in C++ Programming

Learn Do While loop in C++ Programming

After learning the for and while loops, we are on a date to see the third type: the do loop. After that, we will learn how to control loops. An interesting article like its predecessor, enjoy!!

do Loop
The do loop works like the while loop; they both continue to execute their code as long as a certain condition is met. The difference between the two types is that while evaluates the condition at the beginning of the loop. Conversely, the do loop evaluates its condition at the end, so the body of the do loop will be executed before the condition is tested. This means the loop body of a do loop is guaranteed to be executed at least once.

Syntax

do
{
	Loop Body
}
while (CONDITION);

Example
Again, let’s re-write the Odd-Even program using the do loop.

Consider the following code:

#include<iostream.h>
#include<conio.h>
void main()
{
	int x;
	clrscr();
	do
	{
		cout << "Enter an integer number: ";
		cin >> x;
		if (x % 2 == 0)
			cout<<x<<" is an Even number\n";
		else
			cout<<x<<" is an Odd number\n";
	}
	while (x!=0);
}

Will that do the job?!
1

Great! Notice that in this version, we don’t need to initialize x to 1 (as we did in the while loop version). The reason is clear, the do loop guarantees executing its body at least once.

Which One to Use? And When?
So, we have three types of loops. The question now is: which type to use? And when?
Based on what we have already learn, we can summarize the answer in the following few points:

  • When the number of iterations is known in advance, use the for loop.
  • If the number of times to execute the loop body is unknown, the for loop can’t help. In this case, use either the while loop or the do loop.
  • When choosing between while and do loops, if the loop body must be guaranteed to be executed at least one time, use the do loop.

Learn the Basics of C Programming Language

Controlling Loops
Sometimes, the logic of your program may dictate to skip the execution of specific loop iteration, or to exit the loop completely. Such decision is often (if not always) controlled by a condition. In this context, C++ has two statements: the continue statement, and the break statement.

continue
The continue statement skips the remaining statements in the loop body for this iteration, and transfers control to the top of the loop for new iteration.

break
Once seen, the break statement causes the loop to exit.

Example
Write a program that repeatedly asks the user to enter a positive integer to calculate its factorial. If the user enters any negative number, the program exits.

Consider the following code:

#include <iostream.h>
#include <conio.h>
void main()
{
	clrscr();
	int num;
	while (1)
	{
	  long factorial=1;
	  cout << "\nEnter a positive Integer: ";
	  cin >> num;
	  if (num<0)   break;
	  else if (num==0 || num==1)
	  {
	     cout << "Factorial of "<< num << " equals 1\n";
	     continue;
	  }
	     for (int i=num; i>1; i--)
		factorial*=i;
	     cout << "Factorial of " << num << " equals " << factorial<<endl;
	}
}

Let’s execute it and see what we will get:
2
Perfect!

Summary
That was part two in the Loops topic.

  • The do loop uses the same logic as the while loop, except that the condition comes after the loop body.
  • In all cases, the loop body of the do loop will be executed at least once.
  • The continue and break statements could be used to control loops. The continue statement skips the execution of certain iteration, while the break statement exits the loop completely.

The next article will talk about Functions. A very important topic that you should not miss. Just wait for us, and we won’t be late.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -