control structure

45
Control Structure

Upload: nitigan-nakjuatong

Post on 29-May-2015

884 views

Category:

Technology


0 download

DESCRIPTION

Control Structure in JAVA

TRANSCRIPT

Page 1: Control structure

Control Structure

Page 2: Control structure

คำ��สั่��ง IFif, if-else, if-else if

Page 3: Control structure

รูปแบบก�รูทำ��ง�น

•if ( เง��อนไข ) {

•ก�รูทำ��ง�นภ�ยใต้�เง��อนไขเมื่��อเป�นจรู�ง•}

Page 4: Control structure

รูปแบบก�รูทำ��ง�น

•if ( เง��อนไข ) {

•ก�รูทำ��ง�นภ�ยใต้�เง��อนไขเมื่��อเป�นจรู�ง•}else {

•ก�รูทำ��ง�นภ�ยใต้�เง��อนไขเมื่��อเป�นเทำ�จ•}

Page 5: Control structure

รูปแบบก�รูทำ��ง�น•if ( เง��อนไข 1 ) {

•ก�รูทำ��ง�นภ�ยใต้� เง��อนไข 1 เมื่��อเป�นจรู�ง•}else if ( เง��อนไข 2 ) {

• ก�รูทำ��ง�นภ�ยใต้� เง��อนไข 2 เมื่��อเป�นจรู�ง•}else {

•...............................???.......................

•}

Page 6: Control structure

ให้�สั่รู��ง class ข !นมื่�ให้มื่" โดยให้�ชื่��อว่"� TestIF

Page 7: Control structure

•public class testIF{

•public static void main(String[] args ){

•}

•}

Page 8: Control structure

• ทำ'�สั่��ห้รู�บ import class ต้"�งๆ เข��มื่�เพื่��อใชื่�ง�น•public class testIF{

•public static void main(String[] args ){

•}

•}

Page 9: Control structure

• ทำ'�สั่��ห้รู�บ import class ต้"�งๆ เข��มื่�เพื่��อใชื่�ง�น

•public class testIF{

• พื่�!นทำ'�สั่��ห้รู�บปรูะก�ศต้�ว่แปรูทำ'�ใชื่�ง�นได�ทำ,กทำ'�ภ�ยใต้�class น'!•public static void main(String[] args ){

•}

•}

Page 10: Control structure

• ทำ'�สั่��ห้รู�บ import class ต้"�งๆ เข��มื่�เพื่��อใชื่�ง�น

•public class testIF{

• พื่�!นทำ'�สั่��ห้รู�บปรูะก�ศต้�ว่แปรูทำ'�ใชื่�ง�นได�ทำ,กทำ'�ภ�ยใต้� class น'!

• และย�งเป�นพื่�!นทำ'�สั่��ห้รู�บปรูะก�ศ method ต้"�งๆ•public static void main(String[] args ){

•}

•}

Page 11: Control structure

• ทำ'�สั่��ห้รู�บ import class ต้"�งๆ เข��มื่�เพื่��อใชื่�ง�น

•public class testIF{

• พื่�!นทำ'�สั่��ห้รู�บปรูะก�ศต้�ว่แปรูทำ'�ใชื่�ง�นได�ทำ,กทำ'�ภ�ยใต้� class น'!

• และย�งเป�นพื่�!นทำ'�สั่��ห้รู�บปรูะก�ศ method ต้"�งๆ

•public static void main(String[] args ){

• เป�นพื่�!นทำ'�ห้ล�กทำ'�คำว่บคำ,มื่ก�รูทำ��ง�นทำ�!ง class

•}

•}

Page 12: Control structure

ต้�องก�รูรู�บข�อมื่ล 1 ต้�ว่จ�กkeyboard เป�นชื่น�ด int

Enter Number1: ....

Page 13: Control structure

•import java.util.Scanner;•public class testIF{

•public static void main(String[] args ){•Scanner scan = new Scanner(System.in);

•}•}

Page 14: Control structure

•import java.util.Scanner;•public class testIF{

•public static void main(String[] args ){•Scanner scan = new Scanner(System.in);•int num1;•System.out.println(“Enter number1: ”);•num1 = scan.nextInt();

•}•}

Page 15: Control structure

ต้�องก�รูให้�ต้�ว่เลขทำ'�รู �บเข��มื่�น�!นแสั่ดงออกมื่�

