Conditional statements are fundamental programming constructs that allow a program to execute specific blocks of code based on whether certain conditions are true or false. They enable decision-making within the code, allowing developers to control the flow of execution depending on varying inputs or states. 
For example, in a user authentication system, conditional statements can determine whether a user's credentials are valid, thereby allowing access to restricted areas of an application or providing feedback for incorrect input. This capability is essential for creating interactive and dynamic applications that respond appropriately to user actions or system conditions.

Types of Conditional Statements

1. If Statement

The simplest form of a conditional statement is the if statement, which executes a block of code if a specified condition is true.

Syntax:

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

Example:

void main() {
  int age = 18;

  if (age >= 18) {
    print('You are an adult.');
  }
}

Code Explanation:

  • int age = 18;: This line declares an integer variable named age and initializes it with the value 18.
  • if (age >= 18) {: This checks if age is greater than or equal to 18.
  • print('You are an adult.');: If the condition is true, this line executes, printing "You are an adult."

2. If-Else Statement

An if-else statement provides an alternative block of code that executes when 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 = 16;

  if (age >= 18) {
    print('You are an adult.');
  } else {
    print('You are not an adult.');
  }
}

Code Explanation:

  • int age = 16;: This declares age with a value of 16.
  • if (age >= 18) {: This checks if age is 18 or older.
  • If true, it prints "You are an adult."
  • else {: If the condition is false, the code inside this block executes.
  • print('You are not an adult.');: This line prints if the age is less than 18.

3. Else If Statement

You can chain multiple conditions using else if to handle more than two cases.

Syntax:

if (condition1) {
  // Code for condition1 } else if (condition2) {
  // Code for condition2 } else {
  // Code if none of the conditions are true 
  }

Example:

void main() {
  int score = 75;

  if (score >= 90) {
    print('Grade: A');
  } else if (score >= 80) {
    print('Grade: B');
  } else if (score >= 70) {
    print('Grade: C');
  } else {
    print('Grade: D');
  }
}

Code Explanation:

  • int score = 75;: This declares a variable score with a value of 75.
  • if (score >= 90) {: Checks if the score is 90 or above.
  • If true, it prints "Grade: A."
  • else if (score >= 80) {: If the first condition is false, it checks if the score is 80 or above.
  • The process continues until it finds a true condition, or defaults to the else statement, which prints "Grade: D."

4. Switch Statement

A switch statement is a more concise way to handle multiple conditions based on the value of a single variable.

Syntax:

switch (expression) {
  case value1:
    // Code for value1     break;
  case value2:
    // Code for value2     break;
  default:
    // Code if no cases match
     }

Example:

void main() {
  String day = 'Monday';

  switch (day) {
    case 'Monday':
      print('Start of the week.');
      break;
    case 'Friday':
      print('End of the work week.');
      break;
    case 'Saturday':
    case 'Sunday':
      print('It\'s the weekend!');
      break;
    default:
      print('Midweek days.');
  }
}

Code Explanation:

  • String day = 'Monday';: This declares a variable day initialized to 'Monday'.
  • switch (day) {: This begins the switch statement to evaluate the value of day.
  • case 'Monday':: If day matches 'Monday', it executes the following block.
  • print('Start of the week.');: This line is executed if the case matches.
  • break;: This prevents fall-through to the next case.
  • Additional cases check for 'Friday' and the weekend days, with a default case that executes if no cases match.

Conclusion

Conditional statements in Dart provide a powerful way to control the flow of your program based on specific conditions. By using if, else if, else, and switch statements, you can handle various scenarios effectively. Understanding how to implement these statements is crucial for creating dynamic and responsive applications.

PLAY QUIZ

What is the purpose of the if statement in Dart?

To execute code only when a condition is false

To execute a block of code if a specified condition is true

To handle multiple conditions at once

To declare variables