เครื่องหมายที่ใช้ในการเปรียบเทียบ
เท่ากับ(less than or equal to)
> มากกว่า(greater than)
>= มากกว่าหรือเท่ากับ(reater than or equal to)
== เท่ากับ(equal to)
!= ไม่เท่ากับ(not equal to)
The & and | Operators
&& : conditional AND operator
& : unconditional AND operator
|| : conditional OR operator
| : unconditional OR operator
exp1 && exp2
(1 < x) && (x < 100)
(1 < x) & (x < 100)
Caution
ห้ามใส่เครื่องหมาย ; ด้านหลังประโยด if statement.
if (radius >= 0);
{
area = radius*radius*PI;
System.out.println("The area for the circle of radius " + radius + " is " + area);
}
switch Statements
switch (status) {
case 0: compute taxes for single filers;
break;
case 1: compute taxes for married file jointly;
break;
case 2: compute taxes for married file separately;
break;
case 3: compute taxes for head of household;
break;
default: System.out.println("Errors: invalid status");
System.exit(0);
}
Conditional Operator
if (x > 0)
y = 1
else
y = -1;
สามารถเขียนได้อีกรูปแบบหนึ่ง ดังนี้
y = (x > 0) ? 1 : -1;
รูปแบบ
(booleanExpression) ? expression1 : expression2
