|
Computer Science (CSCI)
Department |
|||
|
|||
Control Structures and Relational Operators
(Example code and figures from Computing Concepts with Java Essentials,
3rd edition, by Cay Horstmann.
Visit www.wiley.com/college/horstmann
for information on the current edition of this excellent text.)
if statement
if (amount <= balance) |

if (amount <= balance) |
if (amount <= balance) |
balance = balance - amount; |
if (balance >= amount) balance = balance - amount; |
{
|
|
|
Description |
|
|
Greater than |
|
|
Greater than or equal |
|
|
Less than |
|
|
Less than or equal |
|
|
Equal |
|
|
Not equal |
Equality Testing vs. Assignment
if (x == 0) . . // if x equals zero |
x = 0; // assign 0 to x |
double r = Math.sqrt(2); |
if (input == "Y") // compares the references, not the strings |
if (input.equals("Y"))
|
if (input.equalsIgnoreCase("Y"))
|
Rectangle cerealBox = new Rectangle(5, 10, 20, 30); Rectangle oatmealBox = new Rectangle(5, 10, 20, 30); Rectangle r = cerealBox; |
if (condition1) |
if (condition1) |
If your filing status is Single
| If the taxable income is over |
But not over |
The tax is |
Of the amount over |
| $0 |
$21,450 |
15% |
$0 |
| $21,450 |
$51,900 |
$3,217.50 + 28% |
$21,450 |
| $51,900 |
$11,743.50 + 31% |
$51,900 |
If your filing status is Married
| If the taxable income is over |
But not over |
The tax is |
Of the amount over |
| $0 |
$35,800 |
15% |
$0 |
| $35,800 |
$86,500 |
$5,370.00 + 28% |
$35,800 |
| $86,500 |
$19,566.00 + 31% |
$86,500 |
public boolean isOverdrawn() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
boolean married;
married = input.equals("M");
if (!married) ...
|
|
Exercise - play with these in TextPad
|