In this tutorial you will learn about the Dart Basic Syntax and its application with practical example.
Dart Basic Syntax
In this article, we will explore about various basic syntax in Dart Programming.
Dart Identifiers
An identifiers is a name given to program elements such as variables, array, class and functions etc. An identifier is a sequence of letters, digits, and underscores, the first character of which can not be a digit. Following rules must be kept in mind while naming an identifier.
- The first character must be an alphabet (uppercase or lowercase) or can be an underscore.
- An identifier can not start with a digit.
- All succeeding characters must be alphabets or digits.
- No special characters or punctuation symbol is allowed except the underscore”_” or a dollar sign ($).
- No two successive underscores are allowed.
- Keywords can not be used as identifiers.
Note :- Dart is a case-sensitive programming language, which means “Abc” and “abc” are not the same.
Dart Printing and String interpolation
In Dart, print() function is used to print a value and ${expression}
for string interpolation as following –
1 2 3 4 5 6 |
void main() { var name = "John"; var age = 25; print("I am ${name} I am ${age} years old"); } |
Output:-
1 2 |
Hello World I am John I am 25 years old |
Dart Comments
Comments are a set of statements that are not executed by the Swift compiler and interpreter. The use of comments makes it easy for humans to understand the source code.
Dart Single-line Comments:-
A ‘//’ (double forward slash) is used to specify a single line comment, which extends up to the newline character.
1 2 |
// It prints Hello, World! print("Hello, World!") |
Output:-
1 |
Hello World! |
Dart Multi-line Comments:-
If you want to comment multiple lines then you can do it using
/*
and */
, everything in between from /* to */ is ignored by the compiler-
1 2 3 |
/* It prints Hello World! */ print("Hello, World!") |
Output:-
1 |
Hello World! |
Semicolons In Dart
The semicolon is a statement terminator, that is each of the individual statement must be ended with a semicolon. In Dart, it is mandatory to put a semicolon (;) at the end of each of the executable statement. If you are typing multiple statements in single line, then it is necessary to use a semicolon as a delimiter. Otherwise, the compiler will raise a syntax error.
Example 1:-
1 |
var str = "Hello, World!"; |
Dart is case-sensitive. This means that Dart differentiates between uppercase and lowercase characters.
Dart Whitespace and Line Breaks
Whitespace is used to indicate blanks, tabs, newline characters, comments. Whitespace are also useful separate one part of any statement from another. In Dart, you can use spaces, tabs, and newlines in your program to format and indent your programs that make it easy to read and understand.
Block in Dart
Block is a set of statement grouped together inside opening and closing braces.In Dart, an opening and closing curly braces is used to group all of the statements in a block.
Example:-
1 2 3 4 5 |
{//start of block //block of statement(s) }//end of block |