I learned about Delphi and my notes-statements (Lecture 4)

Source: Internet
Author: User
Tags uppercase character

If the data type is a foundation of Pascal programming, then the other is statements. Programming Language statements mainly consist of keywords and operation instructions. Statements are often placed in procedures or functions, as we will see in the next section. Now, we will focus on the basic programming statements.

Simple and compound statements

A simple Pascal statement does not contain any other statements. The value assignment statement and procedure call are examples of simple statements. Simple statements are separated by semicolons, as shown below:

X := Y + Z;  // assignmentRandomize;   // procedure call

You can use begin and end to enclose a simple statement to form a composite statement. The usage of the composite statement is the same as that of the common Pascal statement. See the following example:

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 * 2end;

Both methods are correct. The first type adds a useless (but harmless) Semicolon. A semicolon is actually an empty statement, that is, a statement without code. Sometimes, empty statements can be used in the loop body or in other special cases.

Note:: Although the semicolon at the end of the last statement is useless, I always add it, and you are advised to do the same. Because sometimes you may need to add a statement at the end. If there is no plus sign at the end, you must remember to add it. Otherwise, add it at the beginning.

Assignment Statement

In Pascal, the value assignment statement uses the colon-equal sign operator ": =", which is a strange symbol for programmers in other languages. "=", Which is used as the value assignment symbol in other languages, is used as a relational operator in Pascal to determine whether the values are equal.

Note:: The assignment and equality judgments use different symbols, so that the Pascal Compiler (like the C compiler) can interpret the source code more quickly, because it does not need to check context to determine the meaning of the symbol, in addition, different operators are used to make the code easier to read.

Condition Statement

The condition statement checks whether to execute the statements contained in the Condition Statement. Conditional statements have two basic forms: if statement and case statement.

If statement

Procedure TForm1.ButTestIf (Sender: TObject );
Var
I, j: Integer;
Begin
I: = 10;
J: = 17;
If I> j then
Showmessage ('I> J ');
End;

If the else statement

Procedure TForm1.ButTestIf (Sender: TObject );
Var
I, j: Integer;
Begin
I: = 10;
J: = 17;
If I> j then
Showmessage ('I> J') (do not add the'; 'sign here)

Else

Showmessage ('I <J ');

Else
End;

Case statement

If your if statement becomes very complex, you can use the case statement instead. 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:

CaseNumberOf1: Text: ='One'; 2: Text: ='Two'; 3: Text: ='Three';End;CaseMyCharOf'+': Text: = 'plus sign'; '-': Text: = 'minus sign'; '*', '/': Text: = 'multiplication or division '; '0 '.. '9': Text: = 'number'; 'A '.. 'Z': Text: = 'lowercase character '; 'A '.. 'Z': Text: = 'uppercase character ';ElseText: = 'unknown character ';EndLoop in Delphi language

Loop statements used in other programming languages, which are included in PascalFor,WhileAndRepeatStatement. If you have used other programming languages, you will find that the loop statements in Pascal are nothing special, so here I will just give a brief description.

For Loop

In Pascal, The for loop is strictly based on the counter. Every execution of the loop is performed. The counter is either increased or decreased. The following is a simple example of a for statement used to add up 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 in Pascal is less 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 or you want to customize the counter, you can use the while statement or repeat statement instead of the for Loop statement.

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

While statement and repeat statement

While-doLoop statements andRepeat-StatementRepeatThe code of the loop statement must be executed at least once. This can be easily understood from the following example:

while (I <= 100) and (J <= 100) dobegin  // 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);

Even ifIOrJThe initial value is greater than 100, and the code in the repeat-until loop will still be executed once.

Note:Another key difference between the two loops is,Repeat-The condition of a loop is a reverse condition. If the condition is not met, the loop is executed. When the condition is met, the loop ends. This is exactly the sameWhile-doOpposite to loop,While-doA loop is executed only when the condition is true. Therefore, I have to use reverse conditions in the above Code to obtain the same results.

Note:: UseBreakAndContinueThe system process can change the standard process of cyclic execution.BreakInterrupt loop;ContinueDirectly jump to the cyclic test sentence, or add a step for the counter, and then continue the loop (unless the condition is null or the counter reaches the maximum value ). There are two other system processesExitAndHaltSo that you can immediately return from the function or process, or terminate the program.

With statement

The last Pascal statement I want to talk about is the With statement, which is unique to the Pascal programming language. However, this statement has recently been added to JavaScript and Visual Basic, it is useful in Delphi programming.

A With statement is a statement used to simplify the code. If you want to access a record type variable (or an object), you do not have to repeat the variable name every time using the With statement. For example, for the following record type code:

type  Date = record    Year: Integer;    Month: Byte;    Day: Byte;  end;var  BirthDay: Date;begin  BirthDay.Year := 1997;  BirthDay.Month := 2;  BirthDay.Day := 14;

You can use the with statement to improve the code in the second half, as shown below:

begin  with BirthDay do  begin    Year := 1995;    Month := 2;    Day := 14;  end;

In the Delphi program, this method can be used to access controls and class variables. Now we use the with statement to access the entries in the list box. We will rewrite the last part of the above loop example:

procedure TForm1.WhileButtonClick(Sender: TObject);var  I: Integer;begin  with ListBox1.Items do  begin    Clear; // shortcut    Randomize;    I := 0;    while I < 1000 do    begin      I := I + Random (100);      // shortcut:      Add ('Random Number: ' + IntToStr (I));    end;  end;end;

When you use controls or classes, the with statement can simplify your code, especially for nested domains. For example, to change the width and color of the form paint brush, you can write the following code:

Form1.Canvas.Pen.Width := 2;Form1.Canvas.Pen.Color := clRed;

However, the With statement code is simpler:

with Form1.Canvas.Pen dobegin  Width := 2;  Color := clRed;end;

When the code is complex, the with statement can be useful and save some temporary variables. However, this method also has disadvantages, because it will make the code more readable, especially for objects with similar or identical attributes.

More seriously, using the with statement may include subtle logical errors in the code, and even the compiler is hard to find. For example:

with Button1 dobegin  Width := 200;  Caption := 'New Caption';  Color := clRed;end;

This code changes the Caption and Width attributes of the button, but also changes the Color attribute of the form, not the Color of the button! The reason is that the TButton control does not have the Color attribute, and because the code executed is for the form object (we are writing the form method), the form object becomes the default access object. Write the following statement:

Button1.Width := 200;Button1.Caption := 'New Caption';Button1.Color := clRed; // error!

The compiler will give an error. Generally, because the with statement defines a new identifier in the current block, omitting the original identifier may lead to an incorrect access to another identifier in the same block (like the above Code ). Even if there are various defects, I suggest you use with statements, because with statements are indeed very convenient and sometimes make the code easier to understand.

However, you should avoid using multipleWithStatement, such:

with ListBox1, Button1 do...

This will make the subsequent code very difficult to read, because each attribute defined in the block must be in the order of corresponding attributes and controls to launch the accessed control.

Note:: Speaking of readability, you must know that Pascal does not have an endif or endcase statement. If the if statement has a begin-end block, the end flag ends. In addition, the case statement always ends with an end. All these end statements are often one by one, making the code hard to understand. Only by reducing the trace, can the corresponding end statement be obtained. A common solution to this problem is to make the code more readable by adding a comment after the end, as shown in the following example:

  if ... then   ...  end; // if

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.