Main menu


C ++ for loop  - Learn CPP, C++ provides a number of iterations (loops) that are used to iterate parts of a program more than once as long as the test expression is true or else it stops iterating. There are three types of loops in C++: for, while, and do while In this article, we will talk about the for clause.

C ++ for loop  - Learn CPP

Define for the iterator statement

The for the clause is one of the repetition clauses available in the C++ language and the most widely used for its simplicity and ease, as many programmers prefer to use it most of the time because the variable used in the loop is simply defined, and the condition and counter are specified in only one line, which makes it very easy for the programmer. The for loop has many functions, including entering values, searching for values, etc., and is frequently used in arrays.

for condition clause arguments

The for keyword has three arguments separated by semicolons, so that

  • The first argument: It is a variable in which we put the value with which the counter will start, for example, to repeat something 10 times, for example, the counter starts with 0 and ends with 10.
  • The second argument is the continuation condition (condition): If the condition is false, the loop will be exited, and if it is true, the commands will be re-executed, for example, if the counter is less than or equal to 10 Continue, and the repetition remains until the condition is satisfied and becomes greater than 10
  • The third mediator: Is the influencer (usually the amount of increase or decrease in the counter).

An example of this is if we want to repeat a certain thing three times

for(int i=1; I<=3;i++)





The i here is a counter of type int with its initial value = 1, then the continuation condition that i is less than or equal to the number 3, and then the increment over the counter by 1. The sentences in parentheses are the operations we want to repeat.

What is the difference between for loop, while loop, and do while loop?

The while loop and the do while loop usually do not know how many times the loop will repeat, but in a for loop, the number of times the loop has been executed is usually stated at the beginning of the loop.

You can read about if else statement here in this article.