Loading the lesson...
In the print dialog, choose "Save as PDF" as the destination.
Operating Systems II - Lab 1
Variables, control flow, functions, and object-oriented programming in Dart, the language behind Flutter
dartpad.dev), no install, runs instantly in the browser, great for quick experimentsDart SDK plus VS Code (with the Dart extension) or Android Studiodart --versiondart create my_first_dart_project, then dart run to execute itmy_first_dart_project/
bin/
entry point
lib/
reusable code
test/
unit tests
pubspec.yaml
config
main() functionprint() outputs text to the consolevoid means the function returns nothingvoid main() {
print("Dart language is easy to learn");
}| Keyword | Meaning |
|---|---|
var | Type inferred from the assigned value; explicit types such as int age = 25; are allowed and encouraged for clarity |
dynamic | Type can change at runtime |
final | Value set at runtime, cannot change after |
const | Value fixed at compile time |
var name = "Dart";
int age = 25;
final a = 12;
const pi = 3.14;int for whole numbers, double for decimalsbool holds only true or falseString name = "Ahmed";
print("Hello, $name!");
print("Next year I'll be ${25 + 1}");| Type | What it is |
|---|---|
List | An ordered collection, Dart's replacement for arrays |
Map | Key-value pairs, accessed by key |
Set | A collection of unique values |
dynamic | Declared without a fixed type; can hold any value and change type later |
var list = [1, 2, 3, 4, 5];
var mapping = {'id': 1, 'name': 'Dart'};
print(mapping['name']); // Dartif, if-else, else-if, and switchint score = 85;
if (score >= 90) {
print("Excellent");
} else if (score >= 75) {
print("Good");
} else {
print("Needs Improvement");
}for: classic counter-based loopfor-in: iterates directly over a collection's itemswhile: repeats while a condition stays truedo-while: runs the body at least once, then checks the conditionCheck condition
Run code block
Update counter
for (var i = 1; i <= 5; i++) {
print(i);
}void (nothing) or a typed valueint multiply(int a, int b) {
return a * b;
}
void main() {
int result = multiply(5, 3);
print("Result: $result"); // 15
}Fields
Getters and setters
Constructor
Methods
class Employee {
String name = "";
void result() {
print(name);
}
}
void main() {
Employee emp = Employee();
emp.name = "employee1";
emp.result(); // employee1
}switch to compute the result1 to 20, substituting Fizz/Buzz/FizzBuzz by divisibility rulesStudent class with isHonors() and displayInfo()isPrime(int number) and test it against several valuesDart uses var/explicit types for variables, final for runtime constants, and const for compile-time constantsint, double, String, bool, plus collections List, Map, and Setif/else/switch for decisions and four loop styles for repetitionDart is object-oriented: classes define fields, constructors, getters/setters, and methodsMahmoud Abas|Lab 1: Introduction to Dart Programming