OneBite.Dev - Coding blog in a bite size

Use A Conditional To Check Less Than Number In Dart

Code snippet for how to Use A Conditional To Check Less Than Number In Dart with sample and detail explanation

Dart, a popular programming language developed by Google, allows developers to easily handle various programming concepts, one of which involves using a conditional to check if a number is less than another number. In this article, we will learn how to use this in Dart with a simple example.

Code snippet for Using a Conditional to Check Less Than Number in Dart

Let’s take a look at a very basic example to understand this concept-

void main() {
  int num1 = 5;
  int num2 = 7;
  
  if (num1 < num2){
    print('$num1 is less than $num2');
  } else {
    print('$num1 is not less than $num2');
  }
}

This code will output: “5 is less than 7”.

Code Explanation for using a Conditional to Check Less Than Number in Dart

The code above is pretty simple to understand. Here we’re using the ‘if’ statement which checks the condition stated in the brackets (num1 < num2) to decide the course of action.

  1. Firstly, we declare and initialise two integer variables “num1” and “num2” to the values 5 and 7 respectively.

  2. We then move to the ‘if’ condition. This is where we actually check whether the value of “num1” is less than “num2”. Here, “num1” is indeed less than “num2” (5<7), so the ‘if’ condition holds true.

  3. When the ‘if’ condition is true, the code block within the ‘if’ statement is executed. The code block here is to print a message stating that ‘num1’ is less than ‘num2’.

  4. In a situation where ‘num1’ is not less than ‘num2’, the ‘if’ condition is false, so the code block in the ‘else’ part will be executed.

Using such ‘if’ conditions in our code is crucial to control the flow of execution based on certain situations. By mastering this concept, the range of applications you can develop using Dart expands significantly. So, keep practicing, and in no time, it will become second nature.

dart