Page cover

Operator

ศิริวรรณ สิงห์ลอ 650710095

Operators หรือ ตัวดำเนินการ เป็นพื้นฐานของภาษาโปรแกรมหลาย ๆ ภาษาโปรแกรม ดังนั้นการทำงานของภาษาโปรแกรม C# จะไม่สมบูรณ์ หากไม่มีการใช้ Operators

Operators ทำให้เราสามารถดำเนินการกับ Operands หรือ ตัวถูกดำเนินการ ได้

การจัดกลุ่มของ Operators ใน C#

  1. การจัดกลุ่มตามฟังก์ชันการใช้งานที่แตกต่างกัน

  • Arithmetic Operators: ตัวดำเนินการทางคณิตศาสตร์

  • Relational Operators: ตัวดำเนินการแบบสัมพันธ์

  • Logical Operators: ตัวดำเนินการทางตรรกะ

  • Bitwise Operators: ตัวดำเนินการระดับบิต

  • Assignment Operators: ตัวดำเนินการกำหนดค่า

  • Conditional Operator: ตัวดำเนินการแบบเงื่อนไข

  1. การจัดกลุ่มตามจำนวนตัวถูกดำเนินการ

  • Unary Operator: ตัวดำเนินการที่มีตัวถูกดำเนินการ 1 ตัว

  • Binary Operator: ตัวดำเนินการที่มีตัวถูกดำเนินการ 2 ตัว

  • Ternary Operator: ตัวดำเนินการที่มีตัวถูกดำเนินการ 3 ตัว

Arithmetic Operators - ตัวดำเนินการทางคณิตศาสตร์

  1. Arithmetic Operators (Binary Operators)

ตัวดำเนินการทางคณิตศาสตร์ ที่มี ตัวถูกดำเนินการ 2 ตัว

Operators
Example

Addition (+)

x+y

Subtraction (-)

x-y

Multiplication (*)

x*y

Division (/)

x/y

Modulus (%)

x%y

  1. Arithmetic Operators (Unary Operators)

ตัวดำเนินการทางคณิตศาสตร์ ที่มี ตัวถูกดำเนินการ 1 ตัว

Operators
Example

Increment (++)

++x (pre-increment operator)

x++ (post-increment operator)

Decrement (--)

--x (pre-decrement operator)

x-- (post-decrement operator)

Relational Operators - ตัวดำเนินการแบบสัมพันธ์

ใช้เปรียบเทียบค่า 2 ค่า โดยจะคืนค่าเป็น จริง หรือ เท็จ (Boolean)

Operators
Example

Equal To (==)

5==5 (return true)

Not Equal To (!=)

5!=5 (return false)

Greater Than (>)

6>5 (return true)

Less Than (<)

6<5 (return false)

Greater Than Equal To (>=)

5>=5 (return true)

Less Than Equal To (<=)

5<=5 (return true)

Logical Operators - ตัวดำเนินการทางตรรกะ

ใช้เชื่อมโยงสอง หรือมากกว่าสองเงื่อนไข ในการตัดสินใจว่าผลลัพธ์จะคืนค่าเป็น true หรือ false

Operators
Example

Logical AND (&&)

a && b (returns true เมื่อทั้ง a และ b เป็นจริง)

Logical OR (||)

a||b (returns true เมื่อ a หรือ b เป็นจริง)

Logical NOT (!)

!a (returns true ถ้า a เป็นเท็จ)

Bitwise Operators - ตัวดำเนินการระดับบิต

ใน C# มีตัวดำเนินการระดับบิต 6 ตัว

Operators
Example

bitwise AND (&)

A = 60, B = 13 จะได้เลขฐานสองเป็น

A = 0011 1100, B = 0000 1101

A&B = 0000 1100 (12)

bitwise OR (|)

A = 60, B = 13 จะได้เลขฐานสองเป็น

A = 0011 1100, B = 0000 1101

A|B = 0011 1101 (61)

bitwise XOR (^)

A = 60, B = 13 จะได้เลขฐานสองเป็น

A = 0011 1100, B = 0000 1101

A^B = 0011 0001 (49)

bitwise Complement (~)

A = 60 จะได้เลขฐานสองเป็น 0011 1100

เปลี่ยนจาก 0 เป็น 1 และ 1 เป็น 0

จะได้ ~A = 1100 0011 (-61)

left shift (<<)

A = 60 จะได้เลขฐานสองเป็น 0011 1100

60<<2 คือ การเลื่อนบิตไปทางซ้าย 2 บิต

จะได้ 1111 0000 (240)

right shift (>>)

A = 60 จะได้เลขฐานสองเป็น 0011 1100

60>>2 คือ การเลื่อนบิตไปทางขวา 2 บิต

จะได้ 0000 1111 (15)

Assignment Operators - ตัวดำเนินการกำหนดค่า

ใช้กำหนดค่าให้กับตัวแปร โดยฝั่งซ้ายเป็นตัวแปร ส่วนฝั่งขวาเป็นค่า

Operators
Example

Simple Assignment (=)

Add Assignment (+=)

Subtract Assignment (-=)

Multiply Assignment (*=)

Division Assignment (/=)

Modulus Assignment (%=)

Left Shift Assignment (<<=)

Right Shift Assignment (>>=)

Bitwise AND Assignment (&=)

Bitwise Exclusive OR (^=)

Bitwise Inclusive OR (|=)

Conditional Operator - ตัวดำเนินการแบบเงื่อนไข

