Dart Program Datatypes - TechXplore

Friday, 10 February 2023

Dart Program Datatypes

 Datatypes:

Dart is a statically-typed, object-oriented programming language used for building scalable and high-performance web, desktop, and mobile applications. It supports several data types, including:



1.Numbers: Dart supports both integers  and floating-point numbers (double).

Example:

void main(){

// numbers

int a=20;
double b=3.5;
var c=a+b;
var d='Dart programing';
print('The sum is : ${c}');
print(d);

}

output:

The sum is : 23.5

Dart programing

2.Strings: Strings are used to represent sequences of characters, and can be declared using single or double quotes.

Example:

void main(){

   //strings

   String string="Techxplore";

   String str1='Tech  ';
   String str2='xplore';

   var str3='Welcome tech channel';
   print(str3);

   print(string);
   print(str1 + str2);
  }

output:
Welcome tech channel
Techxplore
Tech  xplore

3.Booleans: A boolean value can either be true or false.

Example:

void main(){
//Boolean

String str1='coding is ';
String str2='fun';

//String str2='coding is fun';
bool str3=(str1==str2);
print(str3);
}
output:
false

4.Lists: Lists are ordered collections of objects, and can contain any type of object, including numbers, strings, or other lists.

Example:
void main(){
// list 
List list=['dart','python','java','c'];
print(list);
print(list[2]);
}
output:
[dart, python, java, c]
java

5.Maps: Maps are collections of key-value pairs, where each key is unique.
Example:

void main(){
// list 

List list=['dart','python','java','c'];

print(list);
print(list[2]);

}
output:

{pro1: java, pro2: dart, pro3: c, pro4: python}

dart


Operators in Dart Program:

Dart, like many programming languages, uses operators to perform operations on variables and values. Operators are symbols that tell the compiler to perform specific operations and return the result.




1.Arithmetic operators: +, -, *, /, %, and others. These operators are used to perform basic arithmetic operations, such as addition, subtraction, multiplication, division, and modulo.

Example:

void main(){

 // operators

  int a=2;

  int b=3;

  print("The sum is : ${a+b}");

  print('The sub is : ${a-b}');

  print("The mul is : ${a*b}");

  print("The div is : ${a/b}");

}

output:

The sum is : 5

The sub is : -1

The mul is : 6

The div is : 0.6666666666666666

2.Relational operators: ==, !=, >, <, >=, <=. These operators are used to compare values and return a Boolean result (true or false). For example, 5 == 5 returns true, while 5 == 6 returns false.

Example:

void main(){

//Relational operators

var a=20;

var b=10;

print("a is greater than b : ${a>b}");

print("a is less than b : ${a<b}");

print("a is greater than or equal to b : ${a>=b}");

print("a is less than or equal to b : ${a<=b}");

print("a is equal to  b : ${a==b}");

print("a is not equal to b : ${a!=b}");

}

output:

a is greater than b : true

a is less than b : false

a is greater than or equal to b : true

a is less than or equal to b : false

a is equal to  b : false

a is not equal to b : true

3.Type test operators:

Example:

void main(){

//type test operators

String a="techxplore";

var b=20;

print(a is String);

print(b is !int);

}

output:

true

false

4.Logical operators: &&, ||, !. These operators are used to perform logical operations and return a Boolean result. For example, true && false returns false, while true || falsereturns true.

Example:

void main(){

//logical operators

int a=10;

int b=20;

bool c=a>b && a<b;

bool d=a>b || a<b;

print(c);

print(d);

}

output:

false

true

5.Assignment operator: =. This operator is used to assign a value to a variable. For example, x = 5; assigns the value 5 to the variable x.





No comments:

Post a Comment