Dart Basic- Decision Making

Introduction to decision making in Dart

Decision-making is a programming concept that allows a program to execute a set of instructions based on a specific condition or set of conditions. The conditional constructs in Dart are used to make decisions based on evaluating a Boolean expression.

The if statement is the simplest form of a conditional statement in Dart. It consists of a Boolean expression followed by one or more statements. If the expression is true, the statement(s) will be executed; otherwise, they will be skipped.

The if-else statement is used to execute a block of code if the condition is true and another block of code if the condition is false. It consists of an if block followed by an else block.

The else-if ladder is useful when we need to test multiple conditions. It consists of a series of if-else blocks where each block contains a condition that is evaluated. If the condition is true, the corresponding block of code is executed, otherwise, the program moves on to the next block of code.

The switch statement evaluates an expression and executes the statements associated with the matching case clause. If there is no match, the default clause is executed. The switch statement is useful when we have multiple conditions to test and execute different blocks of code based on each condition.

In addition to the basic decision-making constructs, Dart has a ternary operator (condition ? expr1: expr2) that can be used to evaluate a condition and return one of two expressions based on whether the condition is true or false.

Some topics that could be expanded on in a tutorial on decision-making in Dart could include the following:

  • The use of logical operators to combine multiple conditions in a single expression.
  • The difference between a null-aware and a regular if statement, and how to use the former to avoid null pointer exceptions.
  • The concept of short-circuit evaluation and how it affects the order in which expressions are evaluated in a logical operation.
  • How to use decision-making constructs in conjunction with loops to execute code repeatedly based on certain conditions.
  • The use of assert statements to perform runtime checks on conditions that should always be true.

Using decision making

Example:

void main() {
  int num = 5;

  if (num > 0) {
    print("$num is a positive number.");
  } else if (num == 0) {
    print("$num is zero.");
  } else {
    print("$num is a negative number.");
  }
}

Output:

5 is a positive number.

This code snippet checks whether the value of the num variable is positive, zero, or negative, and prints out a message accordingly using an if-else statement. The output will be “5 is a positive number.”

Example:

void main() {
  var day = 5;
  String dayName;
  
  switch (day) {
    case 1:
      dayName = "Monday";
      break;
    case 2:
      dayName = "Tuesday";
      break;
    case 3:
      dayName = "Wednesday";
      break;
    case 4:
      dayName = "Thursday";
      break;
    case 5:
      dayName = "Friday";
      break;
    case 6:
      dayName = "Saturday";
      break;
    case 7:
      dayName = "Sunday";
      break;
    default:
      dayName = "Invalid day";
  }
  
  print("Today is ${dayName}");
}

Output:

Today is Friday

In this example, we have a variable day set to 5, and we use a switch statement to check its value. Depending on the value of day, we assign the corresponding day name to the variable dayName. If day is not in the range of 1 to 7, we assign the string “Invalid day” to dayName. Finally, we print the value of dayName to the console. The output will be “Today is Friday”.

Example:

void main() {
  int age = 18;
  
  if (age < 18) {
    print("You are not eligible to vote.");
  } else if (age >= 18 && age < 21) {
    print("You are eligible to vote, but cannot buy car.");
  } else {
    print("You are eligible to vote and buy car.");
  }
}

Output:

You are eligible to vote, but cannot buy car.

In this example, the program checks the value of the age variable and executes different blocks of code depending on the condition. If age is less than 18, the program prints “You are not eligible to vote.” If age is between 18 and 21, the program prints “You are eligible to vote, but cannot buy car.” And if age is 21 or above, the program prints “You are eligible to vote and buy car.” This is an example of an if…else if…else statement, which allows you to check multiple conditions and execute different blocks of code depending on which condition is true.

Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.