Welcome to your first Dart program! In this section, we will guide you through writing, understanding, and running a simple Dart program. By the end of this lesson, you will have a basic understanding of how Dart code is structured and executed.
Objectives
- Write a simple Dart program.
- Understand the basic structure of a Dart program.
- Run the Dart program and see the output.
Writing Your First Dart Program
Let's start by writing a simple Dart program that prints "Hello, World!" to the console. This is a traditional first program in many programming languages.
Step-by-Step Guide
-
Create a New Dart File:
- Open your preferred code editor (e.g., Visual Studio Code).
- Create a new file and name it
main.dart
.
-
Write the Code:
- Type the following code into
main.dart
:
void main() { print('Hello, World!'); }
- Type the following code into
-
Save the File:
- Save the file by pressing
Ctrl + S
(Windows/Linux) orCmd + S
(Mac).
- Save the file by pressing
Code Explanation
Let's break down the code to understand what each part does:
-
void main() { ... }
:- This is the main function, which is the entry point of every Dart application. When you run a Dart program, the execution starts from the
main
function. void
indicates that themain
function does not return any value.
- This is the main function, which is the entry point of every Dart application. When you run a Dart program, the execution starts from the
-
print('Hello, World!');
:- The
print
function is used to output text to the console. 'Hello, World!'
is a string literal that will be printed to the console.
- The
Running Your Dart Program
To run your Dart program, follow these steps:
-
Open Terminal/Command Prompt:
- Open the terminal or command prompt in your code editor or operating system.
-
Navigate to the Directory:
- Use the
cd
command to navigate to the directory where yourmain.dart
file is located. For example:cd path/to/your/dart/project
- Use the
-
Run the Program:
- Execute the Dart program by typing the following command:
dart main.dart
- Execute the Dart program by typing the following command:
-
View the Output:
- You should see the following output in the terminal:
Hello, World!
- You should see the following output in the terminal:
Practical Exercise
Exercise 1: Modify the Program
Modify the main.dart
program to print your name instead of "Hello, World!".
Solution
- Open
main.dart
in your code editor. - Change the
print
statement to include your name:void main() { print('Hello, [Your Name]!'); }
- Save the file and run the program again using the
dart main.dart
command. - Verify that the output is:
Hello, [Your Name]!
Exercise 2: Add Another Print Statement
Add another print
statement to display your favorite programming language.
Solution
- Open
main.dart
in your code editor. - Add another
print
statement below the first one:void main() { print('Hello, [Your Name]!'); print('My favorite programming language is Dart.'); }
- Save the file and run the program again using the
dart main.dart
command. - Verify that the output is:
Hello, [Your Name]! My favorite programming language is Dart.
Common Mistakes and Tips
-
Missing Semicolon:
- Ensure that each statement ends with a semicolon (
;
). Forgetting the semicolon will result in a syntax error.
- Ensure that each statement ends with a semicolon (
-
Case Sensitivity:
- Dart is case-sensitive. Make sure to use the correct case for keywords and function names (e.g.,
print
notPrint
).
- Dart is case-sensitive. Make sure to use the correct case for keywords and function names (e.g.,
-
String Quotes:
- Strings can be enclosed in single (
'
) or double ("
) quotes. Be consistent with your choice.
- Strings can be enclosed in single (
Conclusion
Congratulations! You have successfully written and run your first Dart program. You learned about the basic structure of a Dart program, how to use the print
function, and how to run a Dart program from the terminal. In the next lesson, we will dive deeper into Dart's basic syntax and structure. Keep practicing and experimenting with the code to reinforce your understanding.
Dart Programming Course
Module 1: Introduction to Dart
- Introduction to Dart
- Setting Up the Development Environment
- Your First Dart Program
- Basic Syntax and Structure