جرّب بنفسك

ملعب Dart

ده DartPad الرسمي من فريق Dart، مجاني ومن غير تثبيت. انسخ مثال من تحت (زر نسخ)، الصقه في المحرر واضغط Run.

افتح DartPad في صفحة لوحده ↗

أمثلة من الدروس

Lesson 3: Hello, Dart
void main() {
  print('Hello, Dart!');
}
Lesson 3: Variables and print
void main() {
  String name = 'Ahmed';
  int age = 20;
  print('$name is $age years old');
}
Lesson 4: if and else
void main() {
  var age = 20;
  if (age >= 18) {
    print('Accepted');
  } else {
    print('Rejected');
  }
}
Lesson 4: else if (grades)
void main() {
  var score = 85;
  if (score >= 90) {
    print('A');
  } else if (score >= 80) {
    print('B');
  } else {
    print('C');
  }
}
Lesson 4: Separate ifs (the bug) vs else if chain
void main() {
  var grade = 90;

  // Bug: separate ifs, the last else belongs to the last if only
  if (grade >= 85) print('Excellent');
  if (grade >= 75) print('Very good');
  else print('Invalid');

  // Correct: one chain, first true wins
  if (grade >= 85) {
    print('Excellent');
  } else if (grade >= 75) {
    print('Very good');
  } else {
    print('Invalid');
  }
}
Lesson 4: && and ||
void main() {
  var age = 20;
  var hasId = true;
  if (age >= 18 && hasId) {
    print('Enter');
  }

  var isAdmin = false;
  var isOwner = true;
  if (isAdmin || isOwner) {
    print('Allowed');
  }
}
Lesson 4: Nested if
void main() {
  var age = 20;
  var hasTicket = true;
  if (age >= 18) {
    if (hasTicket) {
      print('Welcome');
    } else {
      print('Buy ticket');
    }
  }
}
Lesson 4: switch
void main() {
  var day = 3;
  switch (day) {
    case 1:
      print('Monday');
      break;
    case 3:
      print('Wednesday');
      break;
    default:
      print('Other day');
  }
}
Lesson 4: Ternary (one-line if)
void main() {
  var age = 20;
  var msg = age >= 18 ? 'Adult' : 'Minor';
  print(msg);
}
Lesson 4: Max of 3 numbers
void main() {
  var a = 10, b = 15, c = 20;
  if (a >= b && a >= c) {
    print(a);
  } else if (b >= a && b >= c) {
    print(b);
  } else {
    print(c);
  }
}
Lesson 5: Print 1 to 100
void main() {
  for (var i = 1; i <= 100; i++) {
    print(i);
  }
}
Lesson 5: The 3 parts of for (prints 0 to 4)
void main() {
  for (var i = 0; i < 5; i++) {
    print(i);
  }
}
Lesson 5: Sum 1 to 10
void main() {
  var sum = 0;
  for (var i = 1; i <= 10; i++) {
    sum = sum + i;
  }
  print(sum);
}
Lesson 5: Factorial of 5
void main() {
  var fact = 1;
  for (var i = 1; i <= 5; i++) {
    fact = fact * i;
  }
  print(fact);
}
Lesson 5: Even numbers (two ways)
void main() {
  // 1) step by 2
  for (var i = 2; i <= 100; i += 2) {
    print(i);
  }

  // 2) check with %
  for (var i = 1; i <= 100; i++) {
    if (i % 2 == 0) {
      print(i);
    }
  }
}
Lesson 5: Prime check with break and a flag
void main() {
  var n = 7;
  var isPrime = true;
  if (n <= 1) {
    isPrime = false;
  } else {
    for (var i = 2; i < n; i++) {
      if (n % i == 0) {
        isPrime = false;
        break;
      }
    }
  }
  print('$n prime? $isPrime');
}
Lesson 5: while (countdown)
void main() {
  var i = 3;
  while (i > 0) {
    print(i);
    i--;
  }
}
Lesson 5: do-while
void main() {
  var i = 0;
  do {
    print(i);
    i++;
  } while (i < 3);
}
Lesson 5: break and continue
void main() {
  for (var i = 1; i <= 10; i++) {
    if (i == 4) break;
    print(i);
  }

  for (var i = 1; i <= 5; i++) {
    if (i == 3) continue;
    print(i);
  }
}
Lesson 5: Nested loops
void main() {
  for (var r = 1; r <= 3; r++) {
    for (var c = 1; c <= 3; c++) {
      print(r * c);
    }
  }
}