How to find minimum and maximum of 3 number in c++?
Answers
Answered by
0
You can't use if( a < b < c ), because this won't do what you think it will. The short answer is that you need to make two separate comparisons and join them together with "and," which is spelled "&&" in C. It looks like this: if( a < b && b < c ). This reads "if a is less than b and b is less than c."
That's enough to get you going, but it might be worth going into a little more detail on if statements. Intuitively, it seems like you should put some kind of comparison inside the if statement, but this is actually not how they work. You can even put a single variable in an if statement, and it will do something: if( a ) is valid code, and it means "if a is not zero."
Now consider the statement "a < b". This looks like a comparison, but in C it is actually more like an expression that results in a value. Specifically, if a is less than b, the result is 1, and if it isn't, the result is zero. If you wrote:
d = a < b;
Then d would become equal to either 1 or 0, depending on the values of a and b. Give it a try!
That's enough to get you going, but it might be worth going into a little more detail on if statements. Intuitively, it seems like you should put some kind of comparison inside the if statement, but this is actually not how they work. You can even put a single variable in an if statement, and it will do something: if( a ) is valid code, and it means "if a is not zero."
Now consider the statement "a < b". This looks like a comparison, but in C it is actually more like an expression that results in a value. Specifically, if a is less than b, the result is 1, and if it isn't, the result is zero. If you wrote:
d = a < b;
Then d would become equal to either 1 or 0, depending on the values of a and b. Give it a try!
Similar questions