บทความ

กำลังแสดงโพสต์จาก พฤศจิกายน, 2015

Part1 -> Section1 -> Basic -> 011. Functions

รูปภาพ
     ฟังก์ชัน (Function) หรือใน .NET เรียกว่า เมธอด (Method) อธิบายแบบเข้าใจง่ายๆเลยก็คือ การยกโค้ดบางส่วนไปไว้ที่อื่น (เน้นที่มีการใช้งานโค้ดส่วนนี้ซ้ำๆหลายครั้ง) ซึ่งเราสามารถเรียกใช้โค้ดส่วนนั้นจากที่ไหนก็ได้ และเรียกกี่ครั้งก็ได้ ซึ่งเรียกการทำงานแบบนี้ว่าเป็นการ ใช้โค้ดซ้ำ (reuse) เชิญดูรูปแบบการสร้าง Method ด้านล่าง <accessibility> <return type> <method name>([parameters]) { <code> }

Part1 -> Section1 -> Basic -> 010. Loops

รูปภาพ
     วันนี้เราจะมาศึกษาการเขียนโปรแกรมเพื่อทำงานแบบวนซ้ำๆ เป็นส่วนที่สำคัญมากในการเขียนโปรแกรม ซึ่งเรามีตัวช่วยอยู่หลายตัวให้เลือกใช้ เราจะมาดูทีละตัวกันเลยว่าใช้งานอย่างไร The while loop      while loop เป็น loop ที่ง่านต่อการเริ่มศึกษา ซึ่งการทำงานของมันก็คือ มันจะทำงานใน block code ไปเรื่อยๆ จะกว่าเงื่อนไขที่เราส่งเข้าไปจะเป็น false ลองดูจากตัวอย่าง using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int number = 0; while (number < 5) { Console.WriteLine("Number: " + number); number += 1; // equal to number = number + 1 } Console.ReadLine(); } } }

Part1 -> Section1 -> Basic -> 009. The switch statement

     switch พูดง่ายๆคือ ทำหน้าที่เหมือนกันกับ if statement คือการทำงานตามเงื่อนไข แต่ switch จะเน้นในกรณีที่มีเงื่อนไขหลายเงื่อนไข ลองดูจากตัวอย่าง using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int cond = 1; switch (cond) { case 0: Console.WriteLine("Condition zero"); break; case 1: Console.WriteLine("Condition one"); break; } Console.ReadLine(); } } }

Part1 -> Section1 -> Basic -> 008. The if statement

     if statement คือโค้ดบล็อกที่ใช้เลือกเงื่อนไข ซึ่ง if ต้องการ boolean เป็น input (มีแค่ true และ false) ลองศึกษาเพิ่มจากตัวอย่าง using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int input; Console.WriteLine("Please enter a number 0 to 10"); input = int.Parse(Console.ReadLine()); if (input > 10) { Console.WriteLine("You enter a number more than 10"); } else if (input > 0) { Console.WriteLine("Good job"); } else { Console.WriteLine("You enter a negative number"); } } } }