Table of contents
1.
Introduction
2.
Syntax
3.
Keywords 
4.
Identifiers 
5.
Variables 
6.
Strings 
7.
Numbers 
8.
Booleans 
9.
Collections 
10.
Operators in Dart
10.1.
Arithmetic operators
10.2.
Equality Operators
10.3.
Comparison Operators
10.4.
Logical Operators
11.
Comments in Dart
11.1.
Single-Line Comments
11.2.
Multi-Line Comments
11.3.
Dart Documentation Comments
12.
Execution of Dart Programs
13.
Frequently Asked Questions
14.
Conclusion
Last Updated: Mar 27, 2024
Easy

Dart Syntax and Comments

Author Vivek Goswami
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Dart is a static programming language developed by Google. It is one of the most popular programming languages because it supports the Flutter toolkit. Flutter is a framework that leverages dart's native compilation capabilities to generate fast cross-platform apps.

Dart is a client-oriented programming language. It is for quick app development for various platforms. It is an object-oriented, open-source, general-purpose programming language.

Some of its features are: 

  1. Statically typed 
  2. Multi-paradigm including object-oriented and functional programming 
  3. Zero safe 

 We prefer dart due to its capability to develop fast apps on various platforms.

Dart supports two types of compilation: just-in-time and ahead-time. Its syntax combines CPP, Python, Java, and JavaScript. 

This blog describes the basic syntax of the dart. Let us now dive deeper into the various introductory concepts of the dart.

Syntax

Syntax in any program determines the rules of writing a programming language. We can think of it as the grammar of the programming language. Each language specification defines its syntax. Each dart line must end with a semicolon.

The darts program consists of -variables and operators, class, function, expression and programming structure, decision making, a loop structure, remarks, Libraries, and packages.  The dart syntax is similar to the C language.  

Let's start with the traditional "Hello World" example-

main() 
{ 
   print("Hello World!"); 
}

The main () function is a Dart predefined method. This method serves as the entry point for the application. We need the main () function to run the dart script.

Print () is a predefined function that writes the specified string or value to standard output.
Output:

Hello World

Keywords 

Keywords refer to certain reserved words that often have a specified meaning. We use it to represent basic syntax that the compiler will understand. 

Some keywords are listed below:

  1. Import
  2. Else
  3. As
  4. Abstract
  5. Void
  6. Null
  7. New
  8. Return
  9. Typedef
  10. set
     

The Dart language in the program exclusively reserves several other words.

Identifiers 

The identifier is the name of a variable that the compiler can read. There are specific rules for describing identifiers. The rules are as follows:

  1. You cannot use special characters except _ and $. 
  2.  It cannot be a reserved keyword.
  3. There must be no space between the variable names. 
  4. It is case-sensitive.

Variables 

The variable is the name given to the memory location declared by writing the variable type _ name and then initialized as follows:

"variable_type name = value;". 

The dart language has the following built-in types: 

  1. String
  2. Numbers
  3. Boolean
  4. Collections

Let us now discuss these types of variables one by one. 

Strings 

Dart string is a collection of characters and characters. You can create strings using single or double-quotes. 

We can use single quotes or double quotes for multi-line strings. We can specify the value of a character in the string by running $ {expression}.

Example

var name = ‘Coding Ninjas’;

In this example, the variable is called the name, and the string value is Coding Ninjas.

Numbers 

We can use both int and double to represent numbers. We use these data types to represent arbitrary numbers and even decimals. The num type works with simple algebraic operators.

Booleans 

We use the Dart Boolean type to test whether the statement is true or false. True and False are the two types of boolean values. Both these boolean values are compile-time constants. 

Collections 

A collection of Dart is a collection of similar and dissimilar data types. We use them as data structures. 

We can access each element one by one. Collections include data structures like lists.

Operators in Dart

Dart contains all the usual operators that we have already come across in other languages ​​such as C, Swift, and Kotlin. 

The different types of operators in Dart can be classified as follows:

  1. Arithmetic Operator
  2. Equality Operator
  3. Increment and decrement Operator
  4. Comparison Operator
  5. Logical Operators

Arithmetic operators

As the name suggests, Arithmetic operators help us perform the basic arithmetic operations, including addition, subtraction, multiplication, and division. 

Equality Operators

These operators help us in identifying similar and non-similar values. We can use the == operator to denote equality and != operator to denote not equals. 

Comparison Operators

The comparison operators are of the following types:

  1. Less than (<)
  2. Greater than (>)
  3. Equal to (=)

Logical Operators

The logical OR and Logical And are also used in Dart. They help us to analyze the logical relationship between the two operands. 

Comments in Dart

Comments are snippets of code that the compiler cannot read. Comments help us to write our code efficiently. Comments make the code readable and easier to understand. 

There are three types of Comments in Dart:

Single-Line Comments

A single-line comment is specified with a '//' (double forward slash) that extends up to the newline character. This can be used to comment everything out until a line break is reached.

Let’s understand single-line comments in dart better with an example program.

Code:

void main()
{
    // It is a single-line comment that will print Hello Ninja
    print("Hello Ninja");
}

Output:

Hello Ninja

Multi-Line Comments

If you want to remark multiple lines, use the /* and */ characters; everything between /* and */ is disregarded by the compiler.

Let’s understand multi-line comments in dart better with an example program.

Code:

void main()
{
    /* 
     * It is a
     *  multi-line comment program 
     * that will 
     * print Hello Ninja
     */
    print("Hello Ninja");
}

Output:

Hello Ninja

Dart Documentation Comments

This is a unique form of comment that is mostly used to create documentation or a reference for a project or software package. Documentation comments begin with / or /** and can be multi-line or single-line. It has the same effect as a multi-line doc comment when used on consecutive lines. The Dart compiler ignores all text inside a documentation comment except for content wrapped in brackets. Classes, methods, fields, top-level variables, functions, and arguments are denoted by brackets. The names in brackets are resolved in the described program element's lexical scope.

Let’s understand dart documentation comments in dart better with an example program

Code:

/// This  
/// is  an
/// example of documentation  
/// comment 

Execution of Dart Programs

After writing the entire program as per all its specifications, we need to learn how to execute the program such that we can use our program to get the desired results. 

We can run Dart programs in two ways-

  1. Command Line
  2. Dart Console Application

Frequently Asked Questions

  1.  What does Google use Dart for?
    Dart is a client-oriented programming language developed by Google. It is created for rapid app development for various platforms. It is an object-oriented, open-source, general-purpose programming language. 
     
  2. What are the various data types in Dart?
    The various dart data types are as follows:
    Strings
    Integer
    Booleans
    Collections like Lists
     
  3. What is the use of typedef in Dart?
    Typedef in Dart is used to give a user-defined identity to a function. 

Conclusion

In this article, we have extensively discussed the Dart Syntaxes, saw how to write the hello world program, and read about keywords, operators, and comments in Dart. You can also read the blog Dart Data Types on the Coding Ninjas Website to learn about various data types available in the Dart programming language.

We hope that this blog has helped you enhance your knowledge regarding Dart Syntax and Comments knowledge. If you want to learn more, check out our Android Development Course on the Coding Ninjas Website to learn everything you need to know about Android development. Do upvote our blog to help other ninjas grow. 

Happy Coding!

Live masterclass