Sunday, October 20, 2019
Understanding and Using Loops in Delphi Programming
Understanding and Using Loops in Delphi Programming The loop is a common element in all programming languages. Delphi has three control structures that execute blocks of code repeatedly: for, repeat ... until and while ... do. The FOR loop Suppose we need to repeat an operation a fixed number of times. // show 1,2,3,4,5 message boxesvar j: integer;beginfor j : 1 to 5 dobeginShowMessage(Box: IntToStr(j)) ;end;end; The value of a control variable (j), which is really just a counter, determines how many times a for statement runs. The keyword for sets up a counter. In the preceding example, the starting value for the counter is set to 1. The ending value is set to 5.When the for statement begins running the counter variable is set to the starting value. Delphi than checks whether the value for the counter is less than the ending value. If the value is greater, nothing is done (program execution jumps to the line of code immediately following the for loop code block). If the starting value is less than the ending value, the body of the loop is executed (here: the message box is displayed). Finally, Delphi adds 1 to the counter and starts the process again. Sometimes it is necessary to count backward. The downto keyword specifies that the value of a counter should be decremented by one each time the loop executes (it is not possible to specify an increment / decrement other than one). An example of a for loop that counts backward. var j: integer;beginfor j : 5 downto 1 dobeginShowMessage(T minus IntToStr(j) seconds) ;end;ShowMessage(For sequence executed!) ;end; Note: its important that you never change the value of the control variable in the middle of the loop. Doing so will cause errors. Nested FOR loops Writing a for loop within another for loop (nesting loops) is very useful when you want to fill / display data in a table or a grid. var k,j: integer;begin//this double loop is executed 4x416 timesfor k: 1 to 4 dofor j: 4 downto 1 doShowMessage(Box: IntToStr(k) , IntToStr(j)) ;end; The rule for nesting for-next loops is simple: the inner loop (j counter) must be completed before the next statement for the outer loop is encountered (k counter). We can have triply or quadruply nested loops, or even more. Note: Generally, the begin and end keywords are not strictly required, as you can see. If begin and end are not used, the statement immediately following the for statement is considered the body of the loop. The FOR-IN loop If you have Delphi 2005 or any newer version, you can use the new for-element-in-collection style iteration over containers. The following example demonstrates iteration over string expressions: for each char in string check if the character is either a or e or i. consts About Delphi Programming;varc : char;beginfor c in s dobeginif c in [a,e,i] thenbegin// do somethingend;end;end; The WHILE and REPEAT loops Sometimes we wont know exactly how many times a loop should cycle. What if we want to repeat an operation until we reach a specific goal? The most important difference between the while-do loop and the repeat-until loop is that the code of the repeat statement is always executed at least once. The general pattern when we write a repeat (and while) type of loop in Delphi is as follows: repeatbeginstatements;end;until condition true while condition true dobeginstatements;end; Here is the code to show 5 successive message boxes using repeat-until: varj: integer;beginj:0;repeatbeginj : j 1;ShowMessage(Box:IntToStr(j)) ;end;until j 5;end; As you can see, the repeat statement evaluates a condition at the end of the loop (therefore repeat loop is executed for sure at least once). The while statement, on the other hand, evaluates a condition at the beginning of the loop. Since the test is being done at the top, we will usually need to make sure that the condition makes sense before the loop is processed, if this is not true the compiler may decide to remove the loop from the code. var j: integer;beginj:0;while j 5 dobeginj:j1;ShowMessage(Box:IntToStr(j)) ;end;end; Break and Continue The Break and Continue procedures can be used to control the flow of repetitive statements: The Break procedure causes the flow of control to exit a for, while, or repeat statement and continue at the next statement following the loop statement. Continue allows the flow of control to proceed to the next iteration of repeating operation.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.