Dart Control Flow Statements


Control flow statements in Dart allow you to dictate the flow of execution in your program. They enable decision-making, looping, and branching, letting you control how and when certain blocks of code are executed. Here’s a comprehensive overview of the primary control flow statements in Dart:

1. Conditional Statements

Conditional statements execute certain blocks of code based on whether specified conditions are true or false.

1.1 if Statement

The if statement evaluates a condition and executes a block of code if the condition is true.

Syntax:

if (condition) { // Code to execute if condition is true }

Example:

void main() { int number = 10; if (number > 0) { print('The number is positive.'); } }

1.2 else Statement

The else statement provides an alternative block of code that runs if the if 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 number = -5; if (number > 0) { print('The number is positive.'); } else { print('The number is non-positive.'); // This will be executed } }

1.3 else if Statement

The else if statement allows you to check multiple conditions sequentially.

Syntax:

if (condition1) { // Code to execute if condition1 is true } else if (condition2) { // Code to execute if condition1 is false and condition2 is true } else { // Code to execute if both conditions are false }

Example:

void main() { int score = 85; if (score >= 90) { print('Grade: A'); } else if (score >= 80) { print('Grade: B'); // This will be executed } else { print('Grade: C or below'); } }

2. Switch Statement

The switch statement is a control flow statement that allows you to execute different parts of code based on the value of a variable.

Syntax:

switch (expression) { case value1: // Code to execute if expression equals value1 break; case value2: // Code to execute if expression equals value2 break; default: // Code to execute if expression matches none of the cases }

Example:

void main() { String fruit = 'apple'; switch (fruit) { case 'apple': print('You chose apple.'); break; case 'banana': print('You chose banana.'); break; default: print('Unknown fruit.'); } }

3. Looping Statements

Looping statements are used to execute a block of code repeatedly based on a condition.

3.1 for Loop

The for loop is used when you know in advance how many times you want to execute a block of code.

Syntax:

for (initialization; condition; increment) { // Code to execute }

Example:

void main() { for (int i = 0; i < 5; i++) { print('Iteration: $i'); // Outputs 0 to 4 } }

3.2 while Loop

The while loop continues executing as long as the condition is true.

Syntax:

while (condition) { // Code to execute }

Example:

void main() { int count = 0; while (count < 5) { print('Count: $count'); // Outputs 0 to 4 count++; } }

3.3 do-while Loop

The do-while loop is similar to the while loop, but it guarantees that the block of code will be executed at least once.

Syntax:

do { // Code to execute } while (condition);

Example:

void main() { int count = 0; do { print('Count: $count'); // Outputs 0 to 4 count++; } while (count < 5); }

4. Control Flow Statements with break and continue

4.1 break

The break statement is used to exit a loop or a switch case prematurely.

Example:

void main() { for (int i = 0; i < 5; i++) { if (i == 3) { break; // Exit the loop when i is 3 } print('Iteration: $i'); // Outputs 0, 1, 2 } }

4.2 continue

The continue statement skips the current iteration of a loop and moves to the next iteration.

Example:

void main() { for (int i = 0; i < 5; i++) { if (i == 3) { continue; // Skip the rest of the loop body when i is 3 } print('Iteration: $i'); // Outputs 0, 1, 2, 4 } }

Conclusion

Control flow statements are essential in Dart programming, allowing you to make decisions, repeat actions, and manage the flow of execution in your applications. Mastering these constructs will enable you to write more complex and functional Dart code.