Enter Number1: 4Number1 is : 4

Page 16: Control structure

•import java.util.Scanner;•public class testIF{

•public static void main(String[] args ){•Scanner scan = new Scanner(System.in);•int num1;•System.out.println(“Enter number1: ”);•num1 = scan.nextInt();•System.out.println(“Number1 is :” + num1);

•}•}

Page 17: Control structure

ก��ห้นดเง��อนไข ถ้��ต้�ว่เลขทำ'�รู �บเข��มื่�มื่�กกว่"� 10 ให้�น��ต้�ว่เลขน�!นบว่ก ก�น พื่รู�อมื่ทำ�!งแสั่ดงผลของก�รูบว่กก�นออกมื่�

Page 18: Control structure

if(>10){if(>30){ห้�รูก�น

}else{บว่กก�น

}}

Page 19: Control structure

•import java.util.Scanner;

•public class testIF{

•public static void main(String[] args ){

•Scanner scan = new Scanner(System.in);

•int num1;

•System.out.println(“Enter number1: ”);

•num1 = scan.nextInt();

•System.out.println(“Number1 is :” + num1);

•if(num1>10){•int num2 = num1+num1;•System.out.println(“num1+num1=”+num2);

•}•}

•}

Page 20: Control structure

ก��ห้นดเง��อนไข ถ้��ต้�ว่เลขทำ'�รู �บเข��มื่�น�อยกว่"� 10 ให้�น��ต้�ว่เลขน�!นคำณ ก�น พื่รู�อมื่ทำ�!งแสั่ดงผลของก�รูคำณก�นออกมื่�

Page 21: Control structure

End If

Page 22: Control structure

คำ��สั่��ง Switchswitch-case-default

Page 23: Control structure

รูปแบบก�รูทำ��ง�น•switch ( ต้�ว่แปรู ) {

•case คำ"�ทำ'�1 : ...• break;

•case คำ"�ทำ'�2 : ...• break;

•...•default : ...

• break;•}

Page 24: Control structure

ให้�สั่รู��ง class ข !นมื่�ให้มื่" โดยให้�ชื่��อว่"� TestSwitch

Page 25: Control structure

ให้�รู�บคำ"�ต้�ว่เลขเข��มื่� 2 คำ"� พื่รู�อมื่ทำ�!งแสั่ดงต้�ว่เลขทำ�!ง 2

ต้�ว่น�!นออกมื่�Enter num1 : 4Enter num2 : 5Num1 is : 4Num2 is : 5

Page 26: Control structure

ในก�รูรู�บคำ"�ทำ'�เป�นต้�ว่เลขจะใชื่�scan.nextInt();

Page 27: Control structure

ให้�เพื่��มื่ก�รูรู�บคำ"�เคำรู��องห้มื่�ย + - * / เข��มื่�เก�บไว่�ในต้�ว่แปรูชื่น�ดStringEnter Operator : +Operator is : +

Page 28: Control structure

ในก�รูรู�บคำ"�ทำ'�เป�นต้�ว่ห้น�งสั่�อจะใชื่�scan.next();

Page 29: Control structure

น��เคำรู��องห้มื่�ยน�!นไปใชื่�ใน case

operator = msg.charAt(0);

String ทำ'�รู �บเข��มื่�

ต้�ว่แปรูทำ'�จะน��ไปใชื่� ใน case

Page 30: Control structure

น�� switch-case มื่�ใชื่�โดย ให้�สั่รู��งเง��อนไขของ case ต้"�งๆ เชื่"น

ถ้�� + ก�น��ต้�ว่เลข 2 ต้�ว่มื่�บว่กก�น และแสั่ดงผลของก�รูบว่กก�น

ออกมื่�

Page 31: Control structure

คำ��สั่��ง Whilewhile

Page 32: Control structure

รูปแบบก�รูทำ��ง�น

•while ( เง��อนไข ) {•ก�รูทำ��ง�นภ�ยใต้�เง��อนไขทำ'�เป�นจรู�ง

•}

Page 33: Control structure

ให้�สั่รู��ง class ข !นมื่�ให้มื่" โดยให้�ชื่��อว่"� TestWhile

Page 34: Control structure

ก��ห้นดต้�ว่แปรูมื่�ข !นมื่�1 ต้�ว่ ให้�คำ"�เรู��มื่ ต้�นเป�น 0

