.:: ยินดีต้อนรับเข้าสู่เว็บไซต์ ::.

 

 

 

 

05540210 Java Programming Language
ภาษาจาวา
สังกัด บริหารธุรกิจ, บริหารธุรกิจ
หน่วยกิต 3 (3-0-3)
  อาจารย์ สมศักดิ์ บุตรสาคร

เนื้อหาที่เกี่ยวข้อง

 

 

 

ประโยคควบคุม (Control Statement)

 

การเปรียบเทียบระหว่างค่า 2 ค่า, อาทิเช่น i มากกว่า j หรือไม่. ในภาษาจาวามี Operator ที่ใช้ในการเปรียบเทียบ ซึ่งผลลัพธ์จากการเปรียบเทียบจะมีค่าเป็น Boolean value ซึ่งจะมีค่าเพียง true หรือ false.
เครื่องหมายที่ใช้ในการเปรียบเทียบ
เท่ากับ(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