×

Dart Basic Program

Dart provides various convenient platforms to code and compile the program in Dart language. Some of them are:

1. Using IDE = Visual Studio code is a text editor with IDE. All you have to do is download the Dart extension, write your code with the .Dart extension and compile using the same.

2. Online Compiler = DartPad is an online compiler provided by the Dart language to code and compile the program in Dart.

3. Using Command line = After downloading Dart SDK, one can easily code in Dart using Dart specific commands in the command line client.

In case you haven’t downloaded it, visit the previous tutorial that discusses the installation of the above platforms in detail.

Let’s write our first program in Dart.

Program:

//This is the first Dart program

void main( )

{

  var a = 5, b = 6 ;

  var c = a + b ;

  print( ' Sum of two numbers is $c. ' ) ;

}
  • Using Visual Studio Code
    • Open Visual Studio Code.
    • Type your code.
    • Save the file with the .Dart extension.
    • Run the code using F5 or the run button on the top right corner.
Dart Basic Program
  • Using online compiler DartPad.
    • Search on Google DartPad.
    • Type your code and run it.
Dart Basic Program
  • Using the command line.
    • Open notepad and type your code.
    • Save the file with the file name as ‘name.Dart’. .Dart to recognize that this is a Dart file.
    • Open the command prompt and write the command ‘Dart filename’. Here, the ‘filename’ is to be replaced with the name of your file.
    • Press enter, and output will be displayed.
Dart Basic Program Dart Basic Program

Let’s understand the program in detail.

This program initializes two numbers, adds them and print their sum.

//This is the first Dart program

void main( )

{

  var a = 5, b = 6;

  var c = a + b;

  print( 'Sum of two numbers is $c.' );

}
  1. This is the first Dart program

The first line beginning with ‘//’ is a single-line comment. Comments do not get executed but are written in order to make the code more descriptive and easier to understand.  

Dart also supports multi-line comments in case of multiple lines as follows:

/*…………………

……………………*/

  • main( )

Like in C, the execution of the program always and only begins from the main( ) function.

  • void

Return type of the function main( ) is void. Return type informs the compiler about the type of value the function will return. void means that the function does not return any value.
It can have other return types as well like, int, double, etc.

  • The whole program is contained within the curly braces { }.  Remember that group of programming statements will always be enclosed within the curly braces {  }.
  • var a = 5, b = 6;

This is the single line, multiple variable initialization statement. ‘var’ is a keyword used to declare a variable without specifying any data type. The Dart compiler automatically interprets the data type of the variable by looking at the value initialized to it. For example, here, the values are integer type.

We can specify the data type explicitly also instead of var keyword. Initialization will look like this in that case,

int a = 5, b = 6;

  • var c = a + b;

This is an assignment statement that performs the arithmetic operation, addition on variables ‘a’ and ‘b’ using plus (addition)  operator ‘+’, and assigns the value of sum on the right-hand side to the variable ‘c’ on the left-hand side using assignment operator ‘=’.

An important point to remember is that in the case of an assignment statement, the value on the right-hand side is always assigned to the variable on the left-hand side. The converse is not true.

  • print(‘Sum of two numbers is $c’);

print is an inbuilt function to display output on the output screen. The text to be printed on the output screen is enclosed within single quotes ‘   ’. ‘$’ operator is used to convert an expression to a string using string interpolation.

  • Remember that every statement is terminated using a terminator ‘;’.

Related Topics

Dart Recursion

Recursion is one of the most important and interesting concepts in any programming language. It can be defined as a process in which a function calls itself directly or indirectly....

5 minutes read.

Abstract Classes in Dart

Abstract classes in Dart are those classes that only contain abstract methods  (methods that do not contain any implementation). These classes are specifically designed for the purpose of inheritance. We...

4 minutes read.

Dart Arithmetic Operators

Arithmetic Operators comprises of all the operators that are used to perform arithmetic operations such as addition, subtraction, multiplication, division, etc. They are binary operators that work upon two operands. Consider A...

2 minutes read.

Dart Built-in Data Types

A data type defines the type of value a variable can hold, such as integer, double, or string.  Dart supports two data types:  1. Built-in data types : These are the data...

1 minute read.

Dart Future and Stream

Before understanding Future and Stream in Dart, we need first to get familiar with two terms : Synchronous and Asynchronous Programming.  In synchronous programming, a single operation is handled at a...

9 minutes read.

Lexical Scope and Closure

Lexical Scope : Lexical scope is a very important term in any programming language. It defines the scope of the variable based on the block it is contained inside. According to...

2 minutes read.

Dart Comments

Comments are the statements that are used to enhance the readability and understandability of the code. The compiler does not execute these lines. It simply ignores these comments when it scans...

2 minutes read.

Dart Exception Handling

Exception Class – An exception is any runtime error that leads to abrupt termination of the program. It helps in addressing the error programmatically. It is aimed to be caught...

4 minutes read.

Dart Sets

Sets data type in Dart A set is an unordered collection of items that are unique. Dart infers that sets tale strings as values. Example, var languages = { ‘Dart’ , ‘Flutter’, ‘Python’,...

3 minutes read.

Dart HTML DOM

All web pages can be viewed as objects and exist within the browser window. We can access a web page using a web browser and it needs to be connected...

3 minutes read.

Dart Break and Continue

Loop statements are used to change the normal sequence of flow of the program. Dart supports two types of loop control statements: Break StatementContinue Statement Break Statement : The ‘ break ’ statement is...

4 minutes read.

Dart super keyword

The ' super ' keyword is used to refer to the immediate parent class object of the currently in-use child class. Using this keyword, we can invoke the superclass (...

6 minutes read.

Dart Operators Precedence and Associativity

Precedence of operators determines the order in which the operators are evaluated if they are grouped together in a sentence. If two operators share an operand then the one with...

5 minutes read.

Dart - Control Flow Statements

The control flow statements are used to define the control on the flow of the program. Dart programs are executed in sequential order i.e., the order in which the programming...

1 minute read.

Dart Variables

Variables are used to store the value of a particular data type referred to in the whole program by a name or identifier. They refer to a memory location as...

5 minutes read.

Dart Tutorial

Dart is an open-source, structured programming language developed by Google. It is a high-level programming language that emerged in 2011, but its stable version emerged in 2017. It is largely used...

4 minutes read.

Dart Isolates

Dart successfully supports asynchronous programming which runs our program without any hindrance. This asynchronous programming is used to achieve concurrency. Dart supports concurrent programming with features such as ‘ async-await...

9 minutes read.

Dart Assignment Operator

Assignment operators are the operators that assign value to the variables. The value on the right-hand side is assigned to the variable on the left-hand side. We can also use...

4 minutes read.

Golang vs Dart

Go is the procedural programming language. It was founded in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson at Google but was launched in 2009 as the language of...

2 minutes read.

Dart Booleans

Booleans Dart has a data type named ‘bool’ to represent Boolean values : true and false, which are compile-time constants. ‘True’ or ‘False’ cannot be assigned with values 1 or 0. Example, bool...

1 minute read.