Dart Programming Part-1 : Environment & First Dart Code

Comments, Object-Oriented Programming, Variables(Final and Const) & Decision Making

Jayesh Pansheriya
Analytics Vidhya
5 min readDec 29, 2019

--

Dart is an open-source general-purpose programming language. It is originally developed by Google and later approved as a standard by ECMA. Dart is a new programming language meant for the server as well as the browser. Introduced by Google, the Dart SDK ships with its compiler — the Dart VM. The SDK also includes a utility -dart2js, a transpiler that generates JavaScript equivalent of a Dart Script. This tutorial provides a basic level understanding of the Dart programming language.

Google has released a special build of Chromium — the Dart VM. Using Dartium means you don’t have to compile your code to JavaScript until you’re ready to test on other browsers.

Dart Programming — Environment

You may test your scripts online by using the online editor

Your First Dart Code

Let us start with the traditional “Hello World” example −

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

The output of the above code will be −

Hello World!

Comments in Dart

  • Single-line comments ( // ) − Any text between a “//” and the end of a line is treated as a comment
  • Multi-line comments (/* */) − These comments may span multiple lines.
// this is single line comment  

/* This is a
Multi-line comment
*/

Object-Oriented Programming in Dart

Dart is an Object-Oriented language. Object Orientation is a software development paradigm that follows real-world modelling. Object Orientation considers a program as a collection of objects that communicate with each other via mechanism called methods.

  • Object − An object is a real-time representation of any entity. As per Grady Brooch, every object must have three features −
  • State − described by the attributes of an object.
  • Behavior − describes how the object will act.
  • Identity − a unique value that distinguishes an object from a set of similar such objects.
  • Class − A class in terms of OOP is a blueprint for creating objects. A class encapsulates data for the object.
  • Method − Methods facilitate communication between objects.

Variables

Variable names are called identifiers. Following are the naming rules for an identifier −

  • Identifiers cannot be keywords.
  • Identifiers can contain alphabets and numbers.
  • Identifiers cannot contain spaces and special characters, except the underscore (_) and the dollar ($) sign.
  • Variable names cannot begin with a number.

A variable must be declared before it is used. Dart uses the var keyword to achieve the same. The syntax for declaring a variable is as given below −

var name = 'Jayesh';
String name = 'Jayesh';
int num = 10;

Final and Const

The final and const keyword are used to declare constants. Dart prevents modifying the values of a variable declared using the final or const keyword. These keywords can be used in conjunction with the variable’s data type or instead of the var keyword.

The const keyword is used to represent a compile-time constant. Variables declared using the const keyword are implicitly final.

final variable_name
OR
final data_type variable_name
const variable_name
OR
const data_type variable_name

Decision Making

A conditional/decision-making construct evaluates a condition before the instructions are executed.

if , if..else , else..if ladder and Switch..case :

An if statement consists of a Boolean expression followed by one or more statements.

An if can be followed by an optional else block. The else block will execute if the Boolean expression tested by the if block evaluates to false.

The else…if ladder is useful to test multiple conditions. Following is the syntax of the same.

The switch statement evaluates an expression, matches the expression’s value to a case clause and executes the statements associated with that case.

The following example shows how you can use the if statement in Dart.void main() { 
var num=5;
if (num>0) {
print("number is positive");
}
}
Output:
number is positive
--------------------------------------------------------------------The following example prints whether the value in a variable is even or odd. The if block checks the divisibility of the value by 2 to determine the same.void main() {
var num = 12;
if (num % 2==0) {
print("Even");
} else {
print("Odd");
}
}
Output:
Even
--------------------------------------------------------------------The following program code checks whether a given value is positive, negative, or zero.void main() {
var num = 2;
if(num > 0) {
print("${num} is positive");
}
else if(num < 0) {
print("${num} is negative");
} else {
print("${num} is neither positive nor negative");
}
}
Output:
2 is positive
--------------------------------------------------------------------The example verifies the value of the variable grade against the set of constants (A, B, C, D, and E) and executes the corresponding blocks. If the value in the variable doesn’t match any of the constants mentioned above, the default block will be executed.void main() {
var grade = "A";
switch(grade) {
case "A": { print("Excellent"); }
break;

case "B": { print("Good"); }
break;

case "C": { print("Fair"); }
break;

case "D": { print("Poor"); }
break;

default: { print("Invalid choice"); }
break;
}
}
Output:
Excellent

How Can You Contribute?

Posts In This Series

Show Your Support

Press the clap button below if you liked reading this post. The more you clap the more it motivates me to write better!

--

--