Zone Of Makos

Menu icon

Entry Controlled Loops in C++

In the last lesson, we covered the concept of iterative statements in C++. Iterative statements, also known as loops, allow us to execute a block of code repeatedly as long as a certain condition is true. In this lesson, we will focus on entry controlled loops, which are loops where the condition is checked before entering the loop body.

While Loop

The while loop is a common entry controlled loop in C++. It repeatedly executes a block of code as long as a given condition is true. The condition is checked before each iteration. Here's an example of a while loop:


int count = 0;
while (count < 5) {
    cout << "Count: " << count << endl;
    count++;
}

For Loop

The for loop is another entry controlled loop in C++. It provides a more compact way to express loops with initialization, condition, and increment/decrement statements in a single line. Here's an example of a for loop:


for (int i = 0; i < 5; i++) {
    cout << "i: " << i << endl;
}

Conclusion

In this lesson, we explored entry controlled loops in C++, specifically the while and for loops. These loops allow us to repeat a block of code as long as a given condition is true. In the next lesson, we will continue our exploration of loops and cover exit controlled loops, where the condition is checked after executing the loop body. Happy coding!