ให้�รู�บคำ"�ต้�ว่เลขเข��มื่� 1 คำ"�เก�บไว่�ในต้�ว่แปรูต้�ว่น�!นพื่รู�อมื่ทำ�!งให้�แสั่ดงคำ"�น�!นออกมื่�

Enter num1 : 4Num1 is : 4

Page 35: Control structure

น��เง��อนไข while มื่�ใชื่�เพื่��อให้� สั่�มื่�รูถ้รู�บคำ"�ต้�ว่เลขได�ห้ล�ยๆ คำรู�!ง

ภ�ยใต้�เง��อนไขทำ'�ว่"� ถ้��คำ"�ทำ'�รู �บเข��มื่�น�อยกว่"� 0 เมื่��อไห้รู"

ให้�จบก�รูทำ��ง�นทำ�นทำ'

Page 36: Control structure

ถ้��ต้�ว่เลขทำ'�รู �บเข��มื่�มื่�กกว่"� 0 ให้�น��ต้�ว่เลขแต้"ละต้�ว่ทำ'�รู �บเข��มื่�น�!นบว่กก�น

และแสั่ดงผลออกมื่�เรู��อยๆEnter num1 : 4Num1 is : 4Total is : 4Enter num2 : 5Num2 is : 5Total is : 9

Page 37: Control structure

คำ��สั่��ง Do Whiledo while

Page 38: Control structure

รูปแบบก�รูทำ��ง�น

•do {•ก�รูทำ��ง�นภ�ยใต้�เง��อนไขทำ'�เป�นจรู�ง

•} while ( เง��อนไข );

Page 39: Control structure

คำ��สั่��ง Forfor

Page 40: Control structure

รูปแบบก�รูทำ��ง�น•for (ก��ห้นดคำ"�เรู��มื่ต้�น ; เง��อนไข ; ก�รูเปล'�ยนแปลงคำ"�) {

•ก�รูทำ��ง�นภ�ยใต้�เง��อนไขทำ'�เป�นจรู�ง•}

•for (int i=1 ; i<10 ; i++)•{

•System.out.println(“i=” + i );•}

Page 41: Control structure

ให้�สั่รู��ง class ข !นมื่�ให้มื่" โดยให้�ชื่��อว่"� TestFor

Page 42: Control structure

ให้�รู�บคำ"�ต้�ว่เลขเข��มื่� 1 คำ"� และแสั่ดงต้�ว่เลขน�!นออกมื่�

Enter num1 : 5Num1 is : 5

Page 43: Control structure

ให้�ใชื่�เง��อนไข for ในก�รูแสั่ดงต้�ว่เลขออกมื่�ต้�!งแต้"ต้�ว่เลขทำ'�รู �บเข��มื่�จนถ้ ง

ต้�ว่เลข 0 โดยต้�ว่เลขแต้"ละต้�ว่จะห้"�ง ก�น 3 เคำ�ะ

Enter num1 : 5Num1 is : 5Number : 5 4 3 2 1 0

Page 44: Control structure

ว่�ธี'ทำ�� ข�!นทำ'� 1

ห้� Input ทำ'�โจทำย2ต้�องก�รูให้�เรู�ใสั่"คำ"�เข��ไป

ทำ��อย"�งไรูให้�ได� Output น�!นมื่�จ�ก Input ทำ'�มื่'

แสั่ดง Output ทำ'�โจทำย2ต้�องก�รู

Input

Output

Process

Page 45: Control structure

ว่�ธี'ทำ�� ข�!นทำ'� 2 ทำ��ก�รูสั่รู��ง class ให้มื่"

คำ"อยๆ ปรูะก�ศต้�ว่แปรูชื่น�ดต้"�งๆ เพื่��อรู�บคำ"�ทำ'�เป�นinput ทำ'�จะน��ไปใชื่�ทำ�!งห้มื่ด

เล�อกเง��อนไขต้"�งๆ มื่�ใชื่�ให้�เห้มื่�ะสั่มื่สั่��ห้รู�บก�รูกรูะ ทำ��อะไรูซั�กอย"�งน งเพื่��อให้�ได�ผลล�พื่ธี2ต้�มื่ทำ'�ต้�องก�รู

( if, if-else, while, do-while, for, switch-case )

แสั่ดงผลล�พื่ธี2ออกมื่�ในรูปแบบทำ'�ต้�องก�รู