Why variable does not change after changing it within a for loop?

Viewed 303

This is my code

#include <iostream>

int main()
{
    int j = 1;
    for (int i=0, j=1; i<10; i++)
    {
        std::cout << j << std::endl;
        j++;
    }
    std::cout << j << std::endl;

    return 0;
}

This is my output:

2
3
4
5
6
7
8
9
10
11
1

I just want to know why the value of j does not change out

1 Answers

You have two variables j:

    int j = 1; // 1st "j" here
    for (int i=0, j=1; i<10; i++) // 2nd "j" here

You are modifying 2nd j in the loop and printing 1st j after the loop.

Related