Professional Documents
Culture Documents
Lab # 2
void main() {
print("Dart language is easy to learn");
}
Important concepts
As you learn about the Dart language, keep these facts and concepts in mind:
Everything you can place in a variable is an object, and every object is an instance of a class. Even
numbers, functions, and null are objects. All objects inherit from the Object class.
Unlike Java, Dart doesn’t have the keywords public, protected, and private. If an identifier starts with an
underscore (_), it’s private to its library.
Dart supports top-level functions (such as main()), as well as functions tied to a class or object
(static and instance methods, respectively). You can also create functions within functions (nested or local
functions).
Variable is named storage location and Data types simply refer to the type and size of data associated
with variables and functions.
Dart uses var keyword to declare the variable. The syntax of var is defined below,
void main() {
var list = [1,2,3,4,5];
print(list);
}
Dynamic − If the variable type is not defined, then its default type is dynamic. The
following example illustrates the dynamic type variable −
void main() {
dynamic name = "Dart";
print(name);
}
A decision making block evaluates a condition before the instructions are executed. Dart
supports If, If...else and switch statements.
Loops are used to repeat a block of code until a specific condition is met. Dart supports for,
for...in, while and do...While loops.
Let us understand a simple example about the usage of control statements and loops −
void main() {
for( var i = 1 ; i <= 10; i++ ) {
if(i%2==0) {
print(i);
}
}
}
Functions
A function is a group of statements that together performs a specific task. Let us look
void main() {
add(3,4);
}
void add(int a,int b) {
int c;
c = a+b;
print(c);
}
int fibonacci(int n) {
if (n == 0 || n == 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
year += 1;
}
Exception
if (astronauts == 0) {
throw StateError('No astronauts.');
}
try {
for (var object in flybyObjects) {
var description = await File('$object.txt').readAsString();
print(description);
}
} on IOException catch (e) {
print('Could not describe object: $e');
} finally {
flybyObjects.clear();
}
Note that the code above is asynchronous; try works for both synchronous code and code in
an async function.
Read more about exceptions, including stack traces, rethrow, and the difference between Error and
Exception.
Classes
Here’s an example of a class with three properties, two constructors, and a method. One of
the properties can’t be set directly, so it’s defined using a getter method (instead of a
variable).
class Spacecraft {
String name;
DateTime launchDate;
// Method.
void describe() {
print('Spacecraft: $name');
if (launchDate != null) {
int years =
DateTime.now().difference(launchDate).inDays ~/
365;
print('Launched: $launchYear ($years years ago)');
} else {
print('Unlaunched');
}
}
}
Exercise
1. Find the difference between the square of the sum and the sum of the squares of the first N
natural numbers.
2. Given a number n, determine what the nth prime is. By listing the first six prime numbers: 2,
3, 5, 7, 11, and 13, we can see that the 6th prime is 13.If your language provides methods in
the standard library to deal with prime numbers, pretend they don't exist and implement
them yourself.
3. Reverse a string
For example: input: "cool" output: "looc"