C - Relational and logical operators
Share
Relational operators:
When we previously talked about conditions in if statements, we said that conditions are built out of relational and logical operators. We described the six relational operators that C provides for comparing numbers:
== != > < >= <=
But suppose you need to know if the value of the variable x is both greater than zero and less than one? None of these relational operators by itself can answer that question. What you need is to combine two conditions:
(x > 0.0) and (y < 1.0)
C Logical Operators:
This is what logical operators can do for you. A logical operator combines one or two conditions into a single new condition. C provides three logical operators:
* ``&&'' (two ampersands) means and.
* ``||'' (two vertical bars) means or.
* ``!'' (an exclamation point) means not.
Let's look at an example. If you need to know if the value of the variable x is between zero and one, inclusive, you would write this:
if ((x >= 0.0) && (x <= 1.0))
...
if ((x == 0) || (y == 1 )
Comments:
|
Submitted By:
Prof: Software Engineer
Tech: C ,Cpp
|