If-else logic is a foundational concept in programming that allows you to execute different sections of code based on the evaluation of specific conditions. In Dart, the if
and else
statements enable you to introduce decision-making capabilities into your applications, making them responsive to various inputs and situations.
Structure of If-Else Logic
Basic If-Else Statement
The basic structure of an if-else statement consists of the if
condition followed by an optional else
block.
Syntax:
if (condition) {
// Code to execute if condition is true } else {
// Code to execute if condition is false }
Example Use Cases
Example 1: Age Verification
Let's create a simple program that checks if a user is eligible to vote based on their age.
Code:
void main() {
int age = 20; // Example age
if (age >= 18) {
print('You are eligible to vote.');
} else {
print('You are not eligible to vote.');
}
}
Code Explanation:
int age = 20;
: This line declares a variableage
and assigns it the value 20.if (age >= 18) {
: The program checks ifage
is greater than or equal to 18.- If the condition is true, the program executes
print('You are eligible to vote.');
. - If the condition is false, the
else
block executes, printingYou are not eligible to vote.
.
Example 2: Grading System
Now, let's implement a grading system that assigns a letter grade based on a numeric score.
Code:
void main() {
int score = 85; // Example score
if (score >= 90) {
print('Grade: A');
} else if (score >= 80) {
print('Grade: B');
} else if (score >= 70) {
print('Grade: C');
} else {
print('Grade: D');
}
}
Code Explanation:
int score = 85;
: This line declares a variablescore
and assigns it the value 85.- The program uses multiple
else if
statements to check the score:if (score >= 90) {
: Checks if the score is 90 or higher; if true, it prints "Grade: A".else if (score >= 80) {
: Checks if the score is 80 or higher; if true, it prints "Grade: B".else if (score >= 70) {
: Checks if the score is 70 or higher; if true, it prints "Grade: C".- If none of the conditions are met, the
else
block executes, printing "Grade: D".
Example 3: Temperature Check
In this example, we will evaluate a temperature value and provide feedback based on its range.
Code:
void main() {
double temperature = 30.0; // Example temperature in Celsius
if (temperature > 30) {
print('It\'s a hot day.');
} else if (temperature >= 20) {
print('It\'s a pleasant day.');
} else {
print('It\'s a cold day.');
}
}
Code Explanation:
double temperature = 30.0;
: This line declares a variabletemperature
and assigns it the value 30.0.- The program checks the temperature using multiple conditions:
if (temperature > 30) {
: If the temperature is above 30, it prints "It's a hot day."else if (temperature >= 20) {
: If the temperature is between 20 and 30 (inclusive), it prints "It's a pleasant day."- If neither condition is met, the
else
block executes, printing "It's a cold day."
Conclusion
Implementing if-else logic in Dart allows you to create dynamic and responsive applications that can make decisions based on user input or other criteria. By understanding how to structure if-else statements, you can effectively handle various scenarios and enhance the interactivity of your programs.
PLAY QUIZ