| == | equal to |
| != | not equal to |
| < | less than |
| > | greater than |
| <= | less than or equal to |
| >= | greater than or equal to |
| a == T
b == T |
a == T
b == F |
a == F
b == T |
a == F
b == F |
||
| !a | logical negation | F | F | T | T |
| a | b | logical-or | T | T | T | F |
| a & b | logical-and | T | F | F | F |
| a || b | conditional-or | T (*) | T (*) | T | F |
| a && b | conditional-and | T | F | F (*) | F (*) |
Example:
(x >= 0) ? x : -x
| value of x
before |
value of
expression |
value of x
after |
||
| Postfix increment | x++ | 1 | 1 | 2 |
| Postfix decrement | x-- | 1 | 1 | 0 |
| Prefix increment | ++x | 1 | 2 | 2 |
| Prefix decrement | --x | 1 | 0 | 0 |
Example:
temp = x*x + y*y;
d = Math.sqrt(temp);
x = 5;
y = 11;
z = x + y;
System.out.println(z);
Example:
{
double temp, d;
temp = x*x + y*y;
d = Math.sqrt(temp);
}
if (condition) {
statements
}
Example:
if (x % 2 == 0) {
System.out.println("The number
is even.");
}
if (condition) {
statement1;
} else {
statement2;
}
if (condition1) {
statement1;
} else if (condition2) {
statement2;
} else if (condition3) {
statement3;
} else {
statement4;
}
Example: Day2.java
(compare to Day.java)
switch (expression) {
case value1:
statement1;
break;
case value2:
statement2;
break;
case value3:
statement3;
break;
default:
statement4;
}
while (condition) {
statements;
}
When the body of a while statement, or of a for statement consists of a single statement, no braces are necessary.
Example:
int i = 1;
while (i <= 20) {
System.out.println(i);
i++;
}
Example:
int i = 20;
while (i >= 0) {
System.out.println(i);
i--;
}
for (initialization; condition; incrementation)
{
statements;
}
Example:
for (int i = 1; i <= 20; i++) {
System.out.println(i);
}
Example:
for (int i = 20; i >= 0; i--) {
System.out.println(i);
}
do {
statements;
} while (condition);
Example:
int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 20)
Example:
int i = 20;
do {
System.out.println(i);
i--;
} while (i >= 0);
Example:
int i = 0;
while (i <= 20) {
i++;
if (i % 5 == 0) break;
System.out.println(i);
}
Example:
int i = 0;
while (i <= 20) {
i++;
if (i % 5 == 0) continue;
System.out.println(i);
}
ClassName.methodName(parameters,
...)