Decision-making in Dart is crucial for controlling the flow of your program based on certain conditions. Dart provides several decision-making constructs, such as if
, else if
, else
, and switch
statements. These constructs allow you to execute specific blocks of code depending on whether conditions are true or false. Understanding how to use these constructs effectively will enable you to write more dynamic and responsive Dart applications.
For further information on detailed topics of Dart, click here.
If Statement
The if
statement is one of the most basic decision-making constructs in Dart. It allows you to execute a block of code only if a specific condition evaluates to true. This is useful for checking conditions and performing actions based on those conditions.
Example 1: Basic If Statement
void checkAge(int age) {
if (age >= 18) {
print('You are an adult.');
}
}
void main() {
checkAge(20); // Output: You are an adult.
}
In this example, the checkAge
function checks if the age is 18 or older, and if so, it prints that the person is an adult.
Example 2: Check for Positive Number
void checkPositive(int number) {
if (number > 0) {
print('$number is positive');
}
}
void main() {
checkPositive(10); // Output: 10 is positive
}
Here, the checkPositive
function checks if a number is positive and prints a message if the condition is true.
If-Else Statement
The if-else
statement allows you to define two different paths of execution: one if the condition is true and another if the condition is false. This is useful when you need to handle both outcomes of a condition.
Example 1: Check Even or Odd
void checkEvenOdd(int number) {
if (number % 2 == 0) {
print('$number is even');
} else {
print('$number is odd');
}
}
void main() {
checkEvenOdd(5); // Output: 5 is odd
}
In this example, the checkEvenOdd
function checks if a number is even or odd and prints the appropriate message.
Example 2: Check Age for Voting Eligibility
void checkVotingEligibility(int age) {
if (age >= 18) {
print('You are eligible to vote.');
} else {
print('You are not eligible to vote.');
}
}
void main() {
checkVotingEligibility(17); // Output: You are not eligible to vote.
}
This function checks if a person is eligible to vote based on their age and provides feedback accordingly.
If-Else-If Ladder
The if-else-if
ladder is used when you need to check multiple conditions. It allows you to test several conditions in sequence and execute different blocks of code depending on which condition is true.
Example 1: Grading System
void checkGrade(int score) {
if (score >= 90) {
print('Grade: A');
} else if (score >= 80) {
print('Grade: B');
} else if (score >= 70) {
print('Grade: C');
} else {
print('Grade: F');
}
}
void main() {
checkGrade(85); // Output: Grade: B
}
This example demonstrates a grading system where different grades are assigned based on the score.
Example 2: Temperature Check
void checkTemperature(int temp) {
if (temp >= 30) {
print('It\'s hot');
} else if (temp >= 20) {
print('It\'s warm');
} else if (temp >= 10) {
print('It\'s cool');
} else {
print('It\'s cold');
}
}
void main() {
checkTemperature(15); // Output: It's cool
}
This function checks the temperature and provides feedback based on the temperature range.
Switch Statement
The switch
statement is a more efficient way to handle multiple conditions that are based on a single variable or expression. It provides a cleaner alternative to using multiple if-else
statements, especially when dealing with many possible values for a single condition. The switch
statement also supports a default
case, which is executed when none of the specified cases match the given value.
Example 1: Day of the Week
void dayOfWeek(int day) {
switch (day) {
case 1:
print('Monday');
break;
case 2:
print('Tuesday');
break;
case 3:
print('Wednesday');
break;
case 4:
print('Thursday');
break;
case 5:
print('Friday');
break;
case 6:
print('Saturday');
break;
case 7:
print('Sunday');
break;
default:
print('Invalid day');
}
}
void main() {
dayOfWeek(5); // Output: Friday
}
In this example, the switch
statement is used to print the day of the week based on the provided day number.
Example 2: Traffic Light Colors
void trafficLight(String color) {
switch (color) {
case 'red':
print('Stop');
break;
case 'yellow':
print('Slow down');
break;
case 'green':
print('Go');
break;
default:
print('Invalid color');
}
}
void main() {
trafficLight('green'); // Output: Go
}
This function uses a switch
statement to provide instructions based on the color of a traffic light.
Nested If Statements
Nested if
statements allow you to perform further checks within an if
or else
block. This is useful when you need to check multiple conditions in a hierarchical manner.
Example 1: Check Number Properties
void checkNumber(int number) {
if (number > 0) {
if (number % 2 == 0) {
print('$number is positive and even');
} else {
print('$number is positive and odd');
}
} else {
print('$number is not positive');
}
}
void main() {
checkNumber(4); // Output: 4 is positive and even
}
This example checks if a number is positive, and if it is, further checks if it’s even or odd.
Example 2: Check Weather Conditions
void checkWeather(int temp, String condition) {
if (temp > 0) {
if (condition == 'sunny') {
print('It\'s warm and sunny');
} else {
print('It\'s warm but not sunny');
}
} else {
print('It\'s cold');
}
}
void main() {
checkWeather(25, 'cloudy'); // Output: It's warm but not sunny
}
In this example, the function checks the temperature and weather condition to provide detailed feedback.
Conclusion
Decision-making statements in Dart are essential for controlling the flow of your program. Whether you’re using simple if
statements, complex if-else-if
ladders, or switch
statements, mastering these constructs will enable you to write more dynamic and responsive code. By using nested if
statements, you can handle more complex conditions efficiently. Understanding and applying these concepts will improve the quality and flexibility of your Dart applications.
Happy Coding…!!!
Leave a Reply