C++ Loops: In C++ loops are used when you need to execute a block of code several times. In loops, a statement will be executed once and the loop will be executed as many times as we need. And we can also say that a loop is a sequence of instructions that is executed until a certain condition is reached. So in loops, we can initialize variables, check the condition and increment/decrement value. The main advantage of using loops is we need not write the code many times.
- In Loops, the operation is done, when you are getting an item of data and changing it, then such that a condition is checked such as whether the given counter has reached prescribed number (or) not.
- Suppose the condition has been reached, the condition falls through next sequence (or) comes out of the loop.
- In case if the counter is not reached it goes to the next sequence and repeats the execution again.
We can also say that loop is a fundamental idea that is commonly used in writing programs. There are two different types of loops in C++. They are as follows:
- Entry Controlled Loops: In this, the test condition is tested before the loop entering the body. So, here for an while loops are entry controlled loops.
- Exit Controlled Loops: In this, the test condition is executed (or) controlled at the end of the loop. So, here the loop body executes at least once either the condition is true (or) false. The do-while loop is an exit controlled loop. Finally, the loop body executes at least once even the test condition is true (or) false.
In C++ we are having different types of loops. They are as follows:
- for loop in C++
- while loop in C++
- d0-while loop in C++
- Infinite loop in C++
- Nested loop in C++