Menu

C++ TUTORIALS - C++ Loop Types

C++ Loop Types

ADVERTISEMENTS

Loop TypeDescription
while loopRepeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.
for loopExecute a sequence of statements multiple times and abbreviates the code that manages the loop variable.
do...while loopLike a while statement, except that it tests the condition at the end of the loop body
nested loopsYou can use one or more loop inside any another while, for or do..while loop.

ADVERTISEMENTS

Loop Control Statements:

Control StatementDescription
break statementTerminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch.
continue statementCauses the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.
goto statementTransfers control to the labeled statement. Though it is not advised to use goto statement in your program.

ADVERTISEMENTS

The Infinite Loop:

#include <iostream>
using namespace std;
 
int main ()
{

   for( ; ; )
   {
      printf("This loop will run forever.\n");
   }

   return 0;
}