ตัวอย่างในการใช้ตัวดำเนินการทางคณิตศาสตร์เบื้องต้น
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace test_operators { class Program { static void Main(string[] args) { int num = 10,num2 = 5; float num3 = 5.5f; Console.WriteLine("num3 = " + num3); Console.WriteLine("num + num2 = " + (num + num2)); Console.WriteLine("num - num2 = " + (num - num2)); Console.WriteLine("num * num2 = " + (num * num3)); Console.WriteLine("num / num2 = " + (num / num3)); Console.WriteLine("num % num2 = " + (num % num2)); Console.ReadLine(); } } }
ผลการรัน
num3 = 5.5
num + num2 = 15
num - num2 = 5
num * num2 = 55
num / num2 = 1.818182
num % num2 = 0
ตัวอย่างดำเนินการที่ใช้สำหรับเพิ่มค่าและลดค่า
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace increase_reduce { class Program { static void Main(string[] args) { int a = 10; Console.WriteLine("a++ = {0}", a++); // ปริ้น 10 และ a=a+1 a = 11 Console.WriteLine("++a = {0}", ++a); // เพิ่มค่า a=a+1 a = 12 Console.WriteLine("a+=1 = {0}", a+=1); // a = a+1 a=13 Console.WriteLine("--a = {0}", --a); // a = a-1 a=12 Console.ReadLine(); } } }
ผลการรัน
a++ = 10
++a = 12
a+=1 = 13
--a = 12
ตัวอย่างในการใช้ตัวดำเนินการเปรียบเทียบ
a == b a เท่ากับ b
a != b a ไม่เท่ากับ b
a < b a น้อยกว่า b
a > b a มากกว่า b
a <= b a น้อยกว่าหรือเท่ากับ b
a >= b a มากกว่าหรือเท่ากับ b
ตัวอย่าง
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace comparison { class Program { static void Main(string[] args) { int a = 10, b = 10, c = 30; if (a == b) { Console.WriteLine("a == b True"); } if(a != c){ Console.WriteLine("a != c True"); } if(a < c){ Console.WriteLine("a < c True"); } if (a > b) { Console.WriteLine("a > b True"); } else { Console.WriteLine("a > b False"); } if(a<=b){ Console.WriteLine("a < = b True"); } if(a >= b){ Console.WriteLine("a > = b True"); } Console.ReadLine(); } } }
ผลการรัน
a == b True
a != c True
a < c True
a > b False
a < = b True
a > = b True
ตัวดำเนินการเชิงตรรกะ
! ไม่เท่ากับ ! (n==1)
&& และ a == 1 && b == 1
|| หรือ a == 1 || b == 1
ตัวอย่าง
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace logical_operators { class Program { static void Main(string[] args) { bool a = (3 == 4); bool b = (3 != 4); int score = 78; Console.WriteLine("a = " + a); // boolean False Console.WriteLine("b = " + b); // boolean True if(!(score==77)) // เงื่อนไขคือ score != 77 นั้นเอง { Console.WriteLine("score not = 77 "); } if (score <= 0 || score > 100) // หรือ { Console.WriteLine("Error Score."); } if (score >= 50 && score <= 70) // และ { Console.WriteLine("Grade B"); } else if (score >= 71 && score <= 100) { Console.WriteLine("Grade A"); } Console.ReadLine(); } } }
ผลการรัน
a = False
b = True
score not = 77
Grade A
ternary operator ใช้เครื่องหมาย ?
รูปแบบการใช้งานดังนี้
expression ? value1 : value2;
a == "aa" ? True : False
ตัวอย่าง เช็คชื่อ login ว่าตรงไหม แบบพอเข้าใจ
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ternary_operator { class Program { static void Main(string[] args) { Console.Write("Your Name Login : "); String name = Console.ReadLine(); // รับค่ามา Console.WriteLine(name == "zeza" ? "Ok!! Hello zeza" : "Oh!! You are not login {0} ",name); Console.ReadLine(); } } }
ผลการรัน
Your Name Login : bom
Oh!! You are not login bom