1 / 74

คำสั่งเงื่อนไขและการตัดสินใจ

บทที่ 3 ตอนที่ 1 คำสั่งเงื่อนไขและการตัดสินใจ (p.2-26) ตอนที่ 2 Standard Windows Forms Controls (27-74). คำสั่งเงื่อนไขและการตัดสินใจ. ในการทำงานของโปรแกรมทั่วๆไปจะมีการทำงาน 3 ประเภทหลัก การทำงานเป็นลำดับ (sequence) ก่อนและหลัง การตัดสินใจ( decision) มีเงื่อนไขให้ตรวจสอบการทำงาน

Download Presentation

คำสั่งเงื่อนไขและการตัดสินใจ

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. บทที่ 3ตอนที่ 1คำสั่งเงื่อนไขและการตัดสินใจ(p.2-26)ตอนที่ 2 Standard Windows Forms Controls(27-74)

  2. คำสั่งเงื่อนไขและการตัดสินใจคำสั่งเงื่อนไขและการตัดสินใจ • ในการทำงานของโปรแกรมทั่วๆไปจะมีการทำงาน3 ประเภทหลัก • การทำงานเป็นลำดับ (sequence) ก่อนและหลัง • การตัดสินใจ(decision) มีเงื่อนไขให้ตรวจสอบการทำงาน • การทำงานที่ต้องทำซ้ำ ๆ หลายครั้งหรือการวนลูป(loop)

  3. คำสั่งเงื่อนไขและการตัดสินใจคำสั่งเงื่อนไขและการตัดสินใจ • คำสั่งที่ใช้ในการตัดสินใจเลือก ถือเป็นคำสั่งควบคุมชนิดหนึ่ง • ภาษาโปรแกรมโดยทั่วไปจะมี 2 คำสั่ง คือ • if – statement • case หรือ switch

  4. คำสั่ง IF • if เป็นการเปรียบเทียบเงื่อนไขว่าตรงตามที่เราต้องการหรือไม่ หากตรงก็จะทำตามคำสั่งที่กำหนด แต่หากไม่ตรงก็จะข้ามคำสั่งส่วนนั้นไป • กรณี การเขียนเงื่อนไขเดียว และกระทำกรณีเป็นจริง รูปแบบคำสั่ง • ตรวจสอบเงื่อนไข ถ้าเป็นจริงจะทำงานตาม statement if ( condition ) { statement (s) }

  5. ตัวอย่าง การเขียนเงื่อนไขเดียว • if (x==y) { Console.Write("x equals to y"); } • if ((x>0)&& (y<>0)) { Z=x/y; }

  6. คำสั่ง IF • หากหลัง if มีเพียง statement เดียวไม่จำเป็นต้องเขียน {} ครอบคำสั่งก็ได้เช่น • if (x<0) str strText = "It’s Negative“; • if (x>=0) str strText = "It’s Positive” ;

  7. if-else statement if (expression ที่ทดสอบเงื่อนไข) { // ชุดคำสั่งเมื่อเงื่อนไขเป็นจริง } else { // ชุดคำสั่งเมื่อเงื่อนไขเป็นเท็จ }

  8. การใช้ if-else แบบซ้อนกัน if (expression ที่ทดสอบเงื่อนไขที่ 1) { If (expression ที่ทดสอบเงื่อนไขที่ 2) { (ชุดคำสั่งเมื่อเงื่อนไขที่ 2 เป็นจริง) } else { (ชุดคำสั่งเมื่อเงื่อนไขที่ 2 เป็นเท็จ) } else { (expression ที่ทดสอบเงื่อนไขที่ 1) }

  9. if (condition1) { … } else if (condition2) { … } else { … } else if & Nested if statement ตัวอย่าง char Result; if (Score > 80) Result = ‘A’; else if (Score > 50) Result = ‘B’; else Result = ‘F’;

  10. if (condition1) { … if (condition2) { … if (condition3) { … } } } x=10; y=20 if (x>5) { if (y>30) { z=x*y; } else { z=x+y; } } else if & Nested if statement

  11. โปรแกรมสำหรับคำนวณเกรดโปรแกรมสำหรับคำนวณเกรด if (score >= 90) { grade = 'A'; } else if (score >= 80) { grade = 'B'; } else if (score >= 70) { grade = 'C'; } else if (score >= 60) { grade = 'D'; } else { grade = 'F'; }

  12. คำสั่ง switch..case • ใช้ในการตัดสินใจเลือกมากกว่า 2 ทางเลือก • จะทำการตรวจสอบเงื่อนไขในการเลือกก่อนว่าจะตรงกับตัวเลือกใด แล้วจึงทำงานตามคำสั่งที่ต่อท้ายตัวเลือกนั้น

  13. คำสั่ง switch..case switch (variable) { case ค่าที่1 ของตัวแปร:// เงื่อนไขแรก statement1; // ทำงานตามเงื่อนไขแรก case ค่าที่2 ของตัวแปร:// เงื่อนไขที่สอง statement2; // ทำงานตามเงื่อนไขที่สอง … … case สุดท้าย:// เงื่อนไขที่สอง statement; // ทำงานตามเงื่อนไขสุดท้าย default : statementN; // เมื่อไม่ตรงกับเงื่อนไขใดๆ เลย }

  14. ภาพแสดงลักษณะการตรวจสอบเงื่อนไข ของ switch-case

  15. string str, color; Console.Write("Enter day of week:"); str = Console.ReadLine(); switch (str) { case "Sunday" : color = "red"; break; case "Monday" : color="yellow"; break; ... default : color="invalid"; break; } Console.WriteLine("color of " + str.ToUpper() + " is " + color.ToUpper()); Console.Read (); switch (expression) { case value1 :// เงื่อนไขแรก statement1; // คำสั่ง break; case value2 : statement2; break; case valueN : statementN; break; default : statement; break; } expression: ตัวเลข or string switch statement • string str, color; • Console.Write("Enter day of week:"); • str = Console.ReadLine(); • switch (str) { • case "Sunday" : color = "red"; • break; • case “monday" : • case "Monday" : color="yellow"; • break; • ... • default : color="invalid"; • break; • } • //แสดงผลบน lblResult

  16. switch statement class DateDemo { public static void Main () { char d; Console.Write(“ Input number date 1 – 7 : “); d = (char) Console.Read(); Console.Write(“ Input value : “ + d+” = Date of week = “); Switch (d) { case ‘1’ : Console.WriteLine(“Sunday.”); break; case ‘2’ : Console.WriteLine(“Monday.”); break; case ‘3’ : Console.WriteLine(“Tuesday.”); break; case ‘4’ : Console.WriteLine(“Wednesday.”); break; case ‘5’ : Console.WriteLine(“Thursday.”); break; case ‘6’ : Console.WriteLine(“Friday.”); break; case ‘7’ : Console.WriteLine(“Saturday.”); break; default : Console.WriteLine(“Invalid number”); break; } }}

  17. ข้อควรระวังในการใช้ switch คือ • ค่าของตัวแปรหรือนิพจน์ต้องเป็นเลขจำนวนเต็มหรือ char, byte เท่านั้น ไม่สามารถใช้แบบเป็นช่วง • ชนิดข้อมูลของตัวแปร จะต้องเป็นประเภทเดียวกับค่าข้อมูลที่กำหนดหลัง case • คำสั่ง switch จะประกอบด้วยคำสั่ง break หลังการทำงาน เพื่อควบคุมการทำงานของโปรแกรม ให้ทำงานเฉพาะกรณีผลการตรวจสอบเป็นจริง เพื่อลดเวลาในการประมวลผล แต่สามารถไม่ใส่คำสั่งนี้ได้ หากต้องการให้ทำงานต่อเนื่องทุก case

  18. ในทางปฏิบัติจะเห็นว่าคำสั่ง switch และคำสั่ง if ให้ผลการทำงานเกือบเหมือนกัน • ผู้ใช้สามารถเลือกเขียนด้วยชุดคำสั่งใดก็ได้ • โดยทั่วไปคำสั่ง if จะครอบคลุมเงื่อนไขในการทดสอบได้มากกว่า เช่นสามารถทดสอบเป็นช่วงได้ สามารถทดสอบจำนวนจริงได้ แต่คำสั่ง switch จะช่วยให้โปรแกรมดูง่ายและสะดวกในการเขียนหรือแก้ไขโปรแกรม

  19. Iteration Statement

  20. for statement เป็นการทำงานซ้ำตามจำนวนครั้งที่กำหนด โดยเริ่มจากค่าแรกไปจนถึงค่าสุดท้าย รูปแบบคำสั่ง for (counter = first value; counter condition; adjust counter value) { … } เช่น for (i=0;i<=10;i++) { … } หมายถึง ให้วนลูปเริ่มตั้งแต่ค่า i=0 ไปเรื่อยๆ ตราบใดที่ค่า i ยังไม่เกิน 10 และให้เพิ่มค่า i ทีละ 1

  21. Nest for…Loop • สามารถใช้การวนลูปซ้อนกันกี่ชั้นก็ได้ for (…) { … for (…) { … for (…) { … }}}

  22. ตัวอย่าง for statement (2) • int num; string s; • Console.Write ("Please Enter number : "); • s=Console.ReadLine( ); • num=int.Parse(s); • for (int i=1; i<=num; i++){ • for (int j=1;j<=i;j++){ • Console.Write(j+" "); • } // end for j • Console.WriteLine(" "); • } // end for i • Console.Read( );

  23. While • ในการใช้งาน for Loop จำเป็นต้องระบุค่าเริ่มต้นและค่าสุดท้ายของลูป • แต่ในกรณีที่ไม่ทราบค่าดังกล่าว แต่ทราบเงื่อนไขบางอย่าง สามารถใช้ลูป while แทนได้

  24. while statementโครงสร้างของลูป while while (condition) { ….statement…. } ตัวอย่าง • int Result = 0; int i = 0; • while (i < 2) { • i += 1; • Result += i; • } // end while • Console.Write (Result + " " + i); // Result=____ i=____

  25. do-while statement do { … } while (condition); ตัวอย่าง • int Result = 0; int i = 0; • do { • i += 1; • Result += i; • } while (i < 2); • Console.Write (Result + " " + i); // Result=____ i=____ while (condition) { … }

  26. แบบฝึกหัด for & while statement (4) • พี่มอสมีแฟนคลับ 2,857 คน โดยแต่ละปีจะมีแฟนคลับเพิ่มขึ้นปีละ 8% จงเขียนโปรแกรมคำนวณว่าจะใช้เวลากี่ปี พี่มอสจึงจะมีแฟนคลับมากกว่า 5,000 คน

  27. องค์ประกอบของ IDE ใน c#.net

  28. เมื่อเราเพิ่ม control(object)

  29. สังเกตุฟังก์ชั่น

  30. การสร้าง Event-Driven Programming • แนวคิดของการเขียนโปรแกรม คือต้องสามารถตอบสนองต่อเหตุการณ์(Event-handling) ที่เกิดขึ้น เช่น การคลิก(Click) การดับเบิ้ลคลิก(DoubleClick) การกดแป้นพิมพ์(KeyDown, KeyPress, KeyUp) การเคลื่อนที่ของเมาส์(MouseMove, MouseUp, MouseDown) และเหตุการณ์อื่นๆ

  31. รูปแบบของ Event-Driven Programming private void ObjName_Event(object sender, EventArgs e) { . . . } ObjName :ชื่อของวัตถุที่เราต้องการควบคุมเหตุการณ์ Event : เหตุการณ์ที่เกิดขึ้นกับวัตถุ sender: ตัวแปรที่ส่งไปแทนวัตถุที่เกิดเหตุการณ์ขึ้น e: ตัวแปรที่ส่งไปแทนเหตุที่เกิดขึ้นกับวัตถุ private void textBox1_TextChanged(object sender, EventArgs e){ }

  32. Form • อยู่ภายใต้ namespace : System.Windows.Forms.Form • ใช้เป็นcontainer สำหรับนำ controls อื่นๆมาวางเพื่อสร้างGUI (Graphic User Interface)

  33. Text Font Opacity TopMost AcceptButton CancelButton AutoScroll Icon BackColor Enabled WindowState StartPosition Location Size MaximumSize MinimumSize Form Properties

  34. Form Events • Activated and Deactivate • Load • Closing • Closed • Resize • Move • etc.

  35. Form Event : Closing() • privatevoid Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e) • { • if(MessageBox.Show(“Close Application?", "Closing Event", MessageBoxButtons.YesNo) == DialogResult.Yes) • { • this.Dispose(); // ปิดform ตัวเอง • // Application.Exit(); // จบการทำงานของapplication • } • else • { • // Cancel the Closing event from closing the form. • e.Cancel = true; • } • }

  36. การกำหนดControl Tab Order • เลือกView menu -> Tab Order • กำหนดลำดับtab ให้กับcontrol ต่างๆ • เลือกView menu -> Tab Order อีกครั้ง

  37. Windows Forms Inheritance • เลือกAdd -> Add Inherited Form… • กำหนดชื่อform ใหม่ • เลือกform ต้นแบบ • การปรับปรุงที่form ต้นแบบส่งผลถึงform ใหม่ที่inherit มา

  38. TextBox • อยู่ภายใต้ namespace : System.Windows.Forms.TextBox • สำหรับแสดงผลข้อความหรือรับข้อความจากคีย์บอร์ดขณะรันโปรแกรม

  39. Text TextAlign Visible Font BackColor ForeColor MultiLine ScrollBar BorderStyle Enable ReadOnly PasswordChar MaxLength Anchor AutoSize SelectionStart SelectionLength TextLength TextBox Properties

  40. TextBox Method • Focus () กำหนดcursorให้อยู่ที่TextBox • SelectAll () เลือกข้อความทั้งหมดภายใน TextBox • Clear () ลบข้อความในTextBox • Hide () ซ่อนTextBox • Show () แสดงTextBox

  41. TextBox Events • TextChanged เกิดขึ้นเมื่อข้อความบน TextBox เปลี่ยนแปลง • KeyPress เกิดขึ้นเมื่อผู้ใช้กดตัวอักษรบนคีย์บอร์ด • etc.

  42. การจัดการข้อความผ่านTextBoxการจัดการข้อความผ่านTextBox • กำหนดข้อความให้แสดงบนTextBox • ดึงข้อความบนTextBox มาใช้งาน textBox1.Text = "ข้อความ"; string str = textBox1.Text;

  43. การcoding เพื่อกำหนดProperties • void myMethod() { • textBox1.Text = "123"; • textBox1.BackColor = Color.Red; • textBox1.Font=new Font("Tahoma",16,FontStyle.Bold); • }

  44. Label • อยู่ภายใต้ namespace : System.Windows.Forms.Label • สำหรับแสดงข้อความคล้ายTextBox แต่ผู้ใช้ไม่สามารถแก้ไขหรือคัดลอกข้อความได้ (read only)

  45. Button • อยู่ภายใต้ namespace : System.Windows.Forms.Button • เป็นcontrol พื้นฐานที่มีใช้งานสำหรับกดเพื่อทำงานบางอย่าง

  46. Button Properties • Text ข้อความที่แสดงบนButton • Image รูปภาพที่แสดงบนButton • Anchor กำหนดตำแหน่งButtonตรึงบนForm

  47. Button Events • Click เกิดขึ้นเมื่อclick ที่ปุ่ม • MouseHover เกิดขึ้นเมื่อนำเมาส์มาวางเหนือปุ่ม

  48. แบบฝึกหัด(1) • จงเขียนโปรแกรมสำหรับคำนวณเกรดโดยรับคะแนนจากผู้ใช้โดยกำหนดว่า

  49. แบบฝึกหัด(2) • จงเขียนโปรแกรมสำหรับสร้างเครื่องคิดเลขให้สามารถ +, -, *, / ได้โดยรับค่าจากผู้ใช้และแสดงค่าผ่านtextBox

More Related