dimanche 18 août 2019

Query regarding post increment and pre increment operator

If I have the following code:

int n=5;
if (n++>=6){
    cout<<"Yay";
    }
else cout<<"No";

And I compare it with this:

int n=5;
if (++n>=6){
    cout<<"Yay";
    }
else cout<<"No";

Why is the output different in both cases? Isn't n being incremented in both cases(either post/pre-increment) before the inequality is encountered?

When does the post-increment take effect-- is it just before the next statement is encountered? If this is the case, will n++ value remain 5 until the whole inequality is evaluated?

Another question I have is that will increment in the if condition affect the value of the variable outside? If I have the first code(post-increment) and I print n, will n return 6?

Aucun commentaire:

Enregistrer un commentaire