C++ for loop – C# Programming Example
C++

C++ for loop

Its format is:

 

and its main function is to repeat statement while condition remains true, like the while loop. But in addition, for provides places to specify an initialization instruction and an increase instruction. So this loop is specially designed to perform a repetitive action with a counter.

It works the following way:

  1. initialization is executed. Generally it is an initial value setting for a counter varible. This is executed only once.
  2. condition is checked, if it is true the loop continues, otherwise the loop finishes and statement is skipped.
  3. statement is executed. As usual, it can be either a single instruction or a block of instructions enclosed within curly brackets { }.
  4. finally, whatever is specified in the increase field is executed and the loop gets back to step 2.

Here is an example of countdown using a for loop.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: