OneBite.Dev - Coding blog in a bite size

Check If Two Variable Not Equal In Dart

Code snippet for how to Check If Two Variable Not Equal In Dart with sample and detail explanation

The Dart programming language is a versatile language highly used for modern app development. In this article, we would attempt to understand how to check if two variables are not equal in Dart.

Code snippet for Checking if Two Variables are not Equal in Dart

In Dart, the ‘!=’ operator is used to check if two variables are not equal. Below is a simple illustrative code snippet.

void main() {
  var num1 = 3;
  var num2 = 5;

  if(num1 != num2) {
    print("num1 and num2 are not equal");
  } else {
    print("num1 and num2 are equal");
  }
}

In this Dart program, we have two variables, num1 and num2, which are initialized as 3 and 5. Through the use of the ‘if’ statement and the ’!=’ operator, we will check if these two variables are not equal.

Code Explanation for Checking if Two Variables are not Equal in Dart

Firstly, we initialize two variables, ‘num1’ and ‘num2’, and assign them numerical values of 3 and 5 respectively.

var num1 = 3;
var num2 = 5;

Next, we utilize an ‘if’ statement to test if the variables ‘num1’ and ‘num2’ are not equal. Here, ‘!=’ is the not equal operator in Dart. It compares the values of ‘num1’ and ‘num2’. If they are not equal, the condition within the ‘if’ statement becomes true.

// it will print "num1 and num2 are not equal"
if(num1 != num2){
   print("num1 and num2 are not equal");
}

However, if ‘num1’ and ‘num2’ are equal, the condition within the ‘else’ statement becomes true and hence it will print “num1 and num2 are equal”.

// it will only print this if num1 and num2 are equal
else{
   print("num1 and num2 are equal");
}

In this way, Dart provides the ‘!=’ operator for comparing variables.

Understanding how to compare variables and check for inequality or equality is a fundamental part of programming, applicable not only in Dart, but in most programming languages.

dart