Understanding Conditional Statements in Dart: If vs Switch
While learning Dart, I explored conditional statements like the if statement and switch statement to handle conditions in a program. To apply this concept, I developed a program to calculate shipping costs based on the destination zone and the weight of the package.
Here’s a breakdown of what I learned and how I implemented it.
The Exercise
The program calculates shipping costs based on the following conditions:
If the destination zone is
'XYZ'
, the shipping cost is $5 per kilogram.If the destination zone is
'ABC'
, the shipping cost is $7 per kilogram.If the destination zone is
'PQR'
, the shipping cost is $10 per kilogram.If the destination zone is not
'XYZ'
,'ABC'
, or'PQR'
, an error message is displayed.
Initial Setup
I first created two variables to hold the destination zone and the weight of the package:
String destinationZone = “PQR”;
double weightInKg = 6;
I also initialized a double
variable for the cost and set it to 0
initially:
double cost = 0;
Using the If Statement
I began with an if statement to handle the conditions:
if (destinationZone == “PQR”) {
cost = weightInKg * 10;
} else if (destinationZone == “XYZ”) {
cost = weightInKg * 5;
} else if (destinationZone == “ABC”) {
cost = weightInKg * 7;
} else {
print(“Shipping Unavailable”);
return;
}
print(“Shipping Cost is $cost”);
Using the Switch Statement
Next, I implemented the same logic using the switch statement:
switch (destinationZone) {
case “PQR”:
print(‘Shipping Cost is ${weightInKg * 10}’);
break;
case “XYZ”:
print(‘Shipping Cost is ${weightInKg * 5}’);
break;
case “ABC”:
print(‘Shipping Cost is ${weightInKg * 7}’);
break;
default:
print(“Shipping Unavailable”);
}
Results
When I tested the program with the following values:
Output for both approaches:
If statement result:
Shipping Cost is 60.0
Switch statement result:
Shipping Cost is 60.0
Both statements worked as expected!
Testing Error Handling
To ensure the program handles invalid inputs, I changed the destinationZone
to an invalid value:
String destinationZone = “PPK”;
Results:
If statement result: The
else
block executed →Shipping Unavailable
.Switch statement result: The
default
block executed →Shipping Unavailable
.
Both methods successfully displayed the appropriate error message when an invalid input was provided.
Key Takeaways
The if statement works well for simple conditions and offers greater flexibility.
The switch statement is cleaner and more readable when dealing with a fixed set of values.
Always test for edge cases, such as invalid inputs, to ensure the program handles errors gracefully.
Conclusion
This exercise helped me understand how to implement conditional statements effectively in Dart. Both if statements and switch statements are powerful tools for decision-making in any program.
Code Recap
Here’s the complete code for reference:
void main() {
String destinationZone = “PQR”;
double weightInKg = 6;
double cost = 0;
// If statement
if (destinationZone == “PQR”) {
cost = weightInKg * 10;
} else if (destinationZone == “XYZ”) {
cost = weightInKg * 5;
} else if (destinationZone == “ABC”) {
cost = weightInKg * 7;
} else {
print(“Shipping Unavailable”);
return;
}
print(“Shipping Cost is $cost”);
// Switch statement
switch (destinationZone) {
case “PQR”:
print(‘Shipping Cost is ${weightInKg * 10}’);
break;
case “XYZ”:
print(‘Shipping Cost is ${weightInKg * 5}’);
break;
case “ABC”:
print(‘Shipping Cost is ${weightInKg * 7}’);
break;
default:
print(“Shipping Unavailable”);
}
}
Share Your Thoughts
What are your experiences with conditional statements in Dart? Which approach do you prefer for handling conditions: if statements or switch statements? Let me know in the comments!