Boolean
expressions
A boolean expression is one that evaluates
to a result of either true or false.
There are several operators that return
a boolean result. These operators are generally from two categories:
-
comparison operators ?such as those for
less than '<' and greater than '>', and test for equality '=='
-
logical operators ?such
as those for logical AND '&&' and logical OR '||'
For example, we might want to define a
loop that continues while one number is less than 10, and also another
is greater than 100. If the numbers are stored in variables called
x
and y,
then in Java this would be written:
while(
(x < 10) && (y > 100) )
{
//... more
statements here
}
A question frequently asked is why the
symbols used for comparisons are so arcane. For example, it is possible
to envisage a programming language where the statement above could be written:
while
(x isLessThan 10) and (y isGreaterThan 100)
{
//... more
statements here
}
However, this would not make sense to
Java. Moreover, in Java the symbols &
and &&
are both read as `and',
but they are different kinds of 'and'!
The reason these symbols are used is
that the Java developers wanted to use the same symbols as C++, so that
C++ programmers could learn Java easily. The reason that C++ uses these
symbols is that the C++ developers wanted to use the same symbols as C,
so that C programmers could learn C++ easily.
Back
to top
.
RITSEC - Global Campus
Copyright © 1999 RITSEC- Middlesex
University. All rights reserved.
webmaster@globalcampus.com.eg
|