C++ For loop

tutorial and example
tutorial and example

C++ loop Statement

loop statement allows us to repeat execution of statement or group of statements multiple times. The statement(s) repeat execution within loop until condition of loop became false.

There are three types of loops in C++ programming language:

  1. for loop
  2. while loop
  3. do…while loop

C++ for Loop Statement

The for loop is an iterative control statement. It executes loop’s statement(s) several times until the condition of for loop become false.

Syntax

Working Process of for loop

  1. The initialize expression statement is executed only once and initialize variable expr.
  2. Then condition expression checks the condition applied, if it satisfy (true), statement(s) inside for loop executes otherwise execution exit from for loop.
  3. After statement(s) execution within for loop block, it update the variable expr and repeat step 2.

C++ for Loop Example

This example print counting from 1 to 10.

Output:

Infinite for Loop

Infinite for loop repeats the loop block infinite times. This happens when condition statement of for loop always true (never false).

Infinite loop in C++ programming language is terminated using shortcut keys Ctrl + C.

Syntax

Infinite for loop Example

Output:

Print counting from 1 to infinite.