Control flow is a fundamental concept in programming that enables you to dictate the execution of your code based on certain conditions. Dart, like many other programming languages, provides mechanisms for controlling the flow of your program through conditional statements such as if
, else
, and switch
. In this discussion, we’ll delve into the usage of these control flow statements in Dart, with examples to illustrate their functionality.
Conditional Statements with if
and else
Conditional statements in Dart allow you to make decisions in your code. The if
and else
statements are the most basic tools for creating conditions and making choices in your program.
1. if
Statement
The if
statement is used to execute a block of code if a specified condition is true
. If the condition is false
, the code block is skipped.
bool isRaining = true;
if (isRaining) {
print('Take an umbrella');
}
In this example, the code inside the if
block is executed because the isRaining
variable is true
.
2. else
Statement
The else
statement is used in conjunction with if
to execute a different block of code when the condition is false
.
int age = 25;
if (age >= 18) {
print('You can vote.');
} else {
print('You cannot vote.');
}
In this example, the else
block is executed because the condition age >= 18
is false
.
3. else if
Statement
You can use the else if
statement to provide additional conditions to check if the initial if
condition is false
.
int grade = 75;
if (grade >= 90) {
print('A');
} else if (grade >= 80) {
print('B');
} else if (grade >= 70) {
print('C');
} else {
print('D');
}
In this example, the program evaluates the conditions in sequence until it finds a true condition and executes the corresponding code block.
Switch Statement
The switch
statement is used when you have a single expression that can have multiple values and you want to execute different code blocks based on the value of that expression.
String day = 'Monday';
switch (day) {
case 'Monday':
print('It\'s the start of the workweek.');
break;
case 'Friday':
print('It\'s almost the weekend!');
break;
default:
print('It\'s a regular day.');
}
In this example, the program evaluates the value of the day
variable and executes the code block associated with the matching case label. The break
statement is used to exit the switch
block after executing a case.
Conditional Expressions
Dart also provides conditional expressions that allow you to simplify the syntax for making decisions in your code.
1. Conditional (Ternary) Operator
The conditional operator (? :
) is a shorthand way of writing an if-else
statement in a single line.
int age = 20;
String result = (age >= 18) ? 'You can vote' : 'You cannot vote';
In this example, the conditional operator checks if age >= 18
. If it’s true, the first value is assigned to result
; otherwise, the second value is assigned.
2. Null-aware Conditional Operator
Dart also offers the null-aware conditional operator (??
) for handling null values.
String name = null;
String result = name ?? 'Unknown';
In this example, result
is assigned the value of name
, but if name
is null
, it defaults to ‘Unknown’.
Example: Using Control Flow in Dart
Let’s use control flow in Dart to create a simple program that checks if a given number is positive, negative, or zero:
int number = 5;
if (number > 0) {
print('$number is a positive number.');
} else if (number < 0) {
print('$number is a negative number.');
} else {
print('$number is zero.');
}
In this example, we use an if-else if-else
construct to determine the nature of the given number.
Conclusion
Control flow in Dart, including the if-else
, switch
, and conditional operators, provides the ability to make decisions and execute different code blocks based on specific conditions. These constructs are vital tools for writing flexible and responsive programs that can handle various scenarios and user inputs. Understanding and using control flow effectively is essential for writing efficient and reliable Dart code.