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

  1. Create a New Dart File:

    • Open your preferred code editor (e.g., Visual Studio Code).
    • Create a new file and name it main.dart.
  2. Write the Code:

    • Type the following code into main.dart:
    void main() {
      print('Hello, World!');
    }
    
  3. Save the File:

    • Save the file by pressing Ctrl + S (Windows/Linux) or Cmd + S (Mac).

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 the main function does not return any value.
  • 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.

Running Your Dart Program

To run your Dart program, follow these steps:

  1. Open Terminal/Command Prompt:

    • Open the terminal or command prompt in your code editor or operating system.
  2. Navigate to the Directory:

    • Use the cd command to navigate to the directory where your main.dart file is located. For example:
      cd path/to/your/dart/project
      
  3. Run the Program:

    • Execute the Dart program by typing the following command:
      dart main.dart
      
  4. View the Output:

    • You should see the following output in the terminal:
      Hello, World!
      

Practical Exercise

Exercise 1: Modify the Program

Modify the main.dart program to print your name instead of "Hello, World!".

Solution

  1. Open main.dart in your code editor.
  2. Change the print statement to include your name:
    void main() {
      print('Hello, [Your Name]!');
    }
    
  3. Save the file and run the program again using the dart main.dart command.
  4. 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

  1. Open main.dart in your code editor.
  2. Add another print statement below the first one:
    void main() {
      print('Hello, [Your Name]!');
      print('My favorite programming language is Dart.');
    }
    
  3. Save the file and run the program again using the dart main.dart command.
  4. 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.
  • Case Sensitivity:

    • Dart is case-sensitive. Make sure to use the correct case for keywords and function names (e.g., print not Print).
  • String Quotes:

    • Strings can be enclosed in single (') or double (") quotes. Be consistent with your choice.

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.

© Copyright 2024. All rights reserved