C++ Short Hand ဆိုရင် ရပြီလား
Short Hand If...Else (Ternary Operator)
၎င်းတွင် operand သုံးခုပါဝင်သောကြောင့် ternary operator ဟုခေါ်သော short-hand if else လည်း ရှိသည်။ ကုဒ်များစွာကို စာကြောင်းတစ်ကြောင်းတည်းဖြင့် အစားထိုးရန် ၎င်းကို အသုံးပြုနိုင်သည်။ ရိုးရှင်းသော if else statement များကို အစားထိုးရန် မကြာခဏ အသုံးပြုသည်-
အထားအသို
variable = (condition) ? expressionTrue :
expressionFalse;
စာရေးမယ့်အစား
ဥပမာ
int time = 20;
if (time < 18) {
cout << "Good
day.";
} else {
cout << "Good
evening.";
}
သင်ရိုးရှင်းစွာရေးနိုင်သည်-
ဥပမာ
int time = 20;
string result = (time < 18) ? "Good day." : "Good evening.";
cout << result;