Understanding the types of loops in C++

Blog » Understanding the types of loops in C++

Posted on 25 Jul 2012 08:59


As we all know, C++ is usually referred to as the intermediary language since it combines the features of both high level and low level languages. Since, this is a coding language which operates upon the instructions you give, there may be certain situations where you may want to execute a set of instructions repeatedly. So, repeating the codes may become too tedious. That is where a “Loop” comes into action.

In simple terms, loops are a set of commands that allow you to execute a set of instructions again and again (without needing to repeat the coding) until the instruction is met. Before you understand loops, you must know the concept of ‘True’ and ‘False’ in C++. The basic concept is that, when a loop is executed, if the answer obtained meets the conditions specified by the programmer, the value becomes ‘True’ and it goes on to the next code. Whereas, if the answer obtained does not match the specified condition, the value is termed as ‘False” and the loop either ends or re-executes itself depending upon the program.

Loops are of three types in C++:
• For loop
• While loop
• Do While loop

For Loops

The main purpose of For loops is to execute the statement again and again until the condition is true. So, here, the user very well knows how many times the loop has to execute the instruction. The For loop operates in three stages,

It is the first to get executed. Here, the user specifies or assigns a value to the variable which is executed only once.

Once the condition is examined, if the value is true, the loop continues else the loop stops. Finally, depending upon the condition in the increment column, the for loops return to the condition column.


While Loops

A While loop executes a set of instructions or statements when the specified condition is true. The primary difference between a While loop and a Do While loop is that, a While loop examines the given condition prior to executing the body. The given condition is enclosed within braces ( ), which forms the body of the loop. Thus, it functions as follows:

• The program starts with the user assigning a value to the variable.
• The loop begins to implement. Since, this is a While loop, the condition is checked first and then it is executed. On execution, the loop verifies whether the condition points to true or false. If the condition is true, the statement gets executed, else it ends.


Do While Loop

The slightly modified While loop:

The Do While loop is very much similar to the While loop. As mentioned earlier, here the condition is examined after the execution of one loop. The other vital difference is that, the Do While loop requires the presence of a semicolon after the loop condition, which is by default at the bottom. Whereas, the While loop does not require any semicolon to be present after the condition.


Though it may sound a bit difficult in the beginning, I’m sure you will enjoy working with loops once you understand them. My personal advice is that, you should constantly practice a number of examples in loops to master them.

This article has been brought to you by www.firsthomealarm.com - offers full home security to help protect your family, assets from burglary and other crimes. Visit now

Rate this artile

rating: 0+x

Bookmark this article


Leave a comment

Add a New Comment
or Sign in as Wikidot user
(will not be published)
- +
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.