OneBite.Dev - Coding blog in a bite size

Check If Two Variable Is Not Equal In Dart

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

Dart, Google’s extensive general-purpose language, is often used for both server and web applications. Checking for non-equality between two variables is a common operation in Dart, and this article will walk you through how to perform this task in a clear and straightforward way.

Code snippet: Checking for Non-Equality in Dart

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

  if (num1 != num2) {
    print('The two numbers are not equal.');
  } else {
    print('The two numbers are equal.');
  }
}

Code Explanation for Checking for Non-Equality in Dart

The Dart code snippet above checks if two variables are not equal.

First, the void main() function is defined. This is the main function where the program starts execution. It is mandatory for every Dart application.

void main() {...}

Then, two variables num1 and num2 are declared and initialized with the values 5 and 10, respectively.

var num1 = 5;
var num2 = 10;

A comparison operator (!=) is used to ascertain if num1 is not equal to num2. The combination num1 != num2 translates to “num1 is not equal to num2”.

if (num1 != num2) {...}

If the condition num1 != num2 returns true (i.e., the variables are not equal), it executes the code within the first set of curly braces {}. In this case, the output will be ‘The two numbers are not equal.’

print('The two numbers are not equal.');

If the condition returns false (i.e., the variables are equal), it executes the code within the else statement’s curly braces {}. In this instance, the output will be ‘The two numbers are equal.’

print('The two numbers are equal.');

This simple Dart script effectively illustrates how to check if two variables are not equal in Dart. Adapt this code to suit your specific needs as you explore the Dart programming language.

dart