Why does this not work?

Hi everyone, in the code below i have a conditional operator that when m is less than n should populate min with m and increment mCount. However it seems to populate min with n from the third operand!!!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

using namespace std;

int main()
{


    int m = 5, n = 10, min = 0;
    int mCount = 0, nCount = 0;

    min = (m < n ?  mCount++, m : nCount++, n);

    cout << min << endl;

    return 0;
}


When i remove nCount++ the code works as expected!
Could anyone enlighten me please?

Thanks B

int main()
{


int m = 5, n = 10, min = 0;
int mCount = 0, nCount = 0;

min = m < n ? (mCount++, m ): (nCount++, n);

cout << min << endl;

return 0;
}

Topic archived. No new replies allowed.