Blogs by Jay Tillu

Conditionals in Dart

4 min read

Cover Image for Conditionals in Dart
  • There are various situations come in programming when you have to check the condition before code executes. There are many ways to check the condition and execute the code. It totally depends on us to choose which way in which condition.

There are a total of three ways in Dart to check the conditions:

  1. If Statement

  2. If-Else Statement

  3. Else If Statement

If Statement in Dart

If is a conditional statement. It is used when we just want to check the condition. Let’s see if it’s syntax and some rules.

if (boolean_expression) {
    //Statements to be executed if the condition is true
}
  • If the Boolean expression evaluates to be true, then the block of code inside the if statement will be executed.

  • If the Boolean expression evaluates to be false, then the first set of code after the end of the if statement (after the closing curly brace) will be executed.

Sample Code

void main() {
  int number = 35;
  if (number > 5) {
    print("Number is greater than 5");
  }
}

Output
Number is greater than 5

If Else Statement in Dart

If-else is a conditional statement. It is used when we want to check the condition. but apart from the if statement, here we have an else block too. Which will execute if the condition of the if statement is evaluated as false.

Syntax

if (boolean_expression) {
  //Statements to be executed if the condition is true
} else {
  //Statements to be executed if the condition is false
}
  • If the Boolean expression evaluates to be true, then the block of code inside the if statement will be executed.

  • If the Boolean expression evaluates to be false, then the else’s code block will be executed.

  • Here remember that else is optional here.

Sample Code


void main() {
  int number = 35;
  if (number > 5) {
    print("Number is greater than 5");
  } else {
    print("Number is less than 5");
 }
}

Output
Number is greater than 5

Nested If Else in Dart

Else if the ladder is a type of conditional in the dart. Just like if and if-else. We use an else-if ladder when we want to check multiple conditions.

Syntax

if (boolean_expression) {
  //Statements to be executed if the condition is true
} else if (boolean_expression) {
  //Statements to be executed if the condition is true
} else if (boolean_expression) {
  //Statements to be executed if the condition is true
} else {
  //Statements to be executed if all the conditions are false
}
  • If the Boolean expression evaluates to be true, then the block of code inside the if statement will be executed. Otherwise, the compiler will check the else-if condition.

  • Once any else-if condition is true, then all other else-if conditions will be skipped. So every else-if condition must be unique.

  • If all the else-if conditions evaluate to be false, then the else’s code block will be executed.

  • Here again, the else is optional. But if it occurs then it must be after all the else-if blocks.

Sample Code

void main() {
  int number = 2;

  if (number == 5) {
    print("Number is 5");
  } else if (number == 1) {
    print("Number is 1");
  } else if (number == 2) {
    print("Number is 2");
  } else {
    print("None of the number is matched");
  }
}

Output
Number is 2

So, guys, That’s all you need to know about Conditionals. Please let me know if I miss something. I’ll be happy to learn from you. Till Then Keep Loving, Keep Coding. I’ll surely catch you up in the next article. Jai Hind, Vande Mataram 🇮🇳

Remember no teacher, no book, no video tutorial, or no blog can teach you everything. As one said Learning is Journey and Journey never ends. Just collect some data from here and there, read it, learn it, practice it, and try to apply it. Don’t feel hesitant that you can’t do that or you don’t know this concept or that concept. Remember every programmer was passed from the path on which you are walking right now. Remember Every Master was Once a Beginner. Work hard and Give your best.

💡
Subscribe To My Newsletter For More Such Content.

Learn More about Dart and Flutter

Follow me for more such content.