It's simple logic. Let's start from the beginning.
if (percentage<40)
cout<<"fail";
else ...
Let's stop at this point.
If percentage
is less than 40, this branch is taken. The End. Full Stop.
But then, under which conditions will we to get to this else
part, then?
... if (percentage>=40,percentage<=49)
cout<<"third division";
Well, we can only get here if percentage
is not less than 40, or percentage >= 40
. If percentage
was less than 40, the first if
statement will evaluate to true
. But if execution reaches the second if
statement, we must logically conclude that the first comparison must always be true, so it is completely meaningless. If this if
statement was written "correctly" (for some generic definition of "correctly":
if (percentage>=40 && percentage<=49)
cout<<"third division";
Then the first comparison will always evaluate to true, because the only way you can get here is if percentage>=40
, otherwise you wouldn't get here, so this is logically equivalent to, in this case, to:
if (percentage<=49)
cout<<"third division";
And with your mistake of using the comma operator, the first comparison also gets thrown away completely, and you still end up here. Which is why you're getting correct results. But for slightly wrong reasons.
The remaining if
statements end up with the same fate.