vendredi 23 avril 2021

C++ variable list Initialization

So here are piece of 2 code

#1

int i=0, j=0;

for (i=0,j=0; i<=5; i++, j++)
    {
        cout<<i<<"  "<<j<<endl;
    }

#2

int i{0}, j{0};

for (i{0},j{0}; i<=5; i++, j++)
    {
        cout<<i<<"  "<<j<<endl;
    }

Build Messages

1st code allows me to replace the value inside for loop that was stored in i and j. But in the code #2 (in which I used c++11 list initialization) it shows some errors. But this works :

int i{0}, j{0};

for (i,j; i<=5; i++, j++)
    {
        cout<<i<<"  "<<j<<endl;
    }

Why can't I replace them inside loop? What is the correct way for #2 and how should I Proceed?

Aucun commentaire:

Enregistrer un commentaire