LOGO Control Statement 6112563 Nerissa Onkhum 1 public
LOGO ประโยคควบคม (Control Statement( 6/11/2563 Nerissa Onkhum 1
การทำงานแบบเรยงลำดบ public class Shopping { public static void main(String[] args) } int cash = 500; int book. Price = 180; int food. Price = 20; int soda. Price = 7; cash -= book. Price; cash -= food. Price; cash -= soda. Price; System. out. println("Cash = " + cash; ( { { 5
ตวอยางโปรแกรมทใชคำส ง if public class Sample. If { public static void main(String args[]) { int score = Integer. parse. Int(args[0]); if (score >= 50) { System. out. println("You pass"); } } }
ตวอยางการตดสนใจซอหน งสอ public class If { public static void main(String[] args) { int cash ; 200 = int book. Price = 180; if )cash >= book. Price) { cash -= book. Price ; System. out. println("Buy book; (". } System. out. println("Cash = " + cash; ( } } 6/11/2563 10
������ public class If. And { public static void main(String[] args) { int cash = 220; int price = 100; if (cash >= price && price < 150) System. out. println("Buy book. "); } } 6/11/2563 14
ตวอยางโปรแกรมทใชคำส ง if. . else public class Sample. If. Else { public static void main(String args[]) { int score = Integer. parse. Int(args[0]); if (score >= 50) { System. out. println("You pass"); } else { System. out. println("You fail"); } } }
รปแบบของคำสง vเราสามารถใชคำสง ดงน if. . else if ทมหลายเงอนไขได if (logical expression 1) { statements 1 } else if (logical expression 2) { statements 2 } else { statements 3 }
ตวอยางโปรแกรมทใชคำสง if. . else public class Sample. If. Else. If { public static void main(String args[]) { int x = Integer. parse. Int(args[0]); if (x == 1) { System. out. println("Value is one"); } else if (x == 2) { System. out. println("Value is two"); } else { System. out. println("Other than 1 or 2"); } } }
รปแบบของคำสง switch v ���������� switch (expression) { case value 1: statements 1 break; case value 2: statements 2 break; : : case value N: statements N break; default: statements N+1; }
ตวอยางโปรแกรมทใชคำส ง switch public class Sample. Switch { public static void main(String args[]) { int x = Integer. parse. Int(args[0]); switch(x) { case 1: System. out. println("Value is one"); break; case 2: System. out. println("Value is two"); break; default: System. out. println("Other than 1 or 2"); } } }
6/11/2563 ตวอยางโปรแกรมทใชคำสง switch char grade = 'B'; switch (grade) { case 'A': System. out. println("Average"); break; case 'B': System. out. println("Boring"); break; case 'C': System. out. println("Cool!"); break; case 'D': System. out. println("Delight"); break; default: System. out. println("Fabulous"); } 28
รปแบบของคำสง while v ���������� initial statements while (logical expression) statements update statements } {
ตวอยางโปรแกรมทใชคำส ง while public class Sample. While { public static void main(String args[]) { int i = 1; while(i <= 10) { System. out. print(i+" "); i++; } } } ����������
คำสง do. . while v ������� initial statements do { statements update statements } while (logical expression);
ตวอยางโปรแกรมทใชคำส ง do. . while public class Sample. Do. While { public static void main(String args[]) { int i = 1; do { System. out. print(i+" "); i++; } while (i <= 10); } } ��������������
รปแบบของคำสง for ��������� for(initial statements; logical expression; update statements; ){ statements }
ตวอยางโปรแกรมทใชคำส ง for public class Sample. For { public static void main(String args[]) { for (int i=1; i<=10; i++) { System. out. print(i+" "); } } } ������������
ตวอยางโปรแกรมแสดงขอบเขตของตวแป ร public class Variable. Scope { public static void main(String args[]) { for (int i=1; i<10; i++) { System. out. print(i+" "); } System. out. println("i = "+i); //illegal } }
ตวอยางโปรแกรมทใชคำสงโครง สรางแบบซอน public class Nested. For { public static void main(String args[]) { for (int i=1; i<=3; i++) { for (int j=1; j<=5; j++) { System. out. print('*'); } System. out. println(); } } } �����������
การออกจาก for ดวยคำสง break for (int i = 0; i < 5; i++) { System. out. print("<"); if (i == 2) break; System. out. print(i + ">"); } 6/11/2563 48
LOGO คำสง 6/11/2563 continue 49
������ for )int i = 0; i < 5 ; i ( ++ } System. out. print; (">") if (i == 2( continue; System. out. print(i; ("<" + { 6/11/2563 52
การจดระเบยบโปรแกรม public class If. Assign. Messy { public static void main( String[] args ) { int price = 99; int withdraw; if( price <= 100 ) withdraw = 100; else withdraw = 200; System. out. println("Withdraw = " + withdraw); } } 6/11/2563 ������ Java ����� 54
การจดระเบยบโปรแกรม public class If. Assign { public static void main(String[] args) { int price = 99; int withdraw; if (price <= 100) withdraw = 100; else withdraw = 200; System. out. println("Withdraw = " + withdraw); } 6/11/2563} ������ Java ����� 55
การจดระเบยบโปรแกรม public class If. Assign { public static void main(String[] args) { int price = 99; int withdraw; if (price <= 100) withdraw = 100; else withdraw = 200; System. out. println("Withdraw = " + withdraw); } } 6/11/2563 ������ Java ����� 56
LOGO 6/11/2563 62
- Slides: 62