This article covers the different types of loops in C++.
Like any programming language, C++ has a variety of different loops used to perform different tasks. But what are loops? Simply put, loops are control statements that continuously repeat a block of code. Using loops helps prevent redundancy in our code as don’t have to re-write the same block over and over.
C++ also has several loop control statements which are used to manipulate loops while they are running.
Types of Loops
The three types of loops in C++ are listed below.
Statement | Description |
---|---|
do while | Executes a block of code once, and will keep repeating as long as a given given condition holds true. |
while | Repeats a block a code while a given condition holds true. |
for | Repeats a block of code for a given number of times. Can also be used to iterate through collection types variables, like Arrays. |
Loop Control Statements
There is also the GoTo statement, however as using it is not good practice, we didn’t include it here.
Name | Description |
---|---|
break | When a loop encounters this statement during its execution, it will immediately stop and “break”, moving to the next statement after the loop. |
continue | The loop will stop it’s current iteration, and move on to the next iteration. |
This marks the end of the C++ Loops article. Any suggestions or Contributions for CodersLegacy are more than welcome. Questions can be asked in the comments section below.