For Loop in C++ | Topic 12 | VU Insider



When you know how many times you want to loop through a block of code.

There are 3 things in loop structure:
  1. initialization
  2. continuation/termination condition
  3. changing value to condition variable

C language uses for loop structure to implement these things.

Syntax:
"for (initialization condition; continuation condition;
incrementing condition) {
statement1;
statement2;
...
}" code-box


Example:
"for (int i = 0; i < 5; i++) {
cout << i << "\n";
}" code-box


Another example: To display table of 5 upto 10
"int counter;
for (counter=1; counter <= 10; counter = counter + 1)
{
cout<<"5 x "< }" code-box


The output will be; "5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
...
5 x 10 = 50"code-box

Post a Comment

Previous Post Next Post