Greetings, fellow Dart Astronaut! Today, we are going to explore an integral part of programming: conditional statements. Such statements determine the course of our program. Are you prepared to delve into the if-else
, switch case
, and the nimble yet powerful ternary operator
in Dart? Fasten your seatbelt!
The if
and if-else
blocks in Dart are structured as follows:
Dart1if (condition) { 2 // action if the condition is true 3} 4 5// and 6 7if (condition) { 8 // action if the condition is true 9} else { 10 // action if the condition is false 11}
When the provided condition
is true, the if
block gets executed. On the other hand, when the condition
is false, the else
block, if present, gets the go-ahead.
The if
statement in Dart guides the computer to perform specific actions under particular conditions. Let's simulate an encounter with a planet offering breathable air:
Dart1int oxygenLevel = 78; // The oxygen level on the planet 2 3if (oxygenLevel > 20) { 4 print("Planet has breathable air!"); // Oxygen level is fine 5} else { 6 print("Oxygen level is too low!"); // Oxygen level is inadequate 7} 8// The code prints: Planet has breathable air!
In this example, the statement if (oxygenLevel > 20)
verifies whether the oxygen level surpasses 20. If the condition holds true
, it prints: "Planet has breathable air!"
. Conversely, if it is false
, else
guides us to an alternate command that prints: "Oxygen level too low!"
.
To handle multiple conditions, we can employ else if
:
Dart1int oxygenLevel = 58; 2if (oxygenLevel > 70) { 3 print("Excellent Oxygen level!"); 4} else if (oxygenLevel > 50) { 5 print("Oxygen level is acceptable."); 6} else { 7 print("Oxygen level is too low!"); 8} 9// The code prints: Oxygen level is acceptable.
The else if
keyword offers multiple paths for the program to follow until a fitting one is located. Once a condition is met, the program disregards all remaining else if
conditions.
The switch
statement in Dart enables us to deal with multiple outcomes based on variable values. Let's envision preparing visits to various planets:
Dart1int planetNumber = 3; // The chosen planet number 2 3switch (planetNumber) { 4 case 1: 5 print("Gearing up for Planet 1!"); // Case for planet 1 6 break; 7 case 2: 8 print("Preparing for Planet 2!"); // Case for planet 2 9 break; 10 case 3: 11 print("Get ready for Planet 3!"); // Case for planet 3 12 break; 13 default: 14 print("Resting at the spaceship."); // Default case 15} 16// The code prints: Get ready for Planet 3!
In this code snippet, the case
statement corresponds to a planetary number. The value of the default
case signifies that no specific planet has been selected.
Take note of the break
statement after each case
operation. It is necessary to ensure the appropriate flow of command execution — the switch
case execution concludes once we've achieved the desired outcome.
Dart's ternary operator
equates to a condensed if-else
statement compacted into a single line, making it ideal for basic condition checks:
Dart1// Perform 'action1' if the condition is true, or do 'action2' if false 2result = condition ? action1 : action2;
Let's imagine we are verifying the presence of a signal before landing:
Dart1bool detection = true; // A signal detection 2 3String message = detection ? "Signal detected, safe to land!" : "No signal, abort mission!"; 4print(message); // Prints: "Signal detected, safe to land!"
In the code above, message
receives the value "Signal detected, safe to land!"
because detection
is true
. Otherwise, it would be assigned "No signal, abort mission!"
.
Well done! You've successfully navigated Dart's conditional statements today. Continue practicing your newly acquired skills through our forthcoming exercises. Every step you take transforms you into a better navigator in Dart Space, preparing you for the subsequent stage of your cosmic journey. Stay tuned!