ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
if statement ကို လမ်းဆုံမှာ ရှိတဲ့ ဆိုင်းဘုတ်တစ်ခုလို မြင်ကြည့်ပါ။ ဆိုင်းဘုတ်ပေါ်မှာ "မိုးရွာနေလား" လို yes/no မေးခွန်းတစ်ခု ရှိပြီး အဖြေက yes ဆိုရင် ဘယ်ဘက်လမ်းကို၊ no ဆိုရင် ညာဘက်လမ်းကို သွားပါတယ်။ Dart က ဆိုင်းဘုတ်ဆီ ရောက်တဲ့အခါ မေးခွန်းကို ဖြေပြီး လမ်းတစ်ခုတည်းကိုပဲ ဆက်လျှောက်ပါတယ်။
if / else if / else chain ကတော့ ဆိုင်းဘုတ်တွေ တန်းစီနေတာနဲ့ တူပါတယ်။ Dart က အပေါ်ဆုံးကနေ စဖတ်ပြီး အဖြေက yes ဖြစ်တဲ့ ပထမဆုံး လမ်းကို ယူပါတယ်၊ အဲဒီနောက်က ဆိုင်းဘုတ်တွေကိုတော့ ဂရုမစိုက်တော့ပါဘူး။ နောက်ဆုံးက else ကတော့ မေးခွန်းတိုင်းကို no လို့ ဖြေခဲ့သူအားလုံးအတွက် လမ်းပါ။ Beginner တွေ မှတ်ထားရမယ့် အချက်ကတော့ Dart က အဖြေစစ်စစ် (true ဒါမှမဟုတ် false) ကိုပဲ လက်ခံတာပါ — yes လို ခံစားရုံ ဖြစ်တဲ့ number ဒါမှမဟုတ် စာသားကို လက်မခံပါဘူး။
Problem → Why → Solution
ပြဿနာ: ၉၅ မှတ် ရတဲ့ ကျောင်းသားက C ရသွားတယ်
ဆရာက grading function တစ်ခု ရေးခိုင်းတယ် ဆိုပါစို့ — ၉၀ နဲ့အထက် A၊ ၈၀ နဲ့အထက် B၊ ၆၀ နဲ့အထက် C ပါ။ သင်က စာကြောင်းထဲက အစဉ်အတိုင်း စစ်ဆေးချက် သုံးခု ရေးပြီး ၉၅ မှတ်နဲ့ run လိုက်တော့ C ထွက်လာပါတယ်။ Crash မဖြစ်ဘူး၊ မျဉ်းနီလည်း မပေါ်ဘူး၊ ဒါကြောင့် ကျောင်းသားက တိုင်မှပဲ bug ကို သိရမှာပါ။
အမှားက အစဉ်မှာပါ။ ၉၀ နဲ့အထက် မှတ်တိုင်းက ၆၀ နဲ့အထက်လည်း ဖြစ်နေလို့ ပထမ စစ်ဆေးချက်က မြင့်တဲ့ grade အားလုံးကို တခြား စစ်ဆေးချက်တွေ မကြည့်ခင် ယူသွားပါတယ်။ Code က ဥပဒေနဲ့ ညီတဲ့ code မို့ Dart က error မပေးပါဘူး၊ ကိုယ်ရည်ရွယ်တဲ့ အဓိပ္ပာယ်နဲ့ မတူတာပဲ ရှိပါတယ်။
String grade(int score) {
if (score >= 60) {
return 'C';
} else if (score >= 80) {
return 'B';
} else if (score >= 90) {
return 'A';
}
return 'retry';
}
void main() {
print(grade(95)); // prints C: the first condition already matched
}ဘာကြောင့် ဒီလို ဖြစ်တာလဲ
Dart က if / else if chain ရဲ့ condition တွေကို အပေါ်ကနေ အောက်ကို စစ်ပြီး condition က true ဖြစ်တဲ့ ပထမဆုံး block ကိုပဲ run ပါတယ်။ အရင်ဟာနဲ့ ကိုက်သွားတဲ့ score အတွက် နောက် condition တွေကို ဘယ်တော့မှ မရောက်တာမို့ အစဉ်ဆိုတာ logic ရဲ့ အစိတ်အပိုင်းပါ။ အကြမ်းဆုံး၊ တင်းကျပ်ဆုံး test (>= 90) ကို အရင်ထားပြီး ပိုကျယ်တဲ့ test (>= 60) နဲ့ နောက်ဆုံး else က ကျန်တာတွေကို ဖမ်းခိုင်းပါ။
Core concept: chain၊ bool condition နဲ့ short-circuit
if statement က condition ကို တွက်ပြီး true ဖြစ်မှသာ ကိုယ့် block ကို run ပါတယ်။ နောက်ထပ် မေးခွန်းတွေအတွက် else if ကို ထည့်ပြီး ကျန်တာအားလုံးအတွက် else ကို ထည့်ပါတယ်။ Chain က true ဖြစ်တဲ့ ပထမ condition မှာ ရပ်ပါတယ် — နောက် branch တွေရဲ့ condition က true ဖြစ်နိုင်ရင်တောင် ကျော်သွားပါတယ်၊ ဒါကြောင့် စစ်ဆေးချက်တွေရဲ့ အစဉ်က logic ရဲ့ အစိတ်အပိုင်း ဖြစ်ပါတယ်။ Statement တစ်ကြောင်းတည်းဆိုရင် Dart က တွန့်ကွင်း {} မလိုပေမယ့် ရေးထားပါ၊ တွန့်ကွင်း ပျောက်တာက ဒုတိယ statement ကို branch မှားထဲ ရောက်သွားစေတတ်တဲ့ အဖြစ်များတဲ့ အမှားပါ။
How Dart walks the chain for score = 82
---------------------------------------
score >= 90 ? --yes--> 'A' (chain ends)
| no
v
score >= 80 ? --yes--> 'B' (chain ends) <-- 82 stops here
| no
v
score >= 60 ? --yes--> 'C' (chain ends)
| no
v
else ------------------> 'retry'Condition ရဲ့ type က bool ဖြစ်ရပါမယ်။ Dart မှာ truthiness မရှိပါဘူး — int, String, List နဲ့ null တွေကို true ဒါမှမဟုတ် false အဖြစ် တိတ်တိတ်လေး မပြောင်းပေးပါဘူး။ JavaScript ဒါမှမဟုတ် Python မှာ အလုပ်လုပ်တဲ့ if (count), if (name) လို code တွေက Dart မှာ compile error ပါ — "A value of type 'int' can't be assigned to a variable of type 'bool'." ပါ။ ဒါကြောင့် တကယ်မေးချင်တဲ့ မေးခွန်းကို ရေးပါ: count > 0, name.isNotEmpty, items.isNotEmpty, value != null။ တစ်ရက်လောက်တော့ တင်းကျပ်လွန်းသလို ထင်ရပေမယ့် 0 ဒါမှမဟုတ် empty string က မှန်ကန်တဲ့ data ဖြစ်နေတဲ့ bug တွေကနေ ကာကွယ်ပေးပါတယ်။
Condition တွေကို && (and), || (or) နဲ့ ! (not) နဲ့ ပေါင်းနိုင်ပါတယ်။ && နဲ့ || နှစ်ခုလုံး short-circuit လုပ်ပါတယ် — ဘယ်ဘက်က အဖြေကို ဆုံးဖြတ်ပြီးသားဆိုရင် ညာဘက်ကို မတွက်တော့ပါဘူး၊ ဒါကြောင့် local variable တစ်ခုအတွက် user != null && user.isActive လို စစ်ဆေးချက်က လုံခြုံပါတယ်။ && က || ထက် ပိုမြန်မြန် bind လုပ်ပြီး နှစ်ခုလုံးက conditional operator ထက် ပိုမြန်ပါတယ်၊ ဒါကြောင့် isMember && price >= 30 ? 5 : 0 ဆိုတာ (isMember && price >= 30) ? 5 : 0 လို့ ဆိုလိုပါတယ်။ ဖတ်သူအတွက် precedence မရှင်းရင် ကွင်းစကွင်းပိတ် ထည့်ပါ၊ ဒါမှမဟုတ် condition ကို final isEligible = ...; လို variable နာမည်ပေးပါ။
နောက်ဆုံးအနေနဲ့ if က statement ဖြစ်ပြီး value မဟုတ်ဘူးဆိုတာ မှတ်ထားပါ။ တန်ဖိုးနှစ်ခုကြားက တစ်ခုကို ရွေးချင်ရုံဆိုရင် conditional expression condition ? a : b က ပိုတိုပါတယ်။ တည်ငြိမ်တဲ့ case အများကြီးအတွက်ကတော့ အောက်မှာ ပြမယ့် switch expression က else-if လှေကားရှည်ထက် ပိုဖတ်ကောင်းပါတယ်။
- ပထမ condition (bool) ကို စစ်
- true ဆို အဲဒီ block run၊ ကျန်တာ ကျော်
- false ဆို နောက် condition ကို ဆက်စစ်
- ဘာမှ မကိုက်ရင် else ကို run (ရှိရင်)
အစဉ်ဆိုတာ logic ပါပဲ — true ဖြစ်တဲ့ ပထမဆုံး condition က အနိုင်ရလို့ တင်းကျပ်တဲ့ test ကို အပေါ်ဆုံးမှာ ထားပါ။
အထွေထွေ ပုံစံ
if (condition) {
// runs when condition is true
} else if (otherCondition) {
// runs when only the second one is true
} else {
// runs when nothing above matched
}
final result = condition ? valueIfTrue : valueIfFalse;Condition ကို ကွင်းစကွင်းပိတ်ထဲ ထည့်ရပြီး bool ဖြစ်ရပါမယ်။ else if နဲ့ else က မဖြစ်မနေ မလိုပါဘူး၊ else if ကို လိုသလောက် ဆက်နိုင်ပါတယ်။ နောက်ဆုံး line က conditional operator ဖြစ်ပြီး statement run တာ မဟုတ်ဘဲ value တစ်ခု ထုတ်ပေးတဲ့ expression ပုံစံပါ။
Example: grade function နဲ့ discount rule
ဒီ program မှာ ဆုံးဖြတ်ချက်ချတဲ့ function သေးသေးလေး နှစ်ခု ပါပါတယ်။ grade() က မမှန်တဲ့ score ကို အရင်စစ်ပြီး တင်းကျပ်ဆုံး grade ကနေ ပျော့ဆုံး grade ဆီ အောက်ကို ဆင်းပါတယ်။ discount() ကတော့ conditional expression ထဲမှာ bool test နှစ်ခုကို && နဲ့ ပေါင်းထားပါတယ်။ Loop က grade() ကို ၉၀၊ ၈၉၊ ၆၀၊ ၅၉ လို နယ်နိမိတ်တန်ဖိုးတွေ ပို့ပါတယ် — off-by-one comparison အမှားတွေက အဲဒီနေရာမှာ ပုန်းနေတတ်လို့ပါ။ Run မလုပ်ခင် output ကို ကြိုခန့်မှန်းကြည့်ပါ။
String grade(int score) {
if (score < 0 || score > 100) {
return 'invalid';
} else if (score >= 90) {
return 'A';
} else if (score >= 80) {
return 'B';
} else if (score >= 60) {
return 'C';
} else {
return 'retry';
}
}
int discount(int price, bool isMember) {
return isMember && price >= 30 ? 5 : 0;
}
void main() {
for (final score in [95, 90, 89, 60, 59, 101]) {
print('$score -> ${grade(score)}');
}
print('member, price 40: ${discount(40, true)} off');
print('guest, price 40: ${discount(40, false)} off');
print('member, price 20: ${discount(20, true)} off');
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Validate first
if (score < 0 || score > 100) {|| က ဘယ်ဘက် ဒါမှမဟုတ် ညာဘက် တစ်ဖက်ဖက် true ဆို true ပါ၊ ဒါကြောင့် -3 ဒါမှမဟုတ် 101 လို မဖြစ်နိုင်တဲ့ score တွေကို grade မစဉ်းစားခင် ပယ်ချပါတယ်။ ရှားတဲ့ invalid case ကို အရင်ထားတော့ နောက် condition တွေက score က range အတွင်းမှာ ရှိတယ်လို့ ယူဆလို့ ရပါတယ်။
- Strictest grade first
} else if (score >= 90) {၉၀ နဲ့အထက် score တွေက ဒီမှာ ရပ်ပြီး 'A' ကို return ပြန်ပါတယ်။ ဒီ test က >= 80 ရဲ့ အရင်မှာ ရှိလို့ ၉၅ က အောက်က စစ်ဆေးချက်တွေဆီ မရောက်ပါဘူး။ အစဉ်ကို ပြောင်းလိုက်ရင် ၆၀ ကျော်တဲ့ score အားလုံး C ဖြစ်သွားပါလိမ့်မယ်။
- Catch-all
} else {၀ ကနေ ၅၉ အထိက else ကို ရောက်ပါတယ်။ else မှာ condition မရေးရပါဘူး၊ ဘာလို့လဲဆိုတော့ else ရဲ့ အဓိပ္ပာယ်က "အရင် condition တွေနဲ့ မကိုက်တဲ့ အားလုံး" ပါ။
- Boundary values
for (final score in [95, 90, 89, 60, 59, 101]) {စမ်းသပ်တဲ့ တန်ဖိုးတွေကို နယ်နိမိတ်တစ်ဝိုက်မှာ ရွေးထားပါတယ်။ 90 -> A နဲ့ 89 -> B ဆိုတာ >= က နယ်နိမိတ်ကို ထည့်ပေါင်းတာ သက်သေပြပြီး 60 -> C နဲ့ 59 -> retry ကလည်း နောက်နယ်နိမိတ်ကို သက်သေပြပါတယ်။
- Two tests in one
return isMember && price >= 30 ? 5 : 0;&& က ?: ထက် အရင် တွက်လို့ ဒါဟာ (isMember && price >= 30) ? 5 : 0 ပါ။ Member ဖြစ်ပြီး ဈေးက 30 နဲ့အထက်ဆိုမှ 5 ကို return ပြန်ပါတယ်၊ နှစ်ခုထဲက တစ်ခု လွဲရင် 0 ပါ။
95 -> A
90 -> A
89 -> B
60 -> C
59 -> retry
101 -> invalid
member, price 40: 5 off
guest, price 40: 0 off
member, price 20: 0 offတန်ဖိုးတစ်ခု ရွေးခြင်း: ?: နဲ့ switch expression
if အများစုက တန်ဖိုးတစ်ခု ရွေးဖို့ပဲ ရှိနေတာပါ။ Conditional operator က ရလဒ်နှစ်ခုကို line တစ်ကြောင်းတည်းနဲ့ ကိုင်တွယ်ပါတယ်: final label = price >= 30 ? 'big' : 'small';။ ရလဒ်နှစ်ခုထက် များရင်တော့ ?: ကို အထပ်ထပ် ထည့်ထားတာက ဖတ်မရတော့ပါဘူး၊ Dart 3 ရဲ့ switch expression က table တစ်ခုလို ဖတ်ရပါတယ်။ Case တစ်ခုစီက pattern => value ပုံစံဖြစ်ပြီး else-if chain လိုပဲ အပေါ်ကနေ အောက်ကို စမ်းပါတယ်၊ expression တစ်ခုလုံးက exhaustive ဖြစ်ရလို့ int အတွက် နောက်ဆုံး _ (wildcard) လိုပါတယ်။ ရိုးရိုး pattern နဲ့ မဖော်ပြနိုင်တဲ့ condition တွေအတွက် case တစ်ခုမှာ when guard ထည့်လို့ ရပါတယ်။
String describeTemp(int celsius) {
return switch (celsius) {
< 0 => 'freezing',
< 15 => 'cold',
< 28 => 'pleasant',
final t when t > 40 => 'dangerous ($t C)',
_ => 'hot',
};
}
void main() {
for (final t in [-5, 10, 20, 33, 45]) {
print('$t -> ${describeTemp(t)}');
}
final price = 40;
final label = price >= 30 ? 'big' : 'small';
print(label);
}| ကိရိယာ | ဘယ်အတွက် အကောင်းဆုံးလဲ | သတိထားရန် |
|---|---|---|
| if / else if / else | Branch တစ်ခုစီမှာ statement အမျိုးမျိုး run ချင်ရင် | Condition အစဉ်က အရေးကြီးတယ်၊ တွန့်ကွင်း ထည့်ပါ |
| condition ? a : b | တန်ဖိုးနှစ်ခုကြား တစ်ခု ရွေးချင်ရင် | တစ်ထပ်ထက် ပိုမထပ်ပါနဲ့ |
| switch expression | တန်ဖိုးတစ်ခုပေါ်က case အများကြီးနဲ့ value ပြန်ချင်ရင် | Exhaustive ဖြစ်ရမယ်၊ များသောအားဖြင့် နောက်ဆုံး _ လိုတယ် |
if ထဲက Dart 3 pattern နဲ့ collection if
Dart 3 မှာ if က value တစ်ခုရဲ့ ပုံသဏ္ဌာန်ကို စစ်ပြီး data ကို တစ်ဆင့်တည်းနဲ့ ထုတ်ယူလို့ ရပါတယ်။ if (data case {'name': String name, 'age': int age}) က data ဟာ အဲဒီ key နှစ်ခုပါတဲ့ map ဖြစ်ပြီး value တွေက အဲဒီ type တွေ ဟုတ်မဟုတ် စစ်ပါတယ်၊ ကိုက်ရင် block ထဲမှာ name နဲ့ age ကို တန်း သုံးလို့ရပြီး မကိုက်ရင် else branch ကို run ပါတယ်။ Decode လုပ်ထားတဲ့ JSON ကို ကိုင်တွယ်တဲ့အခါ containsKey နဲ့ is စစ်ဆေးချက် အများကြီးကို အစားထိုးပေးပါတယ်။ အလားတူ feature တစ်ခုက list, set, map literal တွေထဲမှာ အလုပ်လုပ်ပါတယ် — if element က condition true ဖြစ်မှ item တစ်ခု ထည့်ပေးလို့ role ပေါ်မူတည်တဲ့ menu ကို ဒီနည်းနဲ့ ဆောက်ပါတယ်။
ရိုးရိုး type test တွေလည်း အလုပ်လုပ်ပါတယ်: if (value is int && value > 40) ပြီးရင် variable ကို int အဖြစ် promote လုပ်ပေးလို့ value + 1 မှာ cast မလိုပါဘူး။
void main() {
final isAdmin = true;
final menu = [
'Home',
'Profile',
if (isAdmin) 'Admin panel',
];
print(menu);
Object? data = {'name': 'Su', 'age': 31};
if (data case {'name': String name, 'age': int age}) {
print('$name is $age');
} else {
print('unexpected shape');
}
data = {'name': 'Su', 'age': 'thirty'};
if (data case {'name': String name, 'age': int age}) {
print('$name is $age');
} else {
print('unexpected shape');
}
Object? input = 42;
if (input is int && input > 40) {
print(input + 1);
}
}လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
သင်တည်ဆောက်တဲ့ feature တိုင်းလိုလို ဆုံးဖြတ်ချက်တစ်ခုနဲ့ စပါတယ်။ Login screen က dashboard ပြမလား sign-in form ပြမလား ဆုံးဖြတ်ပါတယ်၊ checkout က coupon မှန်ပြီး cart က အနည်းဆုံးပမာဏ ကျော်မှ discount ပေးပါတယ်၊ API handler က ရှာတွေ့တာပေါ်မူတည်ပြီး 404, 403 ဒါမှမဟုတ် 200 ပြန်ပါတယ်။ Validation code အများစုက guard clause တွေပါ — email ဗလာဆိုရင် error ကို ချက်ချင်း return ပြန်၊ password တိုလွန်းရင် တခြား error ပြန်၊ မှန်တဲ့လမ်းကြောင်းကပဲ function ရဲ့ အဆုံးကို ရောက်ပါတယ်။
Flutter မှာ collection if က နေရာတိုင်းမှာ ရှိပါတယ်: widget တွေပါတဲ့ column ထဲမှာ if (isAdmin) const AdminBanner() ကို list ထဲ တိုက်ရိုက် ထည့်လို့ ရပါတယ်။ Shipping fee, အခွန်အဆင့်၊ permission စစ်ဆေးချက်လို business rule တွေက များသောအားဖြင့် chain ဒါမှမဟုတ် switch expression ဖြစ်လာပြီး team တွေက ဒီသင်ခန်းစာထဲက loop လိုပဲ နယ်နိမိတ်တန်ဖိုးတွေနဲ့ test လုပ်ကြပါတယ် — bug တွေက ၇၅ မှာ မဟုတ်ဘဲ ၅၉ နဲ့ ၆၀ ကြားမှာ ပုန်းနေလို့ပါ။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. အကျယ်ဆုံး condition ကို အရင်ထားတာ
ပြဿနာမှာ မြင်ခဲ့သလို if (score >= 60) ကို if (score >= 90) ရဲ့ အရင်ထားရင် မြင့်တဲ့ score အားလုံးကို ဖမ်းယူသွားပါတယ်။ Chain ကို အပေါ်ကနေ အောက် ဖတ်ပြီး branch တစ်ခုချင်းစီမှာ "နောက် branch ကို သင့်တဲ့ value က ဒီမှာ ဖမ်းခံရနိုင်လား" လို့ မေးပါ။ ဖမ်းခံရနိုင်ရင် တင်းကျပ်တဲ့ test ကို အပေါ်ရွှေ့ပါ ဒါမှမဟုတ် && နဲ့ အပေါ်ကန့်သတ်ချက် ထည့်ပါ။
2. bool မဟုတ်တာကို condition အဖြစ် သုံးတာ
JavaScript အကျင့်အတိုင်း if (items.length) ဒါမှမဟုတ် if (name) လို့ ရေးရင် "A value of type 'int' can't be assigned to a variable of type 'bool'." နဲ့ compile မဖြစ်ပါဘူး။ မေးချင်တာကို ရှင်းရှင်းရေးပါ: items.isNotEmpty, name.isNotEmpty ဒါမှမဟုတ် count > 0။ သတိပြုရန် — bool variable နဲ့ if (done = true) ကတော့ compile ဖြစ်ပါတယ်၊ true ကို assign လုပ်လိုက်လို့ branch က အမြဲ run ပါတယ်၊ ဒါကြောင့် == နဲ့ နှိုင်းယှဉ်ပါ ဒါမှမဟုတ် if (done) လို့ပဲ ရေးပါ။
3. Catch-all မပါတဲ့ switch expression
int, String လို တန်ဖိုးအများကြီးရှိတဲ့ အပေါ် switch expression က ဖြစ်နိုင်ခြေအားလုံးကို ဖုံးရပါမယ်။ နောက်ဆုံး _ => ... case မပါရင် Dart က "The type 'int' is not exhaustively matched by the switch cases since it doesn't match 'int()'." ဆိုပြီး compile မလုပ်ပေးပါဘူး။ ကျန်တာတွေအတွက် _ => ... ထည့်ပါ၊ ဒါမှမဟုတ် value ကို သုံးချင်ရင် final other => ... လို case ထည့်ပါ။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- မမှန်တဲ့ input အတွက် early return (guard clause) သုံးပါ၊ ဒါဆို အဓိက လမ်းကြောင်းက အထပ်ထပ် else block တွေအောက် မမြုပ်ပါဘူး။
- ရှုပ်တဲ့ condition ကို final canCheckout = cart.isNotEmpty && hasAddress; လို နာမည်ပေးပါ၊ သုံးကြောင်းရှည် if ထက် ဖတ်ကောင်းပါတယ်။
- တန်ဖိုးတစ်ခုပေါ် case ၄ ခုနဲ့အထက်ရှိရင် else-if လှေကားရှည်ထက် switch expression ကို ရွေးပါ။
- နှိုင်းယှဉ်ချက်တိုင်းရဲ့ နှစ်ဖက်စလုံး နယ်နိမိတ်တန်ဖိုးတွေကို test လုပ်ပါ။
Mini challenge
Mini challenge: ပြတိုက် ticket ဈေးနှုန်း
ပြတိုက်တစ်ခုအတွက် အသက်ကနေ အုပ်စုခွဲပေးမယ့် function တစ်ခုနဲ့ အုပ်စုနဲ့ ကျောင်းသားဟုတ်မဟုတ်ကနေ ticket ဈေး တွက်ပေးမယ့် function တစ်ခု ရေးပါ။ အုပ်စုခွဲတာကို if / else if chain နဲ့၊ ဈေးတွက်တာကို switch expression နဲ့ ရေးပါ။
- ageGroup(age) — 0 အောက်ဆို 'invalid'၊ 5 အောက် 'infant'၊ 18 အောက် 'youth'၊ 65 အောက် 'adult'၊ ကျန်တာ 'senior'
- ticketPrice(group, isStudent) — infant 0၊ youth 5၊ senior 4၊ adult 10 (ကျောင်းသားဆို 7)၊ တခြားဆို -1
- show(age, isStudent) က "age 30 (adult, student): 7 units" လို line တစ်ကြောင်း print ထုတ်ရမယ်၊ ဈေးက -1 ဆို "no ticket" လို့ ပြရမယ်
- main() ထဲမှာ အသက် -1, 3, 16, 30 (ကျောင်းသား), 30 နဲ့ 70 အတွက် show() ကို ခေါ်ပါ
String ageGroup(int age) {
// TODO: 'invalid' below 0, 'infant' below 5, 'youth' below 18,
// 'adult' below 65, otherwise 'senior'
throw UnimplementedError();
}
int ticketPrice(String group, bool isStudent) {
// TODO: switch expression: infant 0, youth 5, senior 4,
// adult 10 (7 for students), anything else -1
throw UnimplementedError();
}
void show(int age, bool isStudent) {
// TODO: print one line such as "age 30 (adult, student): 7 units"
}
void main() {
// TODO: call show() for ages -1, 3, 16, 30 (student), 30 and 70
}ရလဒ်: Output က ဒီလို ထွက်ရပါမယ်: age -1 (invalid): no ticket age 3 (infant): 0 units age 16 (youth): 5 units age 30 (adult, student): 7 units age 30 (adult): 10 units age 70 (senior): 4 units
အရိပ်အမြွက် ကြည့်ရန်
ageGroup မှာ အပေါ်ကနေ အောက် အသေးဆုံးကန့်သတ်ချက်ကနေ စပါ — age < 0 ပြီးရင် age < 5၊ age < 18 ... ဆိုတော့ branch တစ်ခုစီက အရင် branch တွေ မကိုက်ဘူးလို့ ယူဆလို့ရပါတယ်။ ticketPrice မှာ switch (group) { 'infant' => 0, ... _ => -1 } ပုံစံနဲ့ ရေးပြီး adult case ထဲမှာ ?: သုံးပါ။
အဖြေ ကြည့်ရန်
String ageGroup(int age) {
if (age < 0) {
return 'invalid';
} else if (age < 5) {
return 'infant';
} else if (age < 18) {
return 'youth';
} else if (age < 65) {
return 'adult';
} else {
return 'senior';
}
}
int ticketPrice(String group, bool isStudent) {
return switch (group) {
'infant' => 0,
'youth' => 5,
'senior' => 4,
'adult' => isStudent ? 7 : 10,
_ => -1,
};
}
void show(int age, bool isStudent) {
final group = ageGroup(age);
final price = ticketPrice(group, isStudent);
final who = isStudent ? '$group, student' : group;
final text = price < 0 ? 'no ticket' : '$price units';
print('age $age ($who): $text');
}
void main() {
show(-1, false);
show(3, false);
show(16, false);
show(30, true);
show(30, false);
show(70, false);
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- if / else if / else chain က condition true ဖြစ်တဲ့ ပထမ block ကိုပဲ run ပါတယ်၊ ဒါကြောင့် တင်းကျပ်ဆုံး test ကို အရင်ထားပါ။
- Condition က bool ဖြစ်ရပါမယ် — Dart မှာ truthiness မရှိလို့ if (count) အစား count > 0 ဒါမှမဟုတ် list.isNotEmpty လို့ ရေးပါ။
- && နဲ့ || က short-circuit လုပ်ပြီး ?: ဒါမှမဟုတ် switch expression က if statement အပြည့်အစုံ မရေးဘဲ value ရွေးပေးပါတယ်။
- Dart 3 မှာ shape စစ်ဖို့ if-case pattern, switch expression နဲ့ list တည်ဆောက်ဖို့ collection if တွေ ပါလာပါတယ်။
Dart language overview — Dart