Syntax and primitive data types

Lesson on Syntax and Primitive Data Types in Dart

1. Introduction to Dart Syntax

Dart is a strongly-typed, object-oriented programming language. Understanding its syntax is crucial for writing clean, effective code. Syntax in Dart refers to the set of rules that defines how programs are written and interpreted.

Basic Syntax Elements in Dart

  • Main Function: Every Dart program starts with the main() function. It’s the entry point of execution.
  • void main() {
        print('Hello, World!');
    }
  • Comments: Comments in Dart can be either single-line (//) or multi-line (/* */).
  • // This is a single-line comment
    /* This is 
       a multi-line comment */
  • Variables: Variables store data values and are declared using var, final, or const.
  • var name = 'Dart';
    final pi = 3.14;
    const gravity = 9.8;
  • Printing: The print() function is used to output data to the console.
  • print('Learning Dart is fun!');

2. Primitive Data Types in Dart

Dart provides several primitive data types for storing simple values like numbers, characters, and booleans. These data types form the foundation of any Dart program.

Why are Primitive Data Types Important?

  • Efficient Memory Usage: Primitive types store basic data efficiently without overhead.
  • Fast Operations: Operations on primitives are faster compared to collections or custom objects.
  • Type Safety: Dart is strongly-typed, ensuring that operations on data are type-checked at compile time, reducing runtime errors.

Primitive Data Types Chart:

Data Type Description Example
int Represents integer values (whole numbers, positive or negative). int age = 25;
double Represents floating-point numbers (numbers with decimal points). double price = 10.99;
bool Represents boolean values (true or false). bool isValid = true;
String Represents a sequence of characters (text). String name = 'John';
runes Represents Unicode code points in strings. var heart = '\u2665';
Symbol Represents identifiers in Dart. Symbol s = #mySymbol;
Null Represents the absence of a value. var x = null;

3. Explanation of Each Data Type

1. int (Integer)

Description: The int data type stores whole numbers. It can hold both positive and negative integers.

Usage: Used when you need to count, index, or perform arithmetic on whole numbers.

void main() {
    int age = 22;
    int year = 2024;
    print('Age: \$age, Year: \$year');
}

2. double (Floating-point number)

Description: The double data type stores decimal numbers.

Usage: Used in calculations involving decimal numbers or real-world measurements like weight, temperature, etc.

void main() {
    double pi = 3.14159;
    double price = 99.99;
    print('Pi: \$pi, Price: \$price');
}

3. bool (Boolean)

Description: The bool type represents truth values: true or false.

Usage: Used in conditions and logical operations to control the flow of the program.

void main() {
    bool isLoggedIn = true;
    bool isAdmin = false;
    print('User logged in: \$isLoggedIn');
}

4. String (Text)

Description: The String data type stores sequences of characters.

Usage: Used for representing and manipulating textual data such as names, messages, or URLs.

void main() {
    String name = 'Shubham';
    String greeting = "Hello, \$name!";
    print(greeting);
}

5. runes (Unicode)

Description: Runes represent Unicode code points in Dart.

void main() {
    var heart = '\u2665';
    print('I love Dart \$heart');
}

6. Symbol

Description: A Symbol represents an identifier in Dart.

void main() {
    Symbol s = #mySymbol;
    print(s);
}

7. Null

Description: Null represents the absence of a value.

void main() {
    var x;
    print(x);  // Output will be null
}

4. Null Safety in Dart

Dart supports null safety, which helps eliminate null dereference errors. With null safety, variables are non-nullable by default, meaning they cannot hold a null value unless explicitly specified.

Using Nullable Types

void main() {
    String? nullableString; // This can be null
    nullableString = 'Hello, Dart!';
    print(nullableString?.length); // Safe access using ?. (output: 14)
}

Default Values Using Null-Aware Operator

void main() {
    String? userName;
    print('User Name: ${userName ?? "Anonymous"}'); // Output: User Name: Anonymous
}

5. Assignments

1. Working with Integers

Create a program that calculates the area of a rectangle using integers for width and height.

2. Manipulating Strings

Write a function that takes a string and returns its reverse.

String reverseString(String input) {
    return input.split('').reversed.join('');
}

3. Using Booleans

Write a Dart program to check if a given number is even or odd using a boolean variable.

Create a program that checks if a user is eligible to vote (age 18 or older) and prints the result.

4. Handling Null Safety

Write a program that uses nullable types to store user input and safely prints the input length if it’s not null.

Create a program that sets default values using the null-aware operator for a user’s age and name, ensuring they cannot be null.

5. Sum of Two Integers

Write a program that takes two integer inputs and prints their sum.

6. String Concatenation

Create a program that takes two strings as input and prints their concatenation.

7. Age Calculation

Write a program that calculates a person's age based on their birth year input.

8. Convert Celsius to Fahrenheit

Write a program that converts temperature from Celsius to Fahrenheit.

9. Check for Prime Number

Create a program that checks if a given number is a prime number.

10. Count Vowels in a String

Write a program that counts the number of vowels in a given string.


 

Post a Comment

0 Comments

Me