ใช้ตรวจสอบเงื่อนไขเพื่อการตัดสินใจในการทำงาน อยู่ในกลุ่ม Ternary Operator (ตัวดำเนินการที่มีตัวถูกดำเนินการ 3 ตัว)

Syntax:

condition ถ้าเงื่อนไขเป็น จริง จะทำการประมวลผลในคำสั่ง first_expression และจะได้ผลลัพธ์จากการประมวลผลในคำสั่ง first_expression แต่ถ้าเงื่อนไขเป็น เท็จ จะทำการประมวลผลในคำสั่ง second_expression และจะได้ผลลัพธ์จากการประมวลผลในคำสั่ง second_expression

ลําดับความสําคัญ และการเรียงของตัวดําเนินการในภาษา C#

กลุ่มของตัวดำเนินการ
ตัวดำเนินการ
เรียงจาก

Unary

+, -, !, ~, ++, --, (type)* ,& ,sizeof

ขวา ไป ซ้าย

Additive

+, -

ซ้าย ไป ขวา

Multiplicative

%, /, *

ซ้าย ไป ขวา

Relational

<, >, <=, >=

ซ้าย ไป ขวา

Shift

<<, >>

ซ้าย ไป ขวา

Equality

==, !=

ขวา ไป ซ้าย

Logical AND

&

ซ้าย ไป ขวา

Logical OR

|

ซ้าย ไป ขวา

Logical XOR

^

ซ้าย ไป ขวา

Conditional OR

||

ซ้าย ไป ขวา

Conditional AND

&&

ซ้าย ไป ขวา

Null Coalescing

??

ซ้าย ไป ขวา

Ternary

?:

ขวา ไป ซ้าย

Assignment

=, *=, /=, %=, +=, - =, <<=, >>=, &=, ^=, |=, =>

ขวา ไป ซ้าย

เปรียบเทียบกับภาษา C# กับภาษา Java / C / Python

ตัวดำเนินการ
C#
Java
C
Python

Arithmetic Operators

+, -, *, /, %, ++, --

+, -, *, /, %, ++, --

+, -, *, /, %, ++, --

+, -, *, /, %, **, //

Relational Operators

==, !=, >, <, >=, <=

==, !=, >, <, >=, <=

==, !=, >, <, >=, <=

==, !=, >, <, >=, <=

Logical Operators

&&, ||, !

&&, ||, !

&&, ||, !

and, or, not

Bitwise Operators

&, |, ^, ~, <<, >>

&, |, ^, ~

&, |, ^, ~, <<, >>

&, |, ^, ~, <<, >>

Assignment Operators

=, +=, -=, *=, /=, %=, <<=, >>=, &=, ^=, |=

=, +=, -=, *=, /=, %=, <<=, >>=, &=, ^=, |=

=, +=, -=, *=, /=, %=, <<=, >>=, &=, ^=, |=

=, +=, -=, *=, /=, %=, //=, **=, &=, |=, ^=, <<=, >>=, :=

Conditional Operator

ใช้ if-else

ใช้ if-else

ใช้ if-else

คลิปนำเสนอ

C# Tutorials | Operators

Presentation (slides)

References

Keshav_786 และ Mithun Kumar. (2567). C# | Operators. จากhttps://www.geeksforgeeks.org/c-sharp-operators/

tutorialspoint. (2567). C - Operators. จาก https://www.tutorialspoint.com/cprogramming/c_operators.htm

tutorialspoint. (2567). C - Bitwise Operators in C จาก https://www.tutorialspoint.com/cprogramming/c_bitwise_operators.htm

w3schools. (2567). C# Operators. จาก https://www.w3schools.com/cs/cs_operators.php

w3schools. (2567). C# Assignment Operators. จาก https://www.w3schools.com/cs/cs_operators_assignment.php

w3schools. (2567). C# Comparison Operators. จาก https://www.w3schools.com/cs/cs_operators_comparison.php

w3schools. (2567). C# Logical Operators. จาก https://www.w3schools.com/cs/cs_operators_logical.php

javatpoint. (2567). C# operators. จาก https://www.javatpoint.com/csharp-operators

learn.microsoft. (2566). C# operators and expressions. จากhttps://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/

learn.microsoft. (2566). Arithmetic operators (C# reference). จากhttps://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/arithmetic-operators

learn.microsoft. (2566). Boolean logical operators - AND, OR, NOT, XOR. จากhttps://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/boolean-logical-operators

learn.microsoft. (2566). Bitwise and shift operators (C# reference). จาก https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/bitwise-and-shift-operators

learn.microsoft. (2566). Equality operators - test if two objects are equal or not. จาก https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/equality-operators

learn.microsoft. (2566). Comparison operators (C# reference). จาก https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/comparison-operators

w3schools. (2567). C Operators. จาก https://www.w3schools.com/c/c_operators.php

w3schools. (2567). C If ... Else. จาก https://www.w3schools.com/c/c_conditions.php

w3schools. (2567). Python Operators. จาก https://www.w3schools.com/python/python_operators.asp

w3schools. (2567). Python If ... Else. จาก https://www.w3schools.com/python/python_conditions.asp

w3schools. (2567). Java Operators. จาก https://www.w3schools.com/java/java_operators.asp

w3schools. (2567). Java If ... Else. จาก https://www.w3schools.com/java/java_conditions.asp

kartik. (2567). Bitwise Operators in Java. จาก https://www.geeksforgeeks.org/bitwise-operators-in-java/

Last updated