Loops are used to repeat a block of statements at specific times. A loop statement will execute until the given expression evaluates to false.
C++ programming uses 3 types of loops:
A for loop is a control structure that allows a sequence of instructions to be performed for a predetermined number of iterations. It's an entry-controlled loop. It has three sections: index declaration, condition (boolean expression), and update statement. For loops are useful when you know the precise number of iterations.
In C++, a nested for loop is one loop inside another, with the inner loop completing all of its iterations for each iteration of the outer loop.
An infinite for loop is formed if the for loop condition remains true or if the programmer fails to apply the test condition/terminating statement within the for loop statement.
This is a new type of loop introduced in C++11. It is only used to iterate across elements in an array or vector.
In C++, the while loop executes a statement or block of statements until the given expression evaluates to false. This is an entry-controlled loop. In C++, the while loop is used when we do not know how many iterations will be performed.
In C++, a nested while loop is one while loop inside another, with the inner loop going through all of its iterations for each iteration of the outer loop.
In C++, an infinite while loop continues indefinitely without a termination condition. It is commonly generated by setting the loop's condition always to be true.
It's an exit-controlled loop. It prints the results at least once before checking the condition. It is similar to the while loop but evaluates the expression after executing the code block. It's useful for one-time executions that depend on defined conditions.
In C++, a nested do...while loop consists of one do...while loop inside another, with the inner loop completing all of its iterations for each iteration of the outer loop. This structure is excellent for situations that require repeated actions under varying conditions.
In C++, an infinite do...while loop continues endlessly without a termination condition, which often comes by setting the loop's condition always to be true.