The for double loop thing in c++

  • 2020-04-01 23:31:02
  • OfStack

Case 1: as follows, we will find that the output of n is 100, although the identifier of the two layers of the loop is I, but the two do the jurisdiction of the scope is different, the specific situation is unknown ~~~ ask the god to explain


int main(int argc,char* argv[])
{
    int n=0;
    int mx;
    for (int i=0;i<10;i++)
    {
        for (int i=0;i<10;i++)
        {
            n++;
        }
    }
    cout<<n<<endl;
}

Case 2: as follows, n outputs 10 when entering the inner loop for the first time, n exits with 10, thus directly ending the first loop.

int main(int argc,char* argv[])
{
    int n=0;
    int mx;
    for (int i=0;i<10;i++)
    {
        for (i=0;i<10;i++)
        {
            n++;
        }
    }
    cout<<n<<endl;
}


Related articles: