Dart Basic syntax



While Dart shares some syntactic similarities with C++, it's important to understand that they are distinct languages with different design philosophies. Here's a breakdown:

Similarities:

  • C-style syntax:
    • Both languages use curly braces {} to define code blocks.
    • Control flow statements like if, for, while, and switch have similar structures.
    • Operators like +, -, *, /, ==, and != are largely the same.
  • Object-oriented programming:
    • Both languages support classes, objects, and inheritance.
    • They share concepts like methods and properties.
  • Static typing:
    • Both Dart and C++ are statically typed, meaning that variable types are checked at compile time. 

Differences:

  • Memory management:
    • C++ requires manual memory management, including allocating and deallocating memory using new and delete.
    • Dart uses automatic garbage collection, which simplifies memory management and reduces the risk of memory leaks.
  • Pointers:
    • C++ uses pointers, which allow direct manipulation of memory addresses.
    • Dart does not have pointers. This makes Dart safer but less suitable for low-level system programming.
  • Asynchronous programming:
    • Dart has strong built-in support for asynchronous programming using async and await, making it well-suited for building responsive user interfaces.
    • While C++ has asynchronous capabilities, Dart's implementation is often considered more streamlined.
  • Language purpose:
    • C++ is a general-purpose language used for a wide range of applications, including system programming, game development, and high-performance computing.
    • Dart is primarily designed for building user interfaces, particularly with the Flutter framework.

 

Example code


Simple Function

void greet(String name) {

  print("hello world $name");

}


void main() {

  var name = "hafiz musa";

  int number =-10;

  

  greet(name);

  

  if (number >0){

    print("more than 0");

  }else{

    print("less than 10");

  }

  

  for (int i=0;i<10;i++){

    print(i);

  }

}



OOP/Class:


class Person{
  String name;
  int age;
  
  Person (this.name,this.age);
  
  void sayHello(){
    print ("hello $name and your age is $age");
  }
  
  
  
}


void main() {
  
  Person person1 = Person("hafiz",23);
  person1.sayHello();

}
Dart Basic syntax Dart Basic syntax Reviewed by Admin on 6:44 PM Rating: 5

No comments:

Powered by Blogger.