Cấu trúc rẽ nhánh (if-else) trong Dart
1. If statement
var age = 18;
if (age >= 18) {
print("Adult");
}2. If-else
if (age >= 18) {
print("Adult");
} else {
print("Minor");
}3. If-else if-else
var score = 85;
if (score >= 90) {
print("A");
} else if (score >= 80) {
print("B");
} else if (score >= 70) {
print("C");
} else {
print("F");
}4. Ternary Operator
var age = 20;
var status = age >= 18 ? "Adult" : "Minor";5. Logical Operators
var age = 25;
var hasLicense = true;
if (age >= 18 && hasLicense) {
print("Can drive");
}
if (age < 18 || !hasLicense) {
print("Cannot drive");
}📝 Tóm tắt
if-else if-else- Conditional branchingcondition ? a : b- Ternary operator&&,||,!- Logical operators
Last updated on