Chapter II-delphi Object-oriented Programming (II.) (1)

Source: Internet
Author: User
Tags case statement constant expression integer reserved

2.1.4 Jump Statement

The jump statement of Object Pascal has the IF and case two.

2.1.4.1 If statement

The IF statement evaluates an expression and determines the program flow based on the result of the calculation. In the previous routine, the background color of the form is determined based on the return value of the Colordialog.execute. The IF reserved word follows an expression that generates a Boolean value of TRUE or false. Generally, "=" is used as the relational operator to produce a Boolean value. When the expression is true, the statement after the then is executed. Otherwise, if the code is executed, the IF statement can also have no else part, and automatically jumps to the next line of program when the expression is false.

If statements can be nested, and when a compound statement is used, the compound statement needs to be preceded and begin...end. Else cannot be added before the word ";", and the compiler treats the else statement as belonging to the nearest if statement. If necessary, use the begin...end reserved word to force the else part to be an if statement of a certain level.

2.1.4.2 Case Statement

Case statements apply when a variable or property that is judged is a shape, character, enumeration, or Longint (except for the exception). Logical jumps with case statements are easier to read than writing complex if statements, and the program code is more quickly shaped.

The following routine shows a form that uses a case statement:

Establish the following event handling process:

Procedure Tform1.button1click (Sender:tobject);

Var

Number:integer;

Begin

Number: = Strtoint (Edit1.text);

Case Number of

1,3,5,7,9:label2.caption: = ' odd ';

0,2,4,6,8:label2.caption: = ' even ';

10..100:

Begin

Label2.Caption: = ' between 10 and 100 ';

Form1.color: = Clblue;

End

Else

Label2.Caption: = ' greater than 100 or negative ';

End

End

Executes the program, when the EDIT1 part receives a value and presses the "OK" button to trigger the program, number is assigned to the value entered by the user. The case statement determines which statement to execute based on the value of number. Like an if statement. The case statement also has a selectable else part. The case statement ends with end.

2.1.5 Loop statement

There are three kinds of loop statements in Object Pascal: Repeat, while, and for statements.

2.1.5.1 Repeat Statement

The repeat statement repeats a line or a statement until a state is true. The statement begins with repeat, ends with until, followed by the Boolean expression being judged. See the following routines:

I: = 0;

Repeat

I: = i+1;

Writen (i);

Until i=10;

When this statement is executed, a number from 1 to 10 appears below the form. Boolean expression i=10 (note that, unlike other languages, "=" is a relational operator, not an assignment operation) until repeat ... The end of the until program segment is evaluated, meaning that the repeat statement is executed at least once.

2.1.5.2 while statement

The difference between a while statement and a repeat statement is that its Boolean expression is judged at the beginning of the loop. The while reserved word must follow a Boolean expression. If the result of the expression is true, the loop is executed, otherwise the loop is exited and the program following the while statement is executed.

The following routine achieves the same result as the repeat routine above:

I: = 0;

While i<10 do

Begin

I: = i+1;

Writeln (i);

End

2.1.5.3 for statement

The program code for the For statement executes a certain number of times. It requires a loop variable to control the number of loops. You need to describe a variable whose type can be an integer, a Boolean, a character, an enumeration, or a child.

The following section shows the number from 1 to 5, and I is the control variable:

Var

I:integer;

For I: = 1 to 5 do

Writeln (i);

The above three kinds of circular statements are described. You can use a for statement if you know how many times the loop will execute. The For loop executes faster and has a higher efficiency. If you do not know how many times the loop will execute, but at least execute it once, choose Repeat. until statements are appropriate; When you think that a program may not execute at once, it's best to choose while. Do statement.

2.1.6 Program Module

The program module is a very important concept in Object Pascal. They provide the structure of the application, determine the scope of variables, attribute values, and procedures for program execution. It consists of two parts: an optional Description section and a statement section. If there is a description section, it must precede the statement section. The description section includes variable description, constant description, type description, label description, program, function, method description, etc. The statement section describes the logical actions that can be performed.

In Delphi, the most common program module is the process of event processing module. The following event-handling procedure is a program module that contains a description of the variable:

Procedure Tform.button1click (Sender tobject);

var {program module Description section}

name:string;

Begin {Statement part of program module}

Name: = Edit1.text;

Edit2.text: = ' Welcome to Delphi ' +name;

End {Program Module End}

The library unit is also a program module. The interface part of a library unit contains a description of the library function, type, private, public domain, or a constant or variable. This section can be used as a description of the program module. The implementation section of a library unit typically contains various event-handling procedures, which can be considered as the statement part of a module and an event-handling module. The Library unit module ends at the end of the library unit.

Other program modules can be included in the program module. An event-handling module is included in the Library unit module. The Library unit module is actually in the Engineering program module.

All Delphi applications have the same basic structure. When the program is gradually complex, the module can be added to the program. For example, the event processing module is added to the library module, and the Library unit module is added to the project. Modular programming makes the program structurally sound and protects the data.

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.