โครงสรางแบบเล%อกทา
ช.ดค5าส(6งโครงสร-างแบบเลอกท5าจะประกอบไปด-วยค5าส(6งด(งตCอไปน$D
• ค5าส(6ง if
• ค5าส(6ง if..else
• ค5าส(6ง if แบบซ-อน (nested if)
• ค5าส(6ง switch
รFปแบบของคาสง if
มร'ปแบบการใชคาสงดงน#
if (logical expression) {
statements
}
Flowchart แสดงล5าด(บการท5างานของคาสง if
ตวอย(างโปรแกรมท$ใช-คาส(6ง if
6 5
import javax.swing.JOptionPane;
public class SampleIf {
public void showDemo() {
String inputStr =
JOptionPane.showInputDialog("Enter value");
int score = Integer.parseInt(inputStr);
if (score >= 50) {
JOptionPane.showMessageDialog(null, "You pass");
// null to tell that there is no parent frame
}
}
}
ตวอย(างโปรแกรมท$ใช-คาส(6ง if..else
6 5
import javax.swing.JOptionPane;
public class SampleIfElse {
public void showDemo() {
String inputStr =
JOptionPane.showInputDialog("Enter value");
int score = Integer.parseInt(inputStr);
if (score >= 50) {
JOptionPane.showMessageDialog(null, "You pass");
} else {
JOptionPane.showMessageDialog(null, "You fail");
}
}
}
รFปแบบของคาสง if แบบซ-อน
เราสามารถใช-คาสง if ทมหลายเง%อนไขได ดงน#
if (logical expression 1) {
if (logical expression 2) {
statements 1
} else {
statements 2
}
} else {
statements 3
}
Flowchart ของคาสง if แบบซ-อน
รFปแบบของคาสง if แบบซ-อน
เราสามารถใช-คาสง if ทมหลายเง%อนไขได ดงน#
if (logical expression 1) {
statements 1
} else {
if (logical expression 2) {
statements 2
} else {
statements 3
}
}
รFปแบบของคาสง if..else if..else
เราสามารถใช-คาสง if ทมหลายเง%อนไขได ดงน#
if (logical expression 1) {
statements 1
} else if (logical expression 2) {
statements 2
} else {
statements 3
}
Flowchart ของคาสง if..else if..else
ตวอย(างโปรแกรมท$6ใชคาสง if..else if..else
import javax.swing.JOptionPane;
public class SampleIfElseIf {
public void showDemo() {
String inputStr = JOptionPane.showInputDialog("Enter value");
int x = Integer.parseInt(inputStr);
if (x == 1) {
JOptionPane.showMessageDialog(null, "Value is one");
} else if (x == 2) {
JOptionPane.showMessageDialog(null, "Value is two");
} else {
JOptionPane.showMessageDialog(null, "Other than 1 and 2 ");
}
}
}
รFปแบบของคาสง switch
มร'ปแบบการใชคาสงดงน#
switch (expression) {
case value 1: statements 1
break;
case value 2: statements 2
break;
: :
case value N: statements N
break;
default: statements N+1;
}
Flowchart แสดงลาดบการทางานของคาสง switch
คาสง switch
นพจนLตองมชน*ดขอม'ลเป.น char, byte, short หร%อ int เท(าน#น
ชน*ดขอม'ลของนพจนLและค(าท 1 ถ/ง N ตองเป.นชน*ดเดยวกน
ถาค(าของนพจนLตรงกบค(าใด จะทาชดคาสงของค(าน#น
ถาค(าของนพจนLไม(ตรงกบค(าใดเลย จะทาชดคาสงของ default
default จะมหร%อไม(มก0ได
6
ตวอย(างโปรแกรมท$ใชคาสง switch
import javax.swing.JOptionPane;
class SampleSwitch {
public void showDemo() {
String inputStr =
JOptionPane.showInputDialog("Enter value");
int x = Integer.parseInt(inputStr);
switch (x) {
case 1:
JOptionPane.showMessageDialog(null, "Value is one");
break;
case 2:
JOptionPane.showMessageDialog(null, "Value is two");
break;
default:
JOptionPane.showMessageDialog(null, "Other than 1 and 2 ");
}
}
}
โครงสรางแบบทาซ#า
เปNนค5าส(งท$6ใช-ในการส(งให-ช.ดค5าส(งใดๆท5าซD5าหลายคร(งตามเง6อนไขท$ระบ.
6 6 6 D 6
ประกอบด-วยค5าส(6ง
• while
• do..while
• for
รFปแบบของคาสง while
มร'ปแบบการใชคาสงดงน#
initial statements
while (logical expression) {
statements
update statements
}
Flowchart แสดงลาดบการทางานของคาสง while
6
ตวอย(างโปรแกรมท$ใชคาสง while
public class SampleWhile {
public void showDemo() {
int i = 1;
while(i <= 10) {
System.out.print(i+" ");
i++;
}
}
}
ผลล(พธLท$6ได-จากการร(นโปรแกรม
1 2 3 4 5 6 7 8 9 10
คาสง do..while
มร'ปแบบการใชคาสงดงน#
initial statements
do {
statements
update statements
} while (logical expression);
Flowchart ของคาสง do..while
6
ตวอย(างโปรแกรมท$ใชคาสง do..while
public class SampleDoWhile {
public void showDemo() {
int i = 1;
do {
System.out.print(i+" ");
i++;
} while (i <= 10);
}
}
ผลล(พธLทได-จากการร(นโปรแกรม
$6
1 2 3 4 5 6 7 8 9 10
รFปแบบของคาสง for
มร'ปแบบการใชคาสงดงน#
for (initial statements; logical expression;
update statements) {
statements
}
Flowchart แสดงล5าด(บการท5างานของคาสง for
คาสง for
คาสงกาหนดค(าเร*มตนและคาสงเปลยนแปลงค(า อาจมมากกว(าอย(างละ 1 คา
สง โดยจะใชเคร%องหมาย ',' ในการแยกคาสง
ตวอย(าง
for (int i=0,j=0; i<4; i++,j+=2) {
...
}
ขอบเขตของตวแปรทประกาศในคาสงกาหนดค(าจะใชไดเฉพาะภายในบล0อก
คาสง for เท(าน#น
6
ตวอย(างโปรแกรมท$ใชคาสง for
public class SampleFor {
public void showDemo() {
for (int i=1; i<=10; i++) {
System.out.print(i+" ");
}
}
}
ผลล(พธLท$6ได-จากการร(นโปรแกรม
1 2 3 4 5 6 7 8 9 10
ตวอย(างโปรแกรมแสดงขอบเขตของตวแปร
public class VariableScope {
public void showDemo() {
for (int i=1; i<10; i++) {
System.out.print(i+" ");
}
System.out.println("i = "+i); //illegal
}
}
0 comments
Post a comment