O'Reilly logo

Ivor Horton's Beginning Java™ 2, JDK™ 5th Edition by Ivor Horton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

3.1. Making Decisions

Making choices will be a fundamental element in all your programs. You need to be able to make decisions like, "If the user wants to enter more data, then read another value from the keyboard" or "If the bank balance is large, buy the car with the go-faster stripes, else renew the monthly bus ticket." Whatever decision you want to make, in programming terms it requires the ability to make comparisons between variables, constants, and the values of expressions and then execute one group of statements or another, depending on the result of a given comparison. Thus, the first step to understanding how you make decisions in a program is to look at how you make comparisons.

3.1.1. Making Comparisons

Java provides you with six relational operators for comparing two data values. The data values you are comparing can be variables, constants, or expressions with values drawn from Java's primitive data types—;byte, short, int, long, char, float or double.

Relational OperatorsDescription
>Produces the value true if the left operand is greater than the right operand, and false otherwise.
>=Produces the value true if the left operand is greater than or equal to the right operand, and false otherwise.
==Produces the value true if the left operand is equal to the right operand, and false otherwise.
!=Produces the value true if the left operand is not equal to the right operand, and false otherwise.
<=Produces the value true if the left operand is less than or equal to the right ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required