Understanding Delphi Syntax: The Basics

August 1, 2024

Understanding Delphi Syntax: The Basics

Welcome back to our Delphi programming course! In this post, we will dive into the basic syntax of Delphi, which is essential for writing effective code. Understanding the syntax will help you create variables, use data types, and apply operators in your programs. Let’s get started!

Data Types in Delphi

Delphi supports a variety of data types that you can use to define the kind of data your variables will hold. Here are some of the most commonly used data types:

  • Integer: Represents whole numbers. For example:
    var age: Integer;
  • Real: Represents floating-point numbers. For example:
    var salary: Real;
  • String: Represents a sequence of characters. For example:
    var name: String;
  • Boolean: Represents true or false values. For example:
    var isActive: Boolean;

Declaring Variables

In Delphi, you declare variables using the var keyword, followed by the variable name and its data type. Here’s an example:

var
    firstName: String;
    lastName: String;
    age: Integer;
    isEmployed: Boolean;

After declaring a variable, you can assign a value to it:

firstName := 'John';
lastName := 'Doe';
age := 30;
isEmployed := True;

Operators in Delphi

Operators are symbols that perform operations on variables and values. Delphi supports various types of operators, including:

  • Arithmetic Operators: Used for mathematical operations.
    • + (Addition)
    • – (Subtraction)
    • * (Multiplication)
    • / (Division)
  • Relational Operators: Used to compare values.
    • = (Equal to)
    • < (Less than)
    • > (Greater than)
    • <= (Less than or equal to)
    • >= (Greater than or equal to)
    • <> (Not equal to)
  • Logical Operators: Used to combine Boolean expressions.
    • and
    • or
    • not

Example: Putting It All Together

Let’s create a simple program that uses the concepts we’ve discussed:

program HelloWorld;

var
    firstName: String;
    lastName: String;
    age: Integer;
    isEmployed: Boolean;

begin
    firstName := 'John';
    lastName := 'Doe';
    age := 30;
    isEmployed := True;

    WriteLn('Hello, ' + firstName + ' ' + lastName + '!');
    WriteLn('You are ' + IntToStr(age) + ' years old.');
    if isEmployed then
        WriteLn('You are employed.')
    else
        WriteLn('You are not employed.');
end.

This program declares variables for a person’s first name, last name, age, and employment status. It then outputs a greeting and employment status based on the variables’ values.

Conclusion

In this post, we covered the basic syntax of Delphi, including data types, variable declaration, and operators. Understanding these fundamentals is crucial as you continue to learn and develop your Delphi programming skills. In the next post, we will explore control structures in Delphi, so stay tuned!