For Loop
The for loop is more complex than the other loops and consists of three parts. Structure of for loop:
for (counter; exit condition; loop expression)
{
// block of instruction
do_something;
}
- Counter;
- Exit condition;
- Loop expression.
main.cpp
#include <iostream> int main() { for (int counter = 0; counter <= 5; counter++) { std::cout << counter << std::endl; } }
int counter = 0
: iteration counter;counter++
: For each iteration, 1 will be added to thecounter
variable to mark the passage of the loop;counter <= 5
: loop termination condition. The loop will continue if thecounter
variable is less than or equal to 5.
Everything was clear?
Thanks for your feedback!
SectionΒ 4. ChapterΒ 5