Watch the video (19 min)
On this page28 sections
  1. Prerequisites
  2. Connection With Previous Lectures
  3. Learning Objectives
  4. 1. What Is a Program?
  5. 2. Creating Your First File
  6. 3. Running the File
  7. 4. main() — Where Every Program Starts
  8. 5. print() — Showing Output
  9. 6. Printing Text
  10. 7. Printing Numbers
  11. 8. Statements and Semicolons
  12. 9. Braces and Indentation
  13. 10. Why Comments?
  14. 11. Single-Line Comments //
  15. 12. Multi-Line Comments /* */
  16. 13. Documentation Comments ///
  17. 14. Commenting Out Code
  18. 15. Writing Good Comments
  19. 16. A Real-World Example
  20. Code Examples
  21. Common Beginner Mistakes
  22. Best Practices
  23. Mini Challenge
  24. Practice Questions
  25. Practice Answers
  26. Quick Revision
  27. Key Takeaways
  28. What's Next?

Lecture 03 — Your First Dart Program & Comments

Category1 — Dart Fundamentals
Lecture003 of 140
Estimated video length19 min
TargetDart 3.12.2
LevelBeginner

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:

bash
dart --version

That 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 .dart file and run it with dart 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:

dart
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:

bash
dart run hello.dart
output
Hello, 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:

bash
dart run helo.dart
output
Could 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.

dart
void main() {
  print('The program starts here.');
}
output
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:

dart
void mian() {
  print('Hello');
}
output
Invoked Dart programs must have a 'main' function defined:
https://dart.dev/to/main-function

5. print() — Showing Output

print() displays whatever you put between its brackets, then moves to a new line.

dart
void main() {
  print('Welcome to the Dart course');
}
output
Welcome to the Dart course

The 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:

dart
void main() {
  print('Ali');
  print("Sara");
  print("It's Zayan's turn");
}
output
Ali
Sara
It's Zayan's turn

Most 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:

dart
void main() {
  print(20);
  print(99.5);
  print(2 + 3);
  print('2 + 3');
}
output
20
99.5
5
2 + 3

Look 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:

dart
void main() {
  print('First');
  print('Second');
  print('Third');
}
output
First
Second
Third

The semicolon marks where one statement ends. Forget it, and Dart stops before running anything:

dart
void main() {
  print('Hello')
}
output
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 }:

dart
void main() {
  print('Hello');
output
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:

dart
void main() {
  // Greet the student
  print('Hello, Ali'); // shown first
  print('Hello, Sara'); // shown second
}
output
Hello, Ali
Hello, Sara

Everything 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:

dart
void main() {
  /*
    This program prints a short welcome.
    It was written for the Dart course.
  */
  print('Welcome!');
}
output
Welcome!

The closing */ is required. Without it, Dart treats the rest of the file as a comment:

dart
void main() {
  /* this comment never ends
  print('Hello');
}
output
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:

dart
/// Prints a welcome message for new students.
///
/// This is the first program of the Dart course.
void main() {
  print('Welcome, new student!');
}
output
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:

dart
void main() {
  print('Line one');
  // print('Line two');
  print('Line three');
}
output
Line one
Line three

This 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:

dart
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);
}
output
1500
1500

Both comments are legal, but only the second one helps. If a comment just repeats the code, leave it out.


16. A Real-World Example

dart
/// 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!');
}
output
=== 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

dart
void main() {
  print('Hello, World!');
}
output
Hello, World!

Example 2 — Several Lines in Order

dart
void main() {
  print('Ali');
  print('Sara');
  print('Zayan');
}
output
Ali
Sara
Zayan

Example 3 — Text and Numbers

dart
void main() {
  print('Age');
  print(20);
  print('Price');
  print(99.5);
}
output
Age
20
Price
99.5

Example 4 — A Blank Line

dart
void main() {
  print('Section one');
  print('');
  print('Section two');
}
output
Section one

Section two

print('') 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

dart
void main() {
  // This line is ignored
  print('Comments are skipped'); // and so is this part
}
output
Comments are skipped

Example 6 — Multi-Line and Doc Comments

dart
/// 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
}
output
Only this line is printed

How it works

  1. The /// line describes main() for readers and tools.
  2. The /* */ block spans four lines and is skipped entirely.
  3. The // note after the statement is skipped too, so one line is printed.

Example 7 — Student Report Card

dart
/// 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);
}
output
Student: Zayan
Grade: 1
Math: 90
English: 85
175

How it works

  1. The first four statements print text exactly as written inside the quotes.
  2. 90 + 85 has no quotes, so Dart calculates it and prints 175.
  3. The comments label the sections but never appear in the output.

Example 8 — Shopping Total

dart
void main() {
  print('Pens x 4');
  print(25 * 4); // 4 pens at 25 each
  print('Bag x 1');
  print(1200); // one bag
}
output
Pens x 4
100
Bag x 1
1200

How it works

  1. 'Pens x 4' is text, so the x is printed as a letter.
  2. 25 * 4 has no quotes, so * multiplies and Dart prints 100.
  3. The end-of-line comments explain each number for the reader.

Example 9 — Bank Balance Notice

dart
/*
  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
}
output
Account holder: Ali
Opening balance:
1000
Closing balance:
750

How it works

  1. A multi-line comment can sit above main() as well as inside it.
  2. 1000 - 250 is calculated to 750 before it is printed.
  3. Each label and its number are on separate lines, because one print() shows one value.

Example 10 — Temperature Report

dart
void main() {
  print('Today in Lahore');
  print(31.5); // degrees Celsius
  // print('Rain expected'); // switched off: no rain today
  print('Have a sunny day!');
}
output
Today in Lahore
31.5
Have a sunny day!

How it works

  1. 31.5 is a decimal number and prints exactly as written.
  2. The rain line is commented out, so it does not run.
  3. Removing the first // on that line would switch it back on.

Common Beginner Mistakes

1. Forgetting the semicolon

dart
void main() {
  // print('Hello') // ✗ no semicolon
}
output
Error: Expected ';' after this.

Every statement ends with ;.

2. Writing Print with a capital P

dart
void main() {
  // Print('Hello'); // ✗
}
output
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

dart
void main() {
  // print(Hello); // ✗
}
output
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

dart
void main() {
  // print('Hello"); // ✗ starts with ' but ends with "
}
output
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

dart
void main() {
  // print('It's Ali'); // ✗
}
output
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

dart
// void main() {
//   print('Hello');
// ✗ no closing }
output
Error: Can't find '}' to match '{'.

7. Giving print() two values

dart
void main() {
  // print('Ali', 'Sara'); // ✗
}
output
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

output
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 let dart format do 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:

output
--- Student Card ---
Name: Sara
Math: 80
Science: 75
155

Try it yourself before opening the solution.

Show solution
dart
/// 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);
}
output
--- Student Card ---
Name: Sara
Math: 80
Science: 75
155

How it works

  1. The /// comment describes the program; the // comment labels the marks.
  2. The text lines print exactly what is inside the quotes.
  3. 80 + 75 has no quotes, so Dart calculates 155.

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

  1. Write a program that prints I am learning Dart.
  2. Print the names Ali, Sara, and Zayan, each on its own line.
  3. Print the number 42 and the decimal number 3.5, each on its own line.

Application

  1. Print Hello on one line, a blank line, and then Goodbye.

  2. Print the text 10 + 5 and, on the next line, the result of the calculation 10 + 5.

  3. Print Dart is fun, and add a single-line comment above it that says what the program does.

  4. This program does not compile. Fix it so that it prints Hello, Sara:

    dart
    void main() {
      Print('Hello, Sara')
    }
  5. Without deleting any line, change this program so that it prints only Step 1 and Step 3:

    dart
    void main() {
      print('Step 1');
      print('Step 2');
      print('Step 3');
    }

Challenge

  1. Write a shopping receipt for Ali. Add a doc comment above main() saying what the program does. Print Item: Notebook, Quantity: 3, and then let Dart calculate the total for 3 notebooks at 120 each. Add a multi-line comment explaining that prices are in rupees.
  2. 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

dart
void main() {
  print('I am learning Dart');
}
output
I am learning Dart

Question 2 — Solution

dart
void main() {
  print('Ali');
  print('Sara');
  print('Zayan');
}
output
Ali
Sara
Zayan

Question 3 — Solution

dart
void main() {
  print(42);
  print(3.5);
}
output
42
3.5

Question 4 — Solution

dart
void main() {
  print('Hello');
  print('');
  print('Goodbye');
}
output
Hello

Goodbye

Question 5 — Solution

dart
void main() {
  print('10 + 5');
  print(10 + 5);
}
output
10 + 5
15

The quotes decide everything: with them, Dart prints the characters; without them, it calculates.

Question 6 — Solution

dart
void main() {
  // Prints a message about Dart
  print('Dart is fun');
}
output
Dart is fun

Question 7 — Solution

Two fixes: print must start with a lowercase p, and the statement needs a semicolon.

dart
void main() {
  print('Hello, Sara');
}
output
Hello, Sara

Question 8 — Solution

dart
void main() {
  print('Step 1');
  // print('Step 2');
  print('Step 3');
}
output
Step 1
Step 3

The line is still there, but // turns it into a comment, so it does not run.

Question 9 — Solution

dart
/// 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);
}
output
Item: Notebook
Quantity: 3
360

Question 10 — Solution

dart
void main() {
  print("It's Zayan's birthday!");
  print(6 + 1); // his age this year plus one
}
output
It's Zayan's birthday!
7

The sentence contains apostrophes, so it uses double quotes. 6 + 1 has no quotes, so Dart calculates 7.


Quick Revision

  1. Which command runs a file named hello.dart?
  2. Where does every Dart program start?
  3. What does print() do, and how many values does it take?
  4. What marks the end of a statement?
  5. What do the curly braces { } after main() do?
  6. What is the difference between print(2 + 3) and print('2 + 3')?
  7. Name the three kinds of comments and the symbols that start them.
  8. What does "commenting out" a line mean?

Key Takeaways

  • A Dart program lives in a .dart file and runs with dart 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.


Video lecture

Subscribe
Placeholder video — the real recording isn't linked yet.