About Lesson
Loops allow you to repeat a block of code multiple times until a condition is met. C++ supports three types of loops: for
, while
, and do-while
.
- for loop: Executes a block of code repeatedly for a fixed number of times.
for (int i = 0; i < 5; i++) {
cout << i << ” “;
}
- while loop: Executes a block of code repeatedly as long as a condition is true.
int i = 0;
while (i < 5) {
cout << i << ” “;
i++;
}
- do-while loop: Similar to a
while
loop, but the block of code is executed at least once before the condition is checked.
int i = 0;
do {
cout << i << ” “;
i++;
} while (i < 5);
Join the conversation