Exploring Dart Fundamentals: Variables, Types, Constants, and Operators

Dart, with its simplicity and power, is a modern programming language that caters to various development needs, from mobile applications to server-side solutions. In this comprehensive guide, we’ll explore the foundational concepts of Dart through practical examples.

1. Variables and Types:

1.1 Variables: Variables store data that can be manipulated and referenced in a program. In Dart, you declare variables using the varfinal, or const keywords.

var age = 23; //age as an integer

const PI = 3.1415; // PI is a compile-time constant

1.2 Types: Dart is a statically typed language, meaning each variable has a specific data type known at compile-time. Dart provides several built-in data types:

int age = 30;
double height = 5.11;

String name = 'Sadanand';

bool isAdult = true;

List<int> numbers = [1, 2, 3, 4, 5];

Map<String, dynamic> person = {
    'name': 'Sadanand',
    'age': 23,
    'isAdult': true
};

2. Dynamic:

Dynamic: Represents a variable whose type can change dynamically at runtime.

dynamic dynamicVariable = 'Sadanand';
dynamicVariable = 23; // Now dynamicVariable is an integer

3. Common Operators:

Common operators in programming languages are symbols or keywords used to perform various operations on data. Here’s a brief explanation of common operators used in programming:

3.1 Arithmetic Operators:

3.2 Relational Operators:

3.3 Equality Operators:

3.4 Logical Operators:

void operatorExample() {
  int x = 23;
  int y = 27;

  // Arithmetic operators
  final add = x + y;                  // Addition
  final sub = x - y;                  // Subtraction
  final mut = x * y;                  // Multiplication
  final div = x / y;                  // Division
  final divwithintegers = y ~/ x;     // Truncating Division (returns an integer)
  final modulo = x % y;               // Modulus (remainder of division)

  // Relational operators
  final greater = x > y;              // Greater than
  final notGreater = x < y;           // Less than
  final greaterthan = x >= y;         // Greater than or equal to
  final notgreaterthan = x <= y;      // Less than or equal to

  // Equality operators
  final equalTo = x == y;             // Equal to
  final notEqualTo = x != y;          // Not equal to

  // Logical operators
  final logicalAnd = x > y && y < x;  // Logical AND
  final logicalOr = x > y || y < x;   // Logical OR

  // Printing results
  print("Addition of two numbers: $add");
  print("Subtraction of two numbers: $sub");
  print("Multiplication of two numbers: $mut");
  print("Division of two numbers: $div");
  print("Divide, returning an integer result: $divwithintegers");
  print("Remainder of an integer division: $modulo");
  print("Greater than: $greater");
  print("Less than: $notGreater");
  print("Greater than or equal to: $greaterthan");
  print("Less than or equal to: $notgreaterthan");
  print("Equal to: $equalTo");
  print("Not equal to: $notEqualTo");
  print("Logical AND: $logicalAnd");
  print("Logical OR: $logicalOr");
}

These operators are fundamental for performing arithmetic calculations, making comparisons, and evaluating conditions in Dart programs.

Conclusion:

Dart’s versatility and simplicity make it an excellent choice for developers across various domains. Understanding these fundamental concepts equips you to write efficient Dart code for diverse applications, ensuring clarity, reliability, and performance. Happy coding with Dart!

Start Coding in Dart Now!

Head over to DartPad (https://dartpad.dev) to start coding immediately. DartPad is a user-friendly online editor where you can write, run, and share Dart code without any setup required.


Also published here