Delphi basic syntax format

Source: Internet
Author: User
Tags case statement uppercase character subdomain

1. Constant: fixed value, not variable.

1) const

Constant name = constant value (expression );

2) resourcestring

Constant name = string (constant );

 

2. Variables

1) Definition

VaR name 1, name 2 ,... Name N: type;

2) Naming principles (reference)

Variable name = type (abbreviation) + scope (abbreviation) + exact meaning name

3) Three assignment formats

Variable name: = value;

Variable name 1: = variable 2;

Variable name: = process or function call;

 

3. Type Constants

1) The type constant is a global variable and is not a constant. It carries out a pre-initialization value.

2) Definition

Const

Name: TYPE = value;

3) Types

Simple Type Constants

Array type constant

Record type constant

Set type constant

Pointer type constant

Process type constant

 

4. Data Type

Simple)

 

 

 

Ordinal)

 

 

 

Integer)

 

 

Character (character)

 

 

Boolean)

 

 

Enumerated)

 

 

Subdomain (subrange)

 

Real Number (real)

 

Structured)

 

 

 

Set)

 

 

Array)

 

 

Record)

 

 

File)

 

 

Class)

 

 

Classreference)

 

 

Interface)

 

String type (string)

 

 

Pointer type (pointer)

 

 

Process type (procedural)

 

 

Variant)

 

 

 

5. User-defined types

Subdomain type

Type name = initial value... End value;

Enumeration type

Type name = (value 1, value 2, value 3... Value N );

Array type

Type name = array [subscript definition] of type name

Type name = [subscript Definition 1, subscript Definition 2... Subscript defines n] of element type

Or name = [subscript definition 1of... Array [subscript defines n] of element type

Set Type

Type name = set of element type

Pointer type

Type name = ^ Node Type

Record type

Type name = record

Domain Name 1: type;

Domain Name 1: type;

......

Domain Name N: type;

End;

File Type

Type name = file of element type

 

6. Statements

Simple statement

X: = Y + z; // assignment

Randomize; // procedure call

Compound statement

Begin

A: = B;

C: = A * 2;

End;

The semicolon at the end of the last statement before end is not required. You can write it as follows:

Begin

A: = B;

C: = A * 2

End;

Assignment Statement

In PASCAL, the value assignment statement uses the colon-equal sign operator ": = ",

Condition Statement

If statement

If-then statements are executed only when the conditions are met;

If checkbox1.checked then

Showmessage ('checkbox1 is checked ')

 

For the if-then-else type, select one of the two if statements for execution.

If checkbox2.checked then

Showmessage ('checkbox2 is checked ')

Else

Showmessage ('checkbox2 is not checked ');

Note that you cannot add points after the first sentence or before the else keyword. Otherwise, the compiler will notify you of a syntax error. In fact, the if-then-else statement is a simple statement, so it cannot add points in the middle of the statement. The IF statement can be complex. The condition part of a sentence can be a series of conditions (connected by boolean operators such as and, or, and not), and the IF statement can be nested with another if statement.

Begin

// Compound if statement

If checkbox1.checked then

If checkbox2.checked then

Showmessage ('checkbox1 and 2 are checked ')

Else

Showmessage ('only checkbox1 is checked ')

Else

Showmessage (

'Checkbox1 is not checked, who cares for checkbox2? ')

End;

 

Case statement

The case statement includes an expression used to select a value, a sequence of possible values, or a value range. These values should be constants, and they must be unique and belong to an ordered type.

The case statement can finally contain an else statement. When no tag is the same as the selector value, the else statement is executed. The following are two simple examples:

Case number

1: Text: = 'one ';

2: Text: = 'two ';

3: Text: = 'three ';

End;

Case mychar

'+': Text: = 'plus sign ';

'-': Text: = 'minus sign ';

'*', '/': Text: = 'multiplication or division ';

'0' .. '9': Text: = 'number ';

'A' .. 'Z': Text: = 'lowercase character ';

'A' .. 'Z': Text: = 'uppercase character ';

Else

Text: = 'unknown character ';

End;

 

For Loop

The for loop is strictly built on the basis of the counter, and is executed every time. The counter is either increased or decreased. The following is a for statement

Simple example to add the top 10:

VaR

K, I: integer;

Begin

K: = 0;

For I: = 1 to 10 do

K: = K + I;

The same for statement can be written with the opposite counter:

VaR

K, I: integer;

Begin

K: = 0;

For I: = 10 downto 1 do

K: = K + I;

 

The for loop statement is more flexible than other languages (it cannot specify a step other than 1), but it is easy to understand. If the conditions to be determined are complex,

You can use the while statement or repeat statement instead of the For Loop statement to customize the counter.

Note: The for loop counter does not need to be a number. It can be an ordered value, for example, a character or an enumerated value.

 

While statement and repeat statement

The difference between the while-do and repeat-until statements lies inCodeAt least once. This can be easily understood from the following examples.

While (I <= 100) and (j <= 100) Do

Begin

// Use I and j to compute something...

I: = I + 1;

J: = J + 1;

End;

Repeat

// Use I and j to compute something...

I: = I + 1;

J: = J + 1;

Until (I> 100) or (j> 100 );

As shown in the preceding figure, even if the initial value of I or J is greater than 100, the code in the repeat-until loop will still be executed once.

Note: Another key difference between the two types of loops is that the repeat-until loop condition is a reverse condition. If this condition is not met, the loop is executed. When the condition is met

The cycle ends. This is exactly the opposite of a while-do loop. A while-do loop is executed only when the condition is true. Therefore, I have to use reverse conditions in the above Code

Obtain the same result.

 

Note: The break and continue system processes can be used to change the standard process of cyclic execution. Break interrupt cycle; Continue directly jumps to the cyclic test sentence or counts

Add a step, and then continue the loop (unless the condition is null or the counter reaches the maximum value ). There are two other system processes: exit and halt, so that you can immediately start the function or process

Or terminateProgram.

With statement

With birthday do

Begin

Year: = 1995;

Month: = 2;

Day: = 14;

End;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.