A Beginners Guide to Dart - Control Flow and Low Level Compilation - Part Two
What Will I Learn?
- You will learn the difference between JIT and AOT compilation
- You will learn about Control Flow Structures
- You will learn why Types are important
- You will learn how to make Loops
- You will learn about Boolean Types and Boolean Algebra
- You will learn about Conditions in Dart
OS Support for Dart:
- The Dart SDK
- A dart supported text editor
- A little time to sit and watch a video and some patience to learn the language
Resources for Dart:
- Dart Website: https://www.dartlang.org/
- Dart Official Documentation: https://www.dartlang.org/guides/language/language-tour
- Dart GitHub repository: https://github.com/dart-lang/sdk
- Awesome Dart GitHub Repository: https://github.com/yissachar/awesome-dart
- Pub website: https://pub.dartlang.org
Dart Logo (Google): https://www.dartlang.org/
In this Dart video tutorial, we take a much lower level look at some of the topics that were covered in the last tutorial. We also go ahead and take a look at control flow structures like
while. We look at conditionals and Boolean types and how they relate to the other control flow structures. We also talk about why Types are important and the implications of Dart being a static language. This includes talking about the
Dart as a Statically Typed Language and its Relation to the Compiler
Up until the release of Dart 2.0, Dart was a dynamically typed programming language. This meant that when you compiled a Dart program, the types were checked at run-time rather then at compile type. This also meant that you didn't need to supply a type for a variable, function or object and the compiler would infer these things for you. When Dart used this method of type checking, it also relied heavily on its Just In Time (JIT) compiler. As Dart moved into Dart 2.0, the type system was shifted from Dynamic to Static. With static typing, the types are known at compile type rather then during run time. This not only boosts the performance of the language significantly, but it also makes the Ahead of Time (AOT) compiler work much better. Now when you compile a Dart program, the compiler can suggest fixes and give clearer errors to the user.
The image above shows the process by which you can interact with your computer through the Dart programming language. You write your Dart code and submit it to the compiler and virtual machine. The Virtual Machine produces machine readable byte-code so that the computer can follow the instructions from the code. This then produces an output based on what the program is. Then the user can take this output and change their program accordingly. Because Dart used to be dynamic, it contains both a JIT and AOT compiler and it also has types that act like dynamic types. These types are the Object type and the dynamic type. The dynamic type is a bit of a catch all type, if the compiler can't infer the type and the user doesn't supply one, then it is assumed to be dynamic. The Object type on the other hand is the type that all other types are derived from.
Using Control Flow Structures in Dart
In Dart, because the language is imperative we have access to many of the C-style control flow structures. This of course, includes structures like
for. Each of these structures uses a set of logic to allow the program flow to change. Some allow for code re-use; such is the case for the looping structures and others allow the program to branch from one path to another. This not only makes it easy for the developer to control the flow of the program but it also helps keep the code easy to reason about.
In the image above, we have some examples of
if/else statements. Also at the bottom, there is a list of the conditionals that are included in the Dart programming language. The
if/else structure allows the user to control the flow of the program based on a conditional statement which appears in the parenthesis. If the initial conditional evaluates to true, then the first block of code is executed. If the conditional evaluates to false, then the program skips over the first block of code and moves on to the next block. If another
if statement follows the
else keyword, the compiler checks the conditional statement and follows the same pattern as before.
The Source Code for this video may be found here: https://github.com/tensor-programming/dart_for_beginners/tree/tensor-programming-patch-1