Dart: Statements

Dart: Statements

Understand what is a statement actually.

A statement is a unit of code that performs some action. In Dart, statements end with a semicolon. Semicolon are optional. There are two kinds of statements:

  1. Simple statements (usually one line of code)

  2. Block statements (having one or many blocks of code)

Block statements are created using curly brackets {...}


Keywords

Most statements start with a keyword. This is an English word. Some statements may use multiple words, some are optional some are mandatory.

Dart has significantly fewer keywords compared to Java. Some key facts:

  • Dart has 33 keywords in total as of Dart 2.16. This includes keywords like class, extends, abstract, get, set, var, etc.

  • In comparison, Java has around 52 keywords as of Java 15. This includes keywords like public, protected, private, final, static, etc. which Dart does not have.

  • Some of the reasons Dart has fewer keywords are:

    • Dart does not have access modifiers like public, private, protected. Everything is public by default.

    • Dart does not have static keyword. Classes are final by default.

    • Dart does not have final keyword. Variables are final by default unless declared var.

  • The reduced number of keywords in Dart makes the language simpler and more concise compared to Java.

So in summary, Dart has significantly fewer keywords (around 35% less) compared to Java because:

  • Dart omits some keywords like access modifiers, static, final that are present in Java

  • Things like class visibility and variable mutability are handled differently in Dart, which reduces the need for certain keywords.

This makes Dart a more minimal and concise language, while still being expressive and powerful. However, Dart borrows some concepts and syntax from Java to make it familiar for Java developers.


Assign statement

You're right, = represents an assignment statement in Dart, not an expression like in Python.

In Dart:

  • An assignment uses = and is a statement. It performs an action (assigning a value).

For example:

var a = 10;  // Assignment statement
  • Expressions evaluate to a value. They are used in statements.

For example:

var b = a + 5; // a + 5 is an expression, = is a statement

So in Dart, = is used to assign values in a statement, it does not represent an expression itself.

In Python, = represents an expression that evaluates and assigns a value. It is used like this:

a = b = 10  # This is an expression

So in summary, the difference is:

In Dart:

  • \= represents an assignment statement

  • It is used to assign a value to a variable

In Python:

  • \= represents an expression that evaluates and assigns a value

  • It is used to both evaluate and assign a value in one step

So, = represents a statement in Dart, not an expression like it does in Python. The semantics around = are different between the two languages.


All Keywords

Here is a table of Dart keywords grouped by category with descriptions:

CategoryKeywordDescription
ClassesclassDeclares a class
extendsUsed to inherit from another class
abstractDeclares an abstract class
VariablesvarDeclares a variable
finalDeclares a final variable
FunctionsFunctionDeclares a function
callCalls a function
Control FlowifConditional statement
elseElse block for if statement
forFor loop
whileWhile loop
doDo-while loop
switchSwitch statement
caseCase block for switch statement
breakBreaks out of a loop or switch block
continueContinues to next iteration of a loop
returnReturns a value from a function
LibrariesimportImports a library
libraryDeclares a library
partPart of a library
TypestypedefDefines an alias for a type
dynamicThe dynamic type
voidThe void type
boolThe bool type
intThe int type
doubleThe double type
numSupertype for numbers
StringThe String type
NullThe null type

I will try to find the details for each keyword. Do not bother to learn all these keywords right now. All in good time. Learning Dart is a pleasure because it has such a concise syntax.


Disclaim: Almost the entire article is created by Rix. My contribution is very small. You can do such articles but only after you have learned several languages. However, you can ask new questions and add comments below.