CATEGORII DOCUMENTE  


Asp  Autocad  C  Dot net  Excel  Fox pro  Html  Java 
Linux  Mathcad  Photoshop  Php  Sql  Visual studio  Windows  Xml 
This chapter has already shown how C makes no distinction between ‘logical’
and other values. The relational operators all give a result of
or
for false and true, respectively. Whenever the control of flow statements demand it, an expression is
evaluated to determine what to do next. A
means ‘don't do it’; anything else means ‘do’. It means
that the fragments below are all quite reasonable.
No experienced C programmer would be surprised by any of them. The second of
them, while (a)
, is a
common abbreviation for while (a != 0)
, as you should be able to
work out.
What we need now is a way of writing more complicated expressions involving
these logical true and false values. So far, it has to be done like this, when
we wanted to say if(
a<b AND c<d)
It will not be a source of great amazement to find that there is a way of expressing such a statement.
There are three operators involved in this sort of operation: the logical AND &&
, the logical OR 
and the NOT !
. The last is unary,
the other two are binary. All of them take expressions as their operands and
give as results either
or
. The &&
gives
only when both
of its operands are nonzero. The
gives
only when both operands are zero. The !
gives
if its operand is nonzero and vice
versa. Easy really. The results are of type int
for all three.
Do not confuse &
and
(the bitwise operators) with their
logical counterparts. They are not the same.
One special feature of the logical operators, found in very few of the other
operators, is their effect on the sequence of evaluation of an expression. They
evaluate left to right (after precedence is taken into account) and every
logical expression ceases evaluation as soon as the overall result can be
determined. For example, a sequence of
s
can stop as soon as one operand is found to be nonzero. This next fragment
guarantees never to divide by zero.
In either version b/a
will only be evaluated if a is nonzero. If a were
zero, the overall result would already have been decided, so the evaluation
must stop to conform with C's rules for the logical
operators.
The unary NOT is simple. It isn't all that common to see it in use largely because most expresssions can be rearranged to do without it. The examples show how.
if (!a)Each of the examples and the alternatives serve to show ways of avoiding (or
at least doing without) the !
operator. In fact, it's
most useful as an aid to readability. If the problem that you are solving has a
natural logical relationship inherent in it—say the (b*b4*a*c) > 0
found in quadratic
equation solving—then it probably reads better if you write if(
!((b*b4*a*c) > 0))
than if( (b*b4*a*c) <= 0)
—but it's up to
you. Pick the one that feels right.
Most expressions using these logical operators work out just about right in terms of the precedence rules, but you can get a few nasty surprises. If you look back to the precedence tables, you will find that there are some operators with lower precedence than the logical ones. In particular, this is a very common mistake:
if(a&b == c){What happens is that b
is compared for equality with c
,
then the
or
result is anded with a!
Some distinctly unexpected behaviour
has been caused by that sort of error.
Politica de confidentialitate 
Vizualizari: 497
Importanta:
Termeni si conditii de utilizare  Contact
© SCRIGROUP 2022 . All rights reserved
Distribuie URL
Adauga cod HTML in site