Operators(Part -7)
Logical Operators
Logical Operators
25. Logical Operators ဆိုတာဘာလဲ
Logical Operators တွေကို Condition နှစ်ခု သို့မဟုတ် နှစ်ခုထက်ပိုပြီး ပေါင်းစပ်စစ်ဆေးဖို့ အသုံးပြုပါတယ်။
| Operator | အမည် | အဓိပ္ပာယ် |
|---|---|---|
&& | Logical AND | Condition အားလုံးမှန်ရမယ် |
|| | Logical OR | Condition အနည်းဆုံးတစ်ခုမှန်ရမယ် |
! | Logical NOT | Result ကို ဆန့်ကျင်ဘက်ပြောင်းမယ် |
26. Logical AND Operator (&&)
&& Operator ကို အသုံးပြုတဲ့အခါ Condition အားလုံး true ဖြစ်မှ Result က true ဖြစ်ပါတယ်။
int age = 18; bool hasStudentCard = true; bool canRegister = age >= 16 && hasStudentCard; Console.WriteLine(canRegister);
Output—
True
ဒီ Condition မှာ—
-
age >= 16ကtrue -
hasStudentCardကtrue
ဖြစ်တဲ့အတွက် Result က true ဖြစ်ပါတယ်။
AND Truth Table—
| Condition 1 | Condition 2 | Result |
|---|---|---|
true | true | true |
true | false | false |
false | true | false |
false | false | false |
27. Range စစ်ဆေးခြင်း
အမှတ်တစ်ခုက 0 မှ 100 ကြား ရှိ၊ မရှိ စစ်ဆေးနိုင်ပါတယ်။
int mark = 85; bool isValidMark = mark >= 0 && mark <= 100; Console.WriteLine(isValidMark);
Output—
True
mark က 0 နဲ့အထက်ဖြစ်ရမယ့်အပြင် 100 နဲ့အောက်လည်း ဖြစ်ရပါတယ်။
28. Logical OR Operator (||)
|| Operator ကို အသုံးပြုတဲ့အခါ Condition များထဲက အနည်းဆုံးတစ်ခု true ဖြစ်ရင် Result က true ဖြစ်ပါတယ်။
bool isOwner = false; bool isAdmin = true; bool canAccess = isOwner || isAdmin; Console.WriteLine(canAccess);
Output—
True
isOwner က false ဖြစ်ပေမယ့် isAdmin က true ဖြစ်တဲ့အတွက် Access ရပါတယ်။
OR Truth Table—
| Condition 1 | Condition 2 | Result |
|---|---|---|
true | true | true |
true | false | true |
false | true | true |
false | false | false |
29. Logical NOT Operator (!)
! Operator က Boolean Value ကို ဆန့်ကျင်ဘက်ပြောင်းပေးပါတယ်။
bool isActive = true; Console.WriteLine(!isActive);
Output—
False
နောက်ထပ်နမူနာ—
bool isDeleted = false; bool isAvailable = !isDeleted; Console.WriteLine(isAvailable);
Output—
True
isDeleted က false ဖြစ်တဲ့အတွက် !isDeleted က true ဖြစ်ပါတယ်။
30. Logical Operators ပေါင်းစပ်ခြင်း
int age = 18; bool hasPermission = true; bool isBlocked = false; bool canJoin = age >= 16 && hasPermission && !isBlocked; Console.WriteLine(canJoin);
Output—
True
ဒီ Condition မှာ—
-
အသက်
16နှစ်နဲ့အထက် ဖြစ်ရမယ်။ - Permission ရှိရမယ်။
- Block လုပ်ထားတာ မဖြစ်ရပါဘူး။
Condition သုံးခုလုံးမှန်တဲ့အတွက် Result က true ဖြစ်ပါတယ်။