Hit enter after type your search item

C++ For Loop: Explained with Examples

What is for loop in C++

The for loop is a way to iterate a certain block of code a given number of times.

You may exit a for entirely or may code to exit only one or more iterations by using specified statements – explained in the sections coming below.

How to write for loop in C++ – the Syntax

The general syntax of writing a for loop in C++ is:

In the above syntax:
  • initialization: e.g. x=1. This is an initialization expression i.e. the loop counter is initialized here. This part executes only once.
  • Condition expression: In this part of the for loop, the condition is given. If it evaluates as true, the code block inside the curly braces is executed. E.g. x=10.
  • Update expression: After executing the statements in the above part, the control comes to this part. There, the value of the variable can be incremented/decremented. E.g. x++


The for loop is used when you know how many times a block of code should be executed. Otherwise, you may use a while loop.

A simple example of using a for loop

In the example below, we will simply execute the for loop ten times. In each iteration, we will display the value of a variable. The target is to display values from 1 to 10. Have a look at the code below and its output:


Reversing the display in for loop

In this example, we will use the decrement operator in the third part of for loop. The value of the variable is initiated as 10 and the condition is to execute the loop till the value is greater than or equal to 1.

In each iteration, the value of the variable x is decremented by 1. Inside the curly braces, we will execute the current value of the variable.

The task is to display the number from 10 to 1 in this example.


Display value with the gap of two

This example displays the value of a variable with a gap of 2. In each iteration, we will increase the value by two in the third expression of for loop:



Displaying text in the for loop

This example displays the text as well as the current value of the variable in the for loop.


Exiting an iteration by using the continue statement in for loop

The task of this example is to display the numbers from 1 through 10 and omit the loop to display the number 5. For this, we have to exit the loop only once as the variable value reaches five. Have a look:


As you saw in the above output, number 5 is not displayed. This is accomplished by using the continue statement. So, the continue statement is used to break the iteration once while the for loop keeps on iterating until the given expression is true in the third part of for loop.

Using the break statement example to exit the loop

For exiting the for loop completely, you may use the break statement as the given condition is met. In this example, we will exit the for loop as the value of the variable reaches 6 and the remaining numbers will not display. See the code and output below:


As you saw in the above output, the variable is displayed till value 5. In the next iteration, as the value of the x is 6, the if condition is evaluated as true. There we used the break statement and rather than displaying 6, 7, 8, 9, and 10, the loop exited.

Iterating through array example

Though this is an advanced level example for the beginners – just for the taste, we are using a string array and for loop. In the array, we stored the names of five fruits.

By using for loop, we will iterate through string array elements and display it’s values:


The example of range-based for loop

For displaying array elements in the above example, you may also use a range-based for loop. This is also a separate topic, but again for your taste here is an example.



This div height required for enabling the sticky sidebar