22.02.2016 Views

C Programming Yellow Book

6019BjHWX

6019BjHWX

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Simple Data Processing<br />

Writing a Program<br />

==<br />

equals. If the left hand side and the right hand side are equal the expression has the<br />

value true. If they are not equal the value is false.<br />

4 == 5<br />

- would evaluate to false. Note that it is not particularly meaningful to compare<br />

floating point variables to see if they hold exactly the same values. Because of the fact<br />

that they are held to limited precision you might find that conditions fail when they<br />

should not for example the following equation:<br />

x = 3.0 * (1.0 / 3.0) ;<br />

- may well result in x containing 0.99999999, which would mean that:<br />

x == 1.0<br />

If you want to compare<br />

floating point values subtract<br />

them and see if the difference<br />

is very small.<br />

- would be false - even though mathematically the test should evaluate to true.<br />

!=<br />

not equal. The reverse of equals. If the operands are not equal the expression has the<br />

value true, if they are equal it has the value false. Again, this test is not advisable<br />

for use with floating point numbers.<br />

<<br />

less than. If the operand on the left is less than the one on the right the value of the<br />

expression is true. If the left hand operand is larger than or equal to the right hand one<br />

the expression gives false. It is quite valid to compare floating point numbers in this<br />

way.<br />

><br />

greater than. If the operand on the left is greater than the one on the right the result is<br />

true. If the operand on the left is less than or equal to the one on the right the result is<br />

false.<br />

=<br />

greater than or equal to. If the operand on the left is greater than or equal to the one<br />

on the right you get true, otherwise it is false.<br />

!<br />

not. This can be used to invert a particular value or expression, for example you can<br />

say !true, which is false, or you could say: !(x==y) - which means the same as<br />

(x!=y). You use not when you want to invert the sense of an expression.<br />

Combining Logical Operators<br />

Sometimes we want to combine logical expressions, to make more complicated<br />

choices, for example to test for a window width being valid we have to test that it is<br />

greater than the minimum and less than the maximum. C# provides additional<br />

operators to combine logical values:<br />

&&<br />

and. If the operands on each side of the && are true the result of the && is true. If one<br />

of them is false the result is false, for example<br />

C# <strong>Programming</strong> © Rob Miles 2015 36

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!