OneBite.Dev - Coding blog in a bite size

Use If Conditional Statement In Dart

Code snippet for how to Use If Conditional Statement In Dart with sample and detail explanation

Dart is a sophisticated, object-oriented language that uses a clear, easy-to-follow syntax. One of the essential topics to grasp in Dart programming is the IF conditional statement, which significantly facilitates decision-making in coding.

Code snippet for IF Conditional Statement

void main() {
  var num = 10;
  if(num > 15){
    print("Number is greater than 15");
  }else{
    print("Number is not greater than 15");
  }
}

Code Explanation for IF Conditional Statement

Every programming language has a way to handle condition-based executions, and Dart uses the if conditional statement, amongst other ways, to handle this.

In our code snippet, we first create a variable num and assign a value of 10 to it using var num = 10;.

The if statement is then implemented to check if num is greater than 15 using if(num > 15). This is the key decision-making point in the code snippet.

Inside the if statement, the print function will output “Number is greater than 15” to the console if the if condition is true (i.e., if num is indeed greater than 15).

But what happens if num is not greater than 15? That’s where Dart’s else statement comes in. After the if statement, the else statement executes when the if condition is not met.

In our code, else prints “Number is not greater than 15” because our num (which is 10 and thus not greater than 15) fails the if condition.

Together, this entire block of code forms a simple demonstration of how an if conditional statement works in Dart. If the coding condition is met, one task is performed, and if it’s not met, another task is executed, providing a concrete basis for decision-based programming in Dart.

dart