Summary - Dart basics
- Dart course
- Beginner
- Lesson 6/116
- 5 min
Combine main, print and comments to write your first Dart program.
Summary - Dart basics
In this group you learned the building blocks of Dart: the main entry point, output with print, comments with // and /* */, and string interpolation with $.
Combining these four concepts, you can write a real program. Now we will practice with two small tasks that use the group's concepts.
As you add variables, types and functions later, this is the skeleton your programs will grow on.
Practice Greet a user
Declare a user variable (set to Zoe). Add a // comment line above describing the program's purpose. Then print Welcome Zoe using interpolation.
Starter code
// Add comment, user and printSample solution
void main() {
// Program that greets a user
var user = 'Zoe';
print('Welcome $user');
}Expected output
Welcome Zoe
Practice Mini program
Write a program with: a // comment, void main, and inside it a variable called topic (set to Dart). Use print to output Topic: Dart (with interpolation).
Starter code
// Add comment, topic and printSample solution
void main() {
// This is a mini program
var topic = 'Dart';
print('Topic: $topic');
}Expected output
Topic: Dart
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What is the return type of the main function?
void main() { ... }- void Correct answer
- int
- String
- bool
Explanation: main returns no value; that is why it is void.
2 City string
var city = 'Istanbul'; print('I live in ____city');
var city = 'Istanbul'; print('I live in $city');
Explanation: $city embeds the city name in the string.
3 Match all concepts
| Term | Meaning |
|---|---|
void main |
Entry point |
// Comment |
Single-line note |
$name |
Variable embedding |
print |
Produces output |