Chapter - 1 (Introduction To Dart)
Chapter - 1 (Introduction To Dart)
codewithzee
void main() {
print("Hello, World!");
}
In this program, the main() function is the entry point of the program. It prints the "Hello,
World!" message to the console using the print() function.
1.4 Variables and data types: Variables are used to store data in a program. Dart is a statically
typed language, which means that variables have a declared type. Let's explore variables and
data types in Dart. Variables in Dart are associated with specific data types, which define the
kind of data that can be stored in a variable. Dart has built-in data types for representing
various kinds of information. Let's explore some commonly used data types in Dart:
int: Represents integer values (whole numbers) such as 1, 2, -10, etc.
double: Represents floating-point values (numbers with decimal places) such as 3.14, -
0.5, etc.
String: Represents a sequence of characters, such as "Hello", "Dart", "123", etc. Strings
are enclosed in double or single quotation marks.
bool: Represents boolean values, which can be either true or false. Boolean values are
used to represent logical states or conditions.
dynamic: Represents a type that is not known at compile-time. The dynamic type allows
you to store values of any type in a variable, but it sacrifices static type checking.
var: A type inference mechanism in Dart that automatically determines the type of a
variable based on its initial value. The var type allows you to omit the explicit type
declaration.
Here's an example that demonstrates the use of different data types:
void main() {
int age = 25;
double height = 1.75;
String name = "John Doe";
bool isStudent = true;
codewithzee
In this example, we declare variables age with the int type, height with the double type, name
with the String type, and isStudent with the bool type. We assign values to these variables, and
then we use the print() function to display the values on the console.
It's important to choose the appropriate data type for your variables to ensure correct
representation and manipulation of data in your programs. Dart provides a rich set of data
types to handle different kinds of information.
1.5 Operators and expressions: Operators are used to perform operations on variables and
values. Dart provides various operators for arithmetic, assignment, comparison, and logical
operations.
void main() {
int a = 10;
int b = 5;
// Arithmetic operators
int sum = a + b;
int difference = a - b;
int product = a * b;
double quotient = a / b;
int remainder = a % b;
// Assignment operators
int x = 10;
x += 5; // Equivalent to x = x + 5
// Comparison operators
bool isEqual = a == b;
bool isNotEqual = a != b;
bool isGreater = a > b;
bool isLess = a < b;
bool isGreaterOrEqual = a >= b;
bool isLessOrEqual = a <= b;
// Logical operators
bool logicalAnd = (a > 0) && (b < 10);
bool logicalOr = (a > 0) || (b < 10);
bool logicalNot = !(a > 0);
print("Sum: $sum");
print("Difference: $difference");
codewithzee
print("Product: $product");
print("Quotient: $quotient");
print("Remainder: $remainder");
print("Is Equal: $isEqual");
print("Is Not Equal: $isNotEqual");
print("Is Greater: $isGreater");
print("Is Less: $isLess");
print("Is Greater or Equal: $isGreaterOrEqual");
print("Is Less or Equal: $isLessOrEqual");
print("Logical AND: $logicalAnd");
print("Logical OR: $logicalOr");
print("Logical NOT: $logicalNot");
}
In this program, we perform arithmetic operations using the +, -, *, /, and % operators. We also
demonstrate assignment operators (+=) to modify variable values. Additionally, we use
comparison operators (==, !=, >, <, >=, <=) and logical operators (&&, ||, !) to perform
comparisons and logical operations.
Congratulations! You have successfully completed chapter 1 of the Dart programming course.
In this chapter, you learned about the Dart programming language, set up your development
environment, wrote your first Dart program, explored variables and data types, and worked
with operators and expressions.
codewithzee