The code i wrote in the beginning works fine. I just didnt understand why we dont need the second comparisons. |
Look at this snippet:
1 2 3 4 5 6 7 8
|
if (top > 90)
{
cout <<"Congratz you passed with AA" << endl;
}
else if (top > 80 && top < 90)
|
In that else if() you already know that the value is less than 90 because of the preceding if() statement. You will only reach that else if() if the value is less than 90.
By the way the above code fails if the number is exactly equal to 90, because you never actually test if the number is exactly 90. But if you remove the second comparison from the else if() it will actually test 90.
The if() statement values of 91, 92, 93, ... would be valid (remember greater than).
The else if() (as written) tests for 81, 82, 83, ..., 88, and 89 ( greater than 80, and less than 90).
But when you only test with one comparison as suggested. Get the following:
The if() statement values of 91, 92, 93, ... would be valid (remember greater than).
The else if() (as suggested) tests for 81, 82, 83, ..., 88, 89 and 90 ( greater than 80).