for statement c++

The for statement in C++ is used to execute a block of code repeatedly. It consists of three parts: initialization, condition, and increment/decrement.

  1. Initialization: This part is used to initialize the loop control variable. It is executed only once at the beginning of the loop.

  2. Condition: This part evaluates a Boolean expression. If the condition is true, the loop continues; otherwise, the loop terminates.

  3. Increment/Decrement: This part is used to update the loop control variable. It is executed after each iteration of the loop.

The general syntax of the for statement in C++ is:

for (initialization; condition; increment/decrement) {
    // code to be executed
}

During each iteration, the initialization is executed first. Then, the condition is evaluated. If the condition is true, the code inside the loop is executed. After the code inside the loop is executed, the increment/decrement part is executed. This process continues until the condition becomes false.