Return To CS 110 Home

Colour Theme   Font Size Options
 
   
   
   
Nested Loop

Nested Loops


Video Summary: https://youtu.be/FHpbU0vyPaU


The body of a loop can contain any type of statement including another loop such as a while loop, a do-while loop, or a for loop. A loop inside another loop is called a nested loop.

The following program counts the number of characters on each line in a file. We know that the I/O manipulator endl forces the next character sent to the output stream to begin a new line. How can we recognize a new line in the input stream? A new line begins following the symbol '\n'. We must be sure to use function .get() defined in iostream, not the extraction operator, to input each character. Otherwise, we will skip all blank spaces including line characters.