Dart if else-if else statement


In Dart, if, else if, and else statements are fundamental control flow constructs that enable you to execute specific blocks of code based on whether certain conditions are true or false. They allow for decision-making in your program and help control the flow of execution. Here’s a detailed breakdown of how to use these statements effectively.

1. The if Statement

The if statement evaluates a condition (a boolean expression). If the condition is true, the block of code inside the if statement is executed. If it is false, the program proceeds to check any subsequent conditions.

Syntax:

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

Example:

void main() { int temperature = 30; if (temperature > 25) { print('It is warm outside.'); // This will be executed } }

2. The else if Statement

The else if statement is used to check multiple conditions. If the initial if condition is false, Dart checks the condition specified in the else if. If that condition is true, the corresponding block of code is executed.

Syntax:

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

Example:

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

3. The else Statement

The else statement provides an alternative block of code that executes when all preceding conditions (if and else if) are false. It acts as a catch-all for any cases not explicitly handled by the prior conditions.

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 all previous conditions are false }

Example:

void main() { int age = 15; if (age >= 18) { print('You are an adult.'); } else if (age >= 13) { print('You are a teenager.'); } else { print('You are a child.'); // This will be executed } }

Complete Example

Here’s a complete example that combines all three statements:

void main() { int score = 72; if (score >= 90) { print('Grade: A'); } else if (score >= 80) { print('Grade: B'); } else if (score >= 70) { print('Grade: C'); // This will be executed } else if (score >= 60) { print('Grade: D'); } else { print('Grade: F'); } }

Important Notes

  • Evaluation Order: The conditions are evaluated in order. Once a true condition is found, the corresponding block is executed, and the rest are skipped.
  • Braces: While braces {} are optional for single statements, it is best practice to use them for clarity and to avoid errors, especially as your code becomes more complex.
  • Boolean Expressions: Conditions can be simple (like score >= 70) or complex, using logical operators (&& for AND, || for OR).

Conclusion

The if, else if, and else statements in Dart are powerful constructs for controlling the flow of your program based on conditions. Understanding how to use these statements effectively will enable you to create dynamic and responsive applications that can handle various scenarios based on user input or other factors.