Lesson 6

Loops in C++

Loops let you repeat a block of code multiple times. This is essential for counting, processing data, and building practical programs.

for Loop Example

Example
#include <iostream>
using namespace std;

int main()
{
    for (int i = 1; i <= 5; i++)
    {
        cout << i << endl;
    }

    return 0;
}

while Loop Example

Example
#include <iostream>
using namespace std;

int main()
{
    int i = 1;

    while (i <= 5)
    {
        cout << i << endl;
        i++;
    }

    return 0;
}
Book Tip:

This short lesson is part of a larger learning path. The full book expands each topic with more explanation, exercises, and projects.