On this page28 sections
- Prerequisites
- Connection With Previous Lectures
- Learning Objectives
- 1. What Is a Program?
- 2. Creating Your First File
- 3. Running the File
- 4. main() — Where Every Program Starts
- 5. print() — Showing Output
- 6. Printing Text
- 7. Printing Numbers
- 8. Statements and Semicolons
- 9. Braces and Indentation
- 10. Why Comments?
- 11. Single-Line Comments //
- 12. Multi-Line Comments /* */
- 13. Documentation Comments ///
- 14. Commenting Out Code
- 15. Writing Good Comments
- 16. A Real-World Example
- Code Examples
- Common Beginner Mistakes
- Best Practices
- Mini Challenge
- Practice Questions
- Practice Answers
- Quick Revision
- Key Takeaways
- What's Next?
Lecture 03 — Your First Dart Program & Comments
| Category | 1 — Dart Fundamentals |
| Lecture | 003 of 140 |
| Estimated video length | 19 min |
| Target | Dart 3.12.2 |
| Level | Beginner |
Dart is installed, but it has nothing to run yet. Today you write a real program, run it, and learn to leave notes for humans inside the code that Dart quietly skips.
Contents
Prerequisites · Connection · Objectives · Lesson · Code Examples · Common Mistakes · Best Practices · Mini Challenge · Practice Questions · Answers · Takeaways
Every error message in this lecture is real. Each one was produced by running the code shown on Dart 3.12.2, so you will recognize it when you meet it yourself.
Prerequisites
Students should already understand, from Lectures 001–002:
- what Dart is and what it is used for
- how to install the Dart SDK and check it with
dart --version - how to open a terminal and a code editor
Connection With Previous Lectures
In Lecture 002, the only thing we asked Dart to do was report its own version:
dart --versionThat proves Dart is installed, but it does not do anything for us. Dart is ready to follow instructions — we just have not written any. How do we write instructions in a file, and how do we make Dart carry them out? And once a program grows, how do we explain to other people (and our future selves) what each part is for?
Learning Objectives
By the end of this lecture you will be able to:
- Create a
.dartfile and run it withdart run - Explain why every program needs a
main()function - Use
print()to display text and numbers - End each statement with a semicolon
- Use curly braces and indentation to group code
- Write single-line, multi-line, and documentation comments
- Comment out code to switch it off without deleting it
- Recognize the error messages Dart produces for common first-program mistakes
1. What Is a Program?
A program is a list of instructions that the computer carries out, one after another.
Analogy — think of a recipe. Each line tells the cook one thing to do, in order: "boil the water", "add the rice", "wait ten minutes". A Dart program is a recipe for the computer, and Dart is the cook who follows it exactly as written.
The cook does not guess. If a step is written wrongly, the cook stops and tells you which step is wrong. Dart behaves the same way, as you will see in this lecture.
2. Creating Your First File
Dart programs live in plain text files that end in .dart. Create a folder for your practice
work, open it in your editor, and create a file named hello.dart. Type this into it:
void main() {
print('Hello, World!');
}Type it yourself rather than copying it. Every bracket, quote, and semicolon matters, and your fingers learn the shape of Dart faster than your eyes do.
3. Running the File
Open a terminal in the same folder as hello.dart and run:
dart run hello.dartHello, World!Congratulations — that is your first Dart program. dart run reads the file, checks it, and
carries out its instructions. The shorter dart hello.dart does the same job.
If the terminal is in a different folder, or the name is misspelled, Dart cannot find the file:
dart run helo.dartCould not find file `helo.dart`4. main() — Where Every Program Starts
When you run a file, Dart looks for a function called main and starts there. Everything
inside main()'s curly braces runs; then the program ends.
void main() {
print('The program starts here.');
}The program starts here.For now, treat void main() { and the closing } as the frame every program sits in.
We will study functions — including what void and the brackets () mean — in Lecture 030.
If Dart cannot find main, it refuses to start. Here it is misspelled as mian:
void mian() {
print('Hello');
}Invoked Dart programs must have a 'main' function defined:
https://dart.dev/to/main-function5. print() — Showing Output
print() displays whatever you put between its brackets, then moves to a new line.
void main() {
print('Welcome to the Dart course');
}Welcome to the Dart courseThe value inside the brackets is what gets printed. print() takes exactly one value.
To print several things, use several print() lines.
6. Printing Text
Text in Dart is called a string, and it must be wrapped in quotes. Single quotes and double quotes both work:
void main() {
print('Ali');
print("Sara");
print("It's Zayan's turn");
}Ali
Sara
It's Zayan's turnMost Dart code uses single quotes. Double quotes are handy when the text itself contains an apostrophe, as in the third line. We will study strings in depth in Lecture 008.
7. Printing Numbers
Numbers are written without quotes. Dart can also work out simple arithmetic before printing:
void main() {
print(20);
print(99.5);
print(2 + 3);
print('2 + 3');
}20
99.5
5
2 + 3Look at the last two lines. Without quotes, 2 + 3 is a calculation, so Dart prints 5. With
quotes, it is just text, so Dart prints the characters exactly. We will study arithmetic in
Lecture 017.
8. Statements and Semicolons
Each instruction that ends in a semicolon ; is a statement. Dart runs statements from
top to bottom, in the order you write them:
void main() {
print('First');
print('Second');
print('Third');
}First
Second
ThirdThe semicolon marks where one statement ends. Forget it, and Dart stops before running anything:
void main() {
print('Hello')
}Error: Expected ';' after this.
print('Hello')
^The ^ points to the exact spot. Dart reads the whole file before running it, so even a
mistake on the last line means no line runs.
9. Braces and Indentation
The curly braces { and } group statements into one block. Here, they mark which statements
belong to main(). Every { needs a matching }:
void main() {
print('Hello');Error: Can't find '}' to match '{'.
void main() {
^The two spaces in front of each statement are indentation. Dart does not need them — but
people do. They show at a glance what sits inside the braces. Dart's own formatter adds them
for you when you run dart format hello.dart. We will study the formatter in Lecture 114.
10. Why Comments?
A comment is a note written inside the code for humans. Dart skips comments completely — they never run and never appear in the output.
Comments explain why something is done, mark sections of a long program, and help the next person (often you, a month from now) understand the code quickly. Dart has three kinds.
11. Single-Line Comments //
Two slashes start a comment that lasts until the end of that line. It can take a whole line, or sit after a statement:
void main() {
// Greet the student
print('Hello, Ali'); // shown first
print('Hello, Sara'); // shown second
}Hello, Ali
Hello, SaraEverything after // is ignored, so the output contains only the two greetings. This is the
comment you will use most often.
12. Multi-Line Comments /* */
For a longer note, start with /* and end with */. Everything in between is ignored, across
as many lines as you like:
void main() {
/*
This program prints a short welcome.
It was written for the Dart course.
*/
print('Welcome!');
}Welcome!The closing */ is required. Without it, Dart treats the rest of the file as a comment:
void main() {
/* this comment never ends
print('Hello');
}Error: Comment starting with '/*' must end with '*/'.
/* this comment never ends
^
Error: Can't find '}' to match '{'.
void main() {
^The second error happens because the closing } was swallowed by the comment.
13. Documentation Comments ///
Three slashes start a documentation comment (doc comment). It describes the piece of code directly below it:
/// Prints a welcome message for new students.
///
/// This is the first program of the Dart course.
void main() {
print('Welcome, new student!');
}Welcome, new student!Dart ignores doc comments when running, just like the others. The difference is that editors
show them when you hover over the code's name, and tools can turn them into a documentation
website. Use /// above code you want to describe, and // for notes inside it.
We will study documentation tools in Lecture 125.
14. Commenting Out Code
Putting // in front of a statement switches it off without deleting it. This is called
commenting out:
void main() {
print('Line one');
// print('Line two');
print('Line three');
}Line one
Line threeThis is useful when testing: turn a line off, run the program, then remove the // to turn it
back on. In most editors, pressing Ctrl + / (or Cmd + / on a Mac) toggles it for you.
15. Writing Good Comments
A good comment tells the reader something the code cannot. Compare:
void main() {
// Bad: repeats what the code already says
// print the number 1500
print(1500);
// Good: explains what the number means
// Total for 3 notebooks at 500 each
print(1500);
}1500
1500Both comments are legal, but only the second one helps. If a comment just repeats the code, leave it out.
16. A Real-World Example
/// Prints a shopping receipt for Sara.
void main() {
// Header
print('=== Receipt ===');
print('Customer: Sara');
// Items: 3 notebooks at 500 each
print('Notebooks x 3');
print(500 * 3);
// print('Discount applied'); // no discount today
// Footer
print('Thank you for shopping!');
}=== Receipt ===
Customer: Sara
Notebooks x 3
1500
Thank you for shopping!Everything from this lecture appears here: main() as the starting point, statements that run
in order, text and a calculated number, a doc comment describing the program, // comments
marking each section, and one line switched off.
Code Examples
Example 1 — Hello, World
void main() {
print('Hello, World!');
}Hello, World!Example 2 — Several Lines in Order
void main() {
print('Ali');
print('Sara');
print('Zayan');
}Ali
Sara
ZayanExample 3 — Text and Numbers
void main() {
print('Age');
print(20);
print('Price');
print(99.5);
}Age
20
Price
99.5Example 4 — A Blank Line
void main() {
print('Section one');
print('');
print('Section two');
}Section one
Section twoprint('') prints an empty string, which shows up as a blank line. Leaving the brackets empty
— print() — is an error, because print() always needs one value.
Example 5 — Single-Line Comments
void main() {
// This line is ignored
print('Comments are skipped'); // and so is this part
}Comments are skippedExample 6 — Multi-Line and Doc Comments
/// Shows how the three comment styles look together.
void main() {
/*
None of these lines
appear in the output.
*/
print('Only this line is printed'); // a single-line comment
}Only this line is printedHow it works
- The
///line describesmain()for readers and tools. - The
/* */block spans four lines and is skipped entirely. - The
//note after the statement is skipped too, so one line is printed.
Example 7 — Student Report Card
/// Prints a simple report card for Zayan.
void main() {
print('Student: Zayan');
print('Grade: 1');
// Marks out of 100
print('Math: 90');
print('English: 85');
// Total of the two marks
print(90 + 85);
}Student: Zayan
Grade: 1
Math: 90
English: 85
175How it works
- The first four statements print text exactly as written inside the quotes.
90 + 85has no quotes, so Dart calculates it and prints175.- The comments label the sections but never appear in the output.
Example 8 — Shopping Total
void main() {
print('Pens x 4');
print(25 * 4); // 4 pens at 25 each
print('Bag x 1');
print(1200); // one bag
}Pens x 4
100
Bag x 1
1200How it works
'Pens x 4'is text, so thexis printed as a letter.25 * 4has no quotes, so*multiplies and Dart prints100.- The end-of-line comments explain each number for the reader.
Example 9 — Bank Balance Notice
/*
Monthly notice for Ali's account.
Amounts are in rupees.
*/
void main() {
print('Account holder: Ali');
print('Opening balance:');
print(1000);
print('Closing balance:');
print(1000 - 250); // after one purchase of 250
}Account holder: Ali
Opening balance:
1000
Closing balance:
750How it works
- A multi-line comment can sit above
main()as well as inside it. 1000 - 250is calculated to750before it is printed.- Each label and its number are on separate lines, because one
print()shows one value.
Example 10 — Temperature Report
void main() {
print('Today in Lahore');
print(31.5); // degrees Celsius
// print('Rain expected'); // switched off: no rain today
print('Have a sunny day!');
}Today in Lahore
31.5
Have a sunny day!How it works
31.5is a decimal number and prints exactly as written.- The rain line is commented out, so it does not run.
- Removing the first
//on that line would switch it back on.
Common Beginner Mistakes
1. Forgetting the semicolon
void main() {
// print('Hello') // ✗ no semicolon
}Error: Expected ';' after this.Every statement ends with ;.
2. Writing Print with a capital P
void main() {
// Print('Hello'); // ✗
}Error: Method not found: 'Print'.Dart is case-sensitive. print and Print are different names, and only print exists.
3. Forgetting the quotes around text
void main() {
// print(Hello); // ✗
}Error: Undefined name 'Hello'.Without quotes, Dart thinks Hello is the name of something and cannot find it. Write
print('Hello');.
4. Mismatched or missing quotes
void main() {
// print('Hello"); // ✗ starts with ' but ends with "
}Error: String starting with ' must end with '.A string must end with the same kind of quote it started with.
5. An apostrophe inside single quotes
void main() {
// print('It's Ali'); // ✗
}Error: String starting with ' must end with '.The apostrophe in It's ends the string early. Use double quotes: print("It's Ali");.
6. A missing closing brace
// void main() {
// print('Hello');
// ✗ no closing }Error: Can't find '}' to match '{'.7. Giving print() two values
void main() {
// print('Ali', 'Sara'); // ✗
}Error: Too many positional arguments: 1 allowed, but 2 found.print() takes exactly one value. Use two print() statements.
8. Running from the wrong folder
Could not find file `hello.dart`The terminal must be in the folder that contains the file.
Best Practices
- Name files in lowercase with underscores, such as
hello_world.dart. - Put one statement on each line.
- Indent the inside of every
{ }block by two spaces — or letdart formatdo it. - Prefer single quotes for text; use double quotes when the text contains an apostrophe.
- Use
//for notes inside code and///to describe what a piece of code is for. - Write comments that explain why, not comments that repeat the code.
- Delete commented-out code once you no longer need it.
- Run your program often, after every few lines, so errors stay small and easy to find.
Mini Challenge
Write a program that prints Sara's student card. Add a doc comment above main(), a //
comment above the marks, and calculate the total of 80 and 75 with Dart.
Your program should print exactly:
--- Student Card ---
Name: Sara
Math: 80
Science: 75
155Try it yourself before opening the solution.
Show solution
/// Prints a student card for Sara.
void main() {
print('--- Student Card ---');
print('Name: Sara');
// Marks out of 100
print('Math: 80');
print('Science: 75');
print(80 + 75);
}--- Student Card ---
Name: Sara
Math: 80
Science: 75
155How it works
- The
///comment describes the program; the//comment labels the marks. - The text lines print exactly what is inside the quotes.
80 + 75has no quotes, so Dart calculates155.
Practice Questions
Work through these in order — they run from beginner to application to challenge. Every question gives you exact values, so you can compare your output with the answers.
Beginner
- Write a program that prints
I am learning Dart. - Print the names
Ali,Sara, andZayan, each on its own line. - Print the number
42and the decimal number3.5, each on its own line.
Application
-
Print
Helloon one line, a blank line, and thenGoodbye. -
Print the text
10 + 5and, on the next line, the result of the calculation10 + 5. -
Print
Dart is fun, and add a single-line comment above it that says what the program does. -
This program does not compile. Fix it so that it prints
Hello, Sara:void main() { Print('Hello, Sara') } -
Without deleting any line, change this program so that it prints only
Step 1andStep 3:void main() { print('Step 1'); print('Step 2'); print('Step 3'); }
Challenge
- Write a shopping receipt for Ali. Add a doc comment above
main()saying what the program does. PrintItem: Notebook,Quantity: 3, and then let Dart calculate the total for 3 notebooks at120each. Add a multi-line comment explaining that prices are in rupees. - Print the sentence
It's Zayan's birthday!exactly, then print Zayan's age next year, calculated by Dart from his age this year,6. Add an end-of-line comment explaining the calculation.
Practice Answers
Question 1 — Solution
void main() {
print('I am learning Dart');
}I am learning DartQuestion 2 — Solution
void main() {
print('Ali');
print('Sara');
print('Zayan');
}Ali
Sara
ZayanQuestion 3 — Solution
void main() {
print(42);
print(3.5);
}42
3.5Question 4 — Solution
void main() {
print('Hello');
print('');
print('Goodbye');
}Hello
GoodbyeQuestion 5 — Solution
void main() {
print('10 + 5');
print(10 + 5);
}10 + 5
15The quotes decide everything: with them, Dart prints the characters; without them, it calculates.
Question 6 — Solution
void main() {
// Prints a message about Dart
print('Dart is fun');
}Dart is funQuestion 7 — Solution
Two fixes: print must start with a lowercase p, and the statement needs a semicolon.
void main() {
print('Hello, Sara');
}Hello, SaraQuestion 8 — Solution
void main() {
print('Step 1');
// print('Step 2');
print('Step 3');
}Step 1
Step 3The line is still there, but // turns it into a comment, so it does not run.
Question 9 — Solution
/// Prints a shopping receipt for Ali.
void main() {
/*
All prices are in rupees.
Each notebook costs 120.
*/
print('Item: Notebook');
print('Quantity: 3');
print(120 * 3);
}Item: Notebook
Quantity: 3
360Question 10 — Solution
void main() {
print("It's Zayan's birthday!");
print(6 + 1); // his age this year plus one
}It's Zayan's birthday!
7The sentence contains apostrophes, so it uses double quotes. 6 + 1 has no quotes, so Dart
calculates 7.
Quick Revision
- Which command runs a file named
hello.dart? - Where does every Dart program start?
- What does
print()do, and how many values does it take? - What marks the end of a statement?
- What do the curly braces
{ }aftermain()do? - What is the difference between
print(2 + 3)andprint('2 + 3')? - Name the three kinds of comments and the symbols that start them.
- What does "commenting out" a line mean?
Key Takeaways
- A Dart program lives in a
.dartfile and runs withdart run file.dart. - Every program starts at
main(); its curly braces hold the statements that run. print()shows exactly one value, then moves to a new line.- Text goes in quotes; numbers and calculations do not.
- Statements end with a semicolon and run from top to bottom.
- Dart checks the whole file first — one mistake stops every line from running.
//comments one line,/* */comments a block, and///documents the code below it.- Comments are for humans: explain why, and use
//to switch code off while testing.
What's Next?
Lecture 04 — Variables in Dart. So far, every value was typed straight into print(). If
Sara's name appeared in ten lines, we would type it ten times — and edit ten lines when it
changed. Next we will learn how variables give a value a name, so the program can store it once
and reuse it anywhere.