Today I wanted to talk about the “for” statement but I realized that I can’t talk about that without first talking about loops, so here we go.

There may be situations where you have to execute the same block of code multiple times,that’s where loops come in. A loop statement allows us to execute a statement or a group of statement multiple times without needing to write them over and over again in your code.

The C++ programming language provides us with the following types of loops to handle looping requirements:

Loop Type Description
while loop Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.
for loop Execute a sequence of statements multiple times and abbreviates the code that manages the loop variable.
do…while loop Like a while statement, except that it tests the condition at the end of the loop body
nested loops You can use one or more loop inside any another while, for or do..while loop.

But we also have a few loop statements, that I will talk about in another post, to help us control those loops so we can save time, for example :

Control Statement Description
break statement Terminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch.
continue statement Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.
goto statement Transfers control to the labeled statement. Though it is not advised to use goto statement in your program because it can cause bugs ,make the program hard to understand and hard to modify.

If you need to execute a loop for an infinite number you can use an infinite loop. A loop becomes infinite if the condition set never becomes false,for example :


your code here



for( ; ; ){

your code here


I hope this helped you understand how loops work and when you should use them.

Keep on coding! 😉


One thought on “Loops

  1. Pingback: For statement | Dev In Control

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s