โครงสร้างควบคุม

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    Favorites, Groups & Events

    โครงสร้างควบคุม - Presentation Transcript

    1. บทท 3 โครงสรางควบคม (Control Structures) อ.ธนศา เครอไวศยวรรณ คณะเทคโนโลย$สารสนเทศ สถาบ(นเทคโนโลย$พระจอมเกล-าเจ-าค.ณทหารลาดกระบ(ง
    2. วตถประสงค แนะนาคาสงโครงสรางควบคม คาสง if,if..else และ if แบบซ-อน คาสง switch คาสง while และ do..while คาสง for โครงสรางแบบซอน (Nested Structure) ค5าส(6ง break และ continue
    3. ลาดบการทางานของโปรแกรม ภาษาจาวามโครงสรางควบคมทจะกาหนดลาดบการทางานของคาสง 3 แบบ ดงน# 1. โครงสรางแบบตามลาดบ (Sequential Structure) 2. โครงสรางแบบเล%อกทา (Selection Structure) 3. โครงสรางแบบทาซ#า (Repetition Structure)
    4. โครงสรางแบบตามลาดบ ภาษาจาวาจะทางานตามลาดบของคาสงทมอย'(ในโปรแกรม เร*มทางานจากเมธอดทช%อว(า main() public static void main(String args[]) ทางานจากคาสงแรกของเมธอด main() และท5างานเรยงตามลาดบค5าส(งไป 6 เร6อยๆ
    5. โครงสรางแบบเล%อกทา ช.ดค5าส(6งโครงสร-างแบบเลอกท5าจะประกอบไปด-วยค5าส(6งด(งตCอไปน$D • ค5าส(6ง if • ค5าส(6ง if..else • ค5าส(6ง if แบบซ-อน (nested if) • ค5าส(6ง switch
    6. รFปแบบของคาสง if มร'ปแบบการใชคาสงดงน# if (logical expression) { statements }
    7. Flowchart แสดงล5าด(บการท5างานของคาสง if
    8. ตวอย(างโปรแกรมท$ใช-คาส(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 } } }
    9. รFปแบบของค5าส(ง 6 if..else มร'ปแบบการใชคาสงดงน# if (logical expression) { true statements } else { false statements }
    10. Flowchart แสดงล5าด(บการท5างานของคาสง if..else
    11. ตวอย(างโปรแกรมท$ใช-คาส(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"); } } }
    12. รFปแบบของคาสง if แบบซ-อน เราสามารถใช-คาสง if ทมหลายเง%อนไขได ดงน# if (logical expression 1) { if (logical expression 2) { statements 1 } else { statements 2 } } else { statements 3 }
    13. Flowchart ของคาสง if แบบซ-อน
    14. รFปแบบของคาสง if แบบซ-อน เราสามารถใช-คาสง if ทมหลายเง%อนไขได ดงน# if (logical expression 1) { statements 1 } else { if (logical expression 2) { statements 2 } else { statements 3 } }
    15. รFปแบบของคาสง if..else if..else เราสามารถใช-คาสง if ทมหลายเง%อนไขได ดงน# if (logical expression 1) { statements 1 } else if (logical expression 2) { statements 2 } else { statements 3 }
    16. Flowchart ของคาสง if..else if..else
    17. ตวอย(างโปรแกรมท$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 "); } } }
    18. ร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; }
    19. Flowchart แสดงลาดบการทางานของคาสง switch
    20. คาสง switch นพจนLตองมชน*ดขอม'ลเป.น char, byte, short หร%อ int เท(าน#น ชน*ดขอม'ลของนพจนLและค(าท 1 ถ/ง N ตองเป.นชน*ดเดยวกน ถาค(าของนพจนLตรงกบค(าใด จะทาชดคาสงของค(าน#น ถาค(าของนพจนLไม(ตรงกบค(าใดเลย จะทาชดคาสงของ default  default จะมหร%อไม(มก0ได
    21. 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 "); } } }
    22. โครงสรางแบบทาซ#า เปNนค5าส(งท$6ใช-ในการส(งให-ช.ดค5าส(งใดๆท5าซD5าหลายคร(งตามเง6อนไขท$ระบ. 6 6 6 D 6 ประกอบด-วยค5าส(6ง • while • do..while • for
    23. รFปแบบของคาสง while มร'ปแบบการใชคาสงดงน# initial statements while (logical expression) { statements update statements }
    24. Flowchart แสดงลาดบการทางานของคาสง while
    25. 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
    26. คาสง do..while มร'ปแบบการใชคาสงดงน# initial statements do { statements update statements } while (logical expression);
    27. Flowchart ของคาสง do..while
    28. 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
    29. รFปแบบของคาสง for มร'ปแบบการใชคาสงดงน# for (initial statements; logical expression; update statements) { statements }
    30. Flowchart แสดงล5าด(บการท5างานของคาสง for
    31. คาสง for คาสงกาหนดค(าเร*มตนและคาสงเปลยนแปลงค(า อาจมมากกว(าอย(างละ 1 คา สง โดยจะใชเคร%องหมาย ',' ในการแยกคาสง ตวอย(าง for (int i=0,j=0; i<4; i++,j+=2) { ... } ขอบเขตของตวแปรทประกาศในคาสงกาหนดค(าจะใชไดเฉพาะภายในบล0อก คาสง for เท(าน#น
    32. 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
    33. ตวอย(างโปรแกรมแสดงขอบเขตของตวแปร public class VariableScope { public void showDemo() { for (int i=1; i<10; i++) { System.out.print(i+" "); } System.out.println("i = "+i); //illegal } }
    34. โครงสรางแบบซอน (Nested Structure) เราสามารถทจะเขยนคาสงโครงสรางควบคมใดๆซอนอย'(ภายในได ตวอย(าง เช(น การเขยนโครงสรางทาซ#าแบบซอน (for อย'(ใน for) โครงสรางควบคมภายในและภายนอกไม(จาเป.นตองเป.นคาสงชน*ดเดยวกน
    35. ตวอย(างโครงสรางแบบซอน (Nested Loop) ตองการจะพ*มพ ***** ***** ***** โครงสรางทาซ#าทอย'(ภายใน จะพ*มพเคร%องหมาย ‘*’ เท(ากบจานวนคอลมน (column) โครงสรางทาซ#าทอย'(ภายนอก จะทาคาสงโครงสรางภายในเท(ากบจานวนแถว (row)
    36. 6 ตวอย(างโปรแกรมท$ใชคาสงโครงสรางแบบซอน public class NestedFor { public void showDemo() { for (int i=1; i<=3; i++) { for (int j=1; j<=5; j++) { System.out.print('*'); } System.out.println(); } } } ****** ผลล(พธLท$6ได-จากการร(นโปรแกรม ****** ******
    37. ค5าส(ง break และ continue 6 ค5าส(6ง break จะท5าให-หย.ดสDนส.ดการท5างานของโครงสร-างแบบท5าซD5า สCวนค5าส(6ง continue จะข-ามการท5างานค5าส(งท$6เหลอภายในบลSอก { } โดย 6 ไปเร6มการท5าซD5าในรอบตCอไปใหมC ต(วอยCางเชCน for(int i = 1; i<= 5; i++) { if (i == 3) break; System.out.println(i); } for(int i = 1; i<= 5; i++) { if (i == 3) continue; System.out.println(i); }
    38. คาสงอ%นๆในการควบคม loop label : statements; • label เป.นการระบตาแหน(งของ loop กรณทม loop ซอนกน  break [label]; • คาสงใหส*#นสดการทางานใน loop  continue [label]; • คาสงใหขามการทางานของคาสงทเหล%อท#งหมดใน loop
    39. 6 ตวอย(างโปรแกรมท$ใชคาสงโครงสรางแบบซอน public class SampleBreak2 { public void showDemo() { int i, j, product; outer: for (i=1; i<=3; i++) { for (j=1; j<=3; j++) product = i*j; if (j==3) break outer; System.out.println(i+" * "+j+" = "+product); } } System.out.println("Outside nested loops."); } } 1 * 1 = 1 ผลล(พธLท$6ได-จากการร(นโปรแกรม 1 * 2 = 2 Outside nested loops.
    40. สร.ปเนDอหาของบท ค5าส(6งโครงสร-างควบค.ม เปNนค5าส(งท$6ใช-ในการก5าหนดล5าด(บการท5างานของค5า 6 ส(งตCางๆ โดยม$สามรFปแบบคอ โครงสร-างแบบตามล5าด(บ โครงสร-างแบบ 6 เลอกท5า และ โครงสร-างแบบท5าซD5า ค5าส(6งท$6เปNนค5าส(6งของโครงสร-างแบบเลอกท5าคอ ค5าส(6ง if,if..else หรอ switch ค5าส(6ง if..else แตกตCางจากค5าส(6ง if ตรงท$6ค5าส(ง if..else จะม$การท5า 6 ค5าส(6งส5าหร(บคCาเทSจถ-านพจนLตรรกศาสตรLเปNนเทSจ สCวนค5าส(6ง if จะไมCม$การ ท5าค5าส(6งใดถ-านพจนLตรรกศาสตรLเปNนเทSจ ค5าส(6ง if หรอ if..else สามารถท$6จะซ-อนอยFCข-างในค5าส(6ง if หรอ if..else อ6นได-
    41. สร.ปเนDอหาของบท ค5าส(6ง switch จะม$ล(กษณะโครงสร-างการท5างานคล-ายคลงก(บค5าส(ง 6 if..else if..else.. แตCชนดข-อมFลของต(วแปรท$6จะน5ามาใช-ก(บค5าส(6ง switch จะต-องเปNนชนด char, byte, short หรอ int เทCาน(Dน ค5าส(6งท$6เปNนค5าส(6งของโครงสร-างแบบท5าซD5าคอ ค5าส(ง while,do..while 6 หรอ for ค5าส(6ง while แตกตCางจากค5าส(6ง do..while ตรงท$6 ค5าส(6ง while จะไมCม$ การท5าช.ดค5าส(6งเลยถ-านพจนLตรรกศาสตรLเปNนเทSจ สCวนค5าส(6ง do..while จะม$การท5าช.ดค5าส(งหนงคร(งถ-านพจนLตรรกศาสตรLเปNนเทSจ 6 6 D ค5าส(6ง for ม$ล(กษณะการท5างานท$6เหมอนก(บค5าส(6ง while แตCจะม$การรวม ค5าส(6งก5าหนดคCาเร6มต-น นพจนLตรรกศาสตรLและค5าส(6งเปล$6ยนแปลงคCาไว-หล(ง ค5าส(6ง for
    42. สร.ปเนDอหาของบท ค5าส(6ง for จะใช-ในกรณ$ททราบจ5านวนคร(Dงในการท5าซD5าท$แนCนอน สCวนค5าส(6ง $6 6 while หรอ do..while นยมใช-ในกรณ$ท$6ไมCทราบจ5านวนคร(งในการท5าซD5า D ลCวงหน-า ค5าส(6งโครงสร-างท(Dงหมดท$6กลCาวมาแล-วข-างต-น สามารถน5ามาใช-รวมก(นเปNน C ล(กษณะแบบซ-อนได- เชCน ค5าส(ง switch อยFCภายในค5าส(6ง while หรอค5าส(6ง 6 for อยFCภายในค5าส(6ง for ซงเร$ยกโครงสร-างในล(กษณะน$Dวา โครงสร-างแบบ 6 C ซ-อน ค5าส(6ง break จะท5าให-หย.ดสDนส.ดการท5างานของโครงสร-างแบบท5าซD5า สCวน ค5าส(6ง continue จะข-ามการท5างานค5าส(6งท$6เหลอภายในบลSอก { } โดยไปเร6ม การท5าซD5าในรอบตCอไปใหมC
    43. แบบฝ7กหด แบบฝ^กห(ดท$6 1 การใช-ค5าส(6ง if..else • ใช-ค5าส(ง if..else เพ6อตรวจสอบคCาของ b2-4ac (โดยร(บคCา a,b และ c เข-า 6 มาทาง command line) ถ-าม$คCาเปNนลบให-พมพLข-อความวCา No answer แตCถาม$คCา - เปNนบวกหรอศFนยLให-ค5านวณหาค5าตอบและพมพLค5าตอบท(งสองคCาออกมาทาง D จอภาพ แบบฝ^กห(ดท$6 2 การใช-ค5าส(6ง if แบบซ-อน • จงเข$ยนโปรแกรมค5านวณหาและพมพLคCาเกรดท$เหมาะสมตามคะแนนท$6ร(บเข-ามาจาก 6 ผF-ใช- โดยม$เกณฑLตามน$D • คะแนนต(DงแตC 90 ถง 100 ได-เกรด A • คะแนนต(DงแตC 80 ถง 89 ได-เกรด B • คะแนนต(DงแตC 70 ถง 79 ได-เกรด C • คะแนนต(DงแตC 60 ถง 69 ได-เกรด D • คะแนนท$6ต65ากวCา 60 ได-เกรด F
    44. แบบฝ7กหด แบบฝ^กห(ดท$6 3 การใช-ค5าส(6ง for แบบซ-อน • จงเข$ยนโปรแกรมภาษาจาวาโดยใช-ค5าส(ง for แบบซ-อนเพ6อพมพLรFปด(งตCอไปน$D 6 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
    SlideShare Zeitgeist 2009

    + Thanachart NumnondaThanachart Numnonda Nominate

    custom

    379 views, 0 favs, 2 embeds more stats

    การเขียนโปรแกรมภา more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 379
      • 93 on SlideShare
      • 286 from embeds
    • Comments 0
    • Favorites 0
    • Downloads 0
    Most viewed embeds
    • 284 views on http://www.thaijavadev.com
    • 2 views on http://thaijavadev.com

    more

    All embeds
    • 284 views on http://www.thaijavadev.com
    • 2 views on http://thaijavadev.com

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories

    Tags