Loop of program flow control (Delphi)

Source: Internet
Author: User

In a cyclical programming language, a common element can be used to repeatedly execute an action to know a specific condition.

All loops have the following elements:

  • Start Point

  • Subject

  • End Point

  • Condition for loop end

  • Secondary break and continue Processes

The starting point of the loop isObject PascalLoop Statement (For,WhileAndRepeat), The statement contained in the subject must be executed once during each loop iteration, and the subject contains any validObject PascalCode, which can be a single line of code or multiple lines of code. If the subject contains multiple lines of code, the code must beBeginAndEndStatement block (onlyRepeatLoop exceptions ). The end point of the loop is either a keyword.End(InForLoop andWhileIn a loop) or a keyword.Until(InRepeatLoop ). Keywords are not required when the subject of a loop is a single line of code.BeginAndEnd.

Most cycles follow these steps: Enter the cycle and test the condition. If the test result isFalseWhen the program is executed to the bottom of the loop body, it will jump back to the top of the loop and check the condition again. If the test result is stillFalse, Repeat the above process. If the test result isTrue, The program will immediately jump to the code line immediately following the circular code block. (These descriptions areRepeatLoop is an exception. It tests the condition at the bottom of the loop, not at the top of the loop .)

Caution

Occasionally, it is easy to write the cyclic test conditions as permanentFalseThe result will lock or suspend the program and keep repeating. In this case, you can onlyWindows Task ManagerTo terminate the process.

Tip

In DelphiRun buttonOr pressF9To kill a program running in IDE, you can selectRun | program resetOr pressCTRL + F2.

It can be used in DelphiFor to/down to do,While... DoAndRepeat... UntilThree methods to achieve loop control.

VaR I: integer; X: integer; begin X: = 10; for I: = 0 to 9 do {I changes progressively from 0 to 9, execute dosomething} begin {dosomething} end 10 times in sequence; for I: = 0 to PRED (x) do {PRED (x) returns the previous value of X, that is, 9} begin {dosomething} end; for I: = 9 downto 0 do {I is decreased from 9 to 0, and 10 dosomething} begin {dosomething} end; X: = 8; {I decreases from 9 to 0 and loops 10 times} For I: = succ (X) downto 0 do begin {succ function returns the last {dosomething} end; I: = 0; while I <= 10 do {and for I: = 0 to 10 do functions are the same} begin showmessage (inttostr (I); Inc (I); {While features are that you can control the cyclic factor} end; I: = 0; repeat {and while I <= 10 do functions are the same} showmessage (inttostr (I); Inc (I); until I = 10; I: = 0; {Inc increments with the specified value, and Dec decreases with the specified value} Inc (I); {I = 1} Inc (I, 2 ); {I = 3} Dec (I, 2); {I = 1} Dec (I); {I = 0} showmessage (inttostr (I); end;

Caution

When using the for method, you must note that the step of the cyclic variable is fixed to 1, and the Code cannot be modified in the recycle body.

While is more flexible than.

Repeat evolved from while. While first, you must determine whether the condition is true before execution, while repeat is the first execution, and then determine whether the condition is true.

Note

In the above code, I variable names are originated from the Fortran language, which is also a habit of for loop usage.

Continue and break Processes

Continue ProcessTo force the program to run at the bottom of the loop and enter a loop, and skip all the statements after the continue. If you are in try... In the Finally block, the finally... end block is executed before entering the next loop.

var  I:Integer;begin  for I := 0 to 2 do  begin    try      if I < 1 then        Continue;      ShowMessage(IntToStr(I));    finally      ShowMessage('finally..end');    end;  end;end;

The running results are as follows (0, 1, and 2 represent the cyclic variables respectively.I display results of changes):

Break ProcessTo terminate the loop before the normal end of the loop. If you are in try... In the Finally block, then finally... The end block is also executed, for example, the following code:

var  I:Integer;begin  for I := 0 to 2 do  begin    try      if I > 1 then        Break;      ShowMessage(IntToStr(I));    finally      ShowMessage('finally..end');    end;  end;end;

The running results are as follows (0, 1, and 2 represent the cyclic variables respectively.I display results of changes):

Continue and break can only be used in the for, while, and repeat loops. If these two processes are used outside the loop, a compiler error will occur.

The above code has passed the test in Delphi7.

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.