if(condition){
statement .... A
}else{
statement ... B
}
condition หมายถึง เงื่อนไขสำหรับใช้ในการตรวจสอบ
statement หมายถึง คำสั่งที่ดำเนินการ
สมมติว่า
ถ้าเงื่อนไขเป็นจริง ให้ทำ statement A
ถ้าเงื่อนไขไม่เป็นจริง ให้ทำ statement ฺ
คำสั่ง nest ... if
if(condition1) statement 1;
else if(condition2) statement 2;
else statement 3;
condition หมายถึง เงื่อนไขสำหรับใช้ในการตรวจสอบ
statement หมายถึง คำสั่งที่ดำเนินการ
คำสั่ง switch ... case
switch(variable){
case constant1 : statement1;
break;
case constant2 : statement2;
break;
default : statement3;
}
variable หมายถึง ตัวแปรที่เก็บค่าข้อมูล
constant หมายถึง ค่าคงที่
break หมายถึง ออกจากเงื่อนไข
default มีค่าเท่ากับ else