if, else, else if
Ternary Operators
switch and case
Exception Handling
try, catch, finally
Throwing Custom Exceptions
Control flow statements in Dart allow you to dictate the direction your program takes, making decisions, executing code based on conditions, and repeating tasks. Let’s go through some common control statements in Dart:
The if statement executes a block of code if a specified condition is true. The else statement runs an alternative block of code if the condition is false.
Syntax:
if (condition) {
// code to execute if condition is true
} else {
// code to execute if condition is false
}
Example:
void main() {
int age = 20;
if (age >= 18) {
print("You are an adult.");
} else {
print("You are a minor.");
}
}
else if:
Use else if to test multiple conditions.
void main() {
int score = 85;
if (score >= 90) {
print("Grade: A");
} else if (score >= 80) {
print("Grade: B");
} else if (score >= 70) {
print("Grade: C");
} else {
print("Grade: F");
}
}
The switch statement evaluates an expression and executes code based on matching case blocks. It’s useful for checking equality of a single variable against multiple values.
Syntax:
switch (expression) {
case value1:
// code to execute
break;
case value2:
// code to execute
break;
default:
// code to execute if no case matches
}
Example:
void main() {
String grade = 'B';
switch (grade) {
case 'A':
print("Excellent!");
break;
case 'B':
print("Good job!");
break;
case 'C':
print("You passed.");
break;
default:
print("Invalid grade.");
}
}
Loops allow you to execute a block of code repeatedly based on a condition.
a) For Loop
The for loop runs a block of code a specific number of times.
Syntax:
for (initialization; condition; increment/decrement) {
// code to execute
}
Example:
void main() {
for (int i = 1; i <= 5; i++) {
print("Count: $i");
}
}
b) While Loop
The while loop executes as long as a condition is true.
Syntax:
while (condition) {
// code to execute
}
Example:
void main() {
int count = 1;
while (count <= 5) {
print("Count: $count");
count++;
}
}
c) Do-While Loop
The do-while loop is similar to while but guarantees at least one execution, as the condition is checked after the loop body.
Syntax:
do {
// code to execute
} while (condition);
Example:
void main() {
int count = 1;
do {
print("Count: $count");
count++;
} while (count <= 5);
}
break: Exits a loop or switch statement immediately.
continue: Skips the current iteration and continues with the next loop iteration.
Example:
void main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue; // skip the rest of the loop when i == 3
}
if (i == 4) {
break; // exit the loop when i == 4
}
print("Count: $i");
}
}
Control statements like if-else, switch, loops (for, while, do-while), break, and continue give you the tools to create dynamic and flexible Dart applications. These control structures manage the flow of execution based on conditions and repetition patterns, essential for making any program responsive to different inputs and scenarios.