[go: up one dir, main page]

0% found this document useful (0 votes)
15 views6 pages

Variale Data Types Dart

Variale data types dart

Uploaded by

Rahul Borate
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views6 pages

Variale Data Types Dart

Variale data types dart

Uploaded by

Rahul Borate
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Introduction:

In this post, we will explore variables, data types, and


operators in Dart. Understanding these fundamental
concepts is crucial for building robust and dynamic
applications. So, let’s dive in and discover the exciting
world of Dart variables and operators!

Declaring Variables and Assigning Values:


Variables are essential in programming as they store and
manipulate data. In Dart, declaring a variable is as simple
as using the `var` keyword. For example:

var message = "Hello, Dart!";

Different Data Types in Dart:


Dart supports various data types,
including integers, strings, booleans, lists,
and maps, etc. Each data type serves a specific purpose in
handling different kinds of information. Here are a few
examples:

 Integers: Used for representing whole numbers, such


as
int score = 42;
 Strings: Used for representing textual data enclosed in
quotes, such as String name = “John”;

 Booleans: Used for representing true or false values,


such as
bool isRaining = true;

 Lists: Used for storing collections of items, such as


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

 Maps: Used for representing key-value pairs, such as


Map<String, int> ages = {“Alice”: 25, “Bob”: 30};

Type Inference and Explicit Type Declarations:


Dart offers type inference, meaning the language
automatically determines the type of a variable based on
its assigned value.
However, you can also explicitly declare the type if
needed, which can enhance code clarity and prevent
errors.
For example:

var age = 23; // Type inference: Dart infers age as an integer


String name = "Alice"; // Explicit type declaration: name is explicitly
declared as a string

Working with Constants and Final Variables:


In Dart, you can declare constants and final variables to
represent values that won’t change during the execution of
your program.
Constants are declared using the const keyword,
while final variables are declared using the final keyword.

Here’s the difference:

 Constants: The value of a constant is determined at


compile-time and cannot be changed afterwards.
For example:

const double PI = 3.14159; // Constant variable

 Final Variables: The value of a final variable is


determined at runtime and can only be assigned once.
For example:

final String appName = "My App"; // Final variable

Dart Operators for Arithmetic, Comparison, and


Logical Operations:
Operators in Dart allow you to perform various operations
on variables and values. Here are some commonly used
operators:

 Arithmetic
Operators: +, -, *, /, % (for addition, subtraction, mul
tiplication, division, and remainder)
void main() {
int a = 10;
int b = 5;
int sumation = a + b; // Addition
int difference = a - b; // Subtraction
int product = a * b; // Multiplication
double quotient = a / b; // Division
int remainder = a % b; // Modulo
print("Sum: $sumation"); // Output: Sum: 15
print("Difference: $difference"); // Output: Difference: 5
print("Product: $product"); // Output: Product: 50
print("Quotient: $quotient"); // Output: Quotient: 2.0
print("Remainder: $remainder"); // Output: Remainder: 0
}

 Comparison Operators: ==, !

=, <, >, <=, >= (for equality, inequality, less


than, greater than, less than or equal to,
and greater than or equal to)

void main() {
int a = 10;
int b = 5;

bool isEqual = a == b; // Equality check


bool isNotEqual = a != b; // Inequality check
bool isGreaterThan = a > b; // Greater than check
bool isLessThan = a < b; // Less than check
bool isGreaterThanOrEqual = a >= b; // Greater than or equal to check
bool isLessThanOrEqual = a <= b; // Less than or equal to check

print("Is Equal: $isEqual"); // Output: Is Equal: false


print("Is Not Equal: $isNotEqual"); // Output: Is Not Equal: true
print("Is Greater Than: $isGreaterThan"); // Output: Is Greater Than:
true
print("Is Less Than: $isLessThan"); // Output: Is Less Than: false
print(
"Is Greater Than or Equal: $isGreaterThanOrEqual"); // Output: Is
Greater Than or Equal: true
print(
"Is Less Than or Equal: $isLessThanOrEqual"); // Output: Is Less
Than or Equal: false
}
 Logical Operators: &&,||, ! (for logical AND, logical
OR, and logical NOT)

void main() {
bool isTrue = true;
bool isFalse = false;

bool andResult = isTrue && isFalse; // Logical AND


bool orResult = isTrue || isFalse; // Logical OR
bool notResult = !isTrue; // Logical NOT

print("AND Result: $andResult"); // Output: AND Result: false


print("OR Result: $orResult"); // Output: OR Result: true
print("NOT Result: $notResult"); // Output: NOT Result: false
}

These operators are widely used in real-life programming


scenarios, such as performing calculations, making
decisions based on comparisons, and combining conditions
in logical expressions.

String Manipulation and Common String Methods in


Dart:
Working with strings is a fundamental part of many
applications. Dart provides various methods to manipulate
and interact with strings. Here are a few commonly used
string methods:

 length: Returns the length of the string.

 toUpperCase(): Converts the string to uppercase.

 toLowerCase(): Converts the string to lowercase.


 substring(startIndex, endIndex): Extracts a portion of the
string based on the provided indices.

Let’s see some code examples:

String message = "Hello, Dart!";


print(message.length); // Output: 13
print(message.toUpperCase()); // Output: HELLO, DART!
print(message.substring(0, 5)); // Output: Hello

Conclusion:
Congratulations on completing this post! In this post, we
explored variables, data types, and operators in Dart.

We learned how to declare variables, work with different


data types, including lists and maps, use type inference
and explicit type declarations, handle constants and final
variables, and perform operations with operators.
Additionally, we discovered common string manipulation
methods in Dart.

You might also like