Lesson 4 -- For statements


For Statement

Probably the second most encountered method of program flow control is the FOR statement. The FOR statement executes a given number of statements a specified number of times. The syntax of the FOR statement is:

ex4-4.gif (1976 bytes)
figure 4-4

<integer_var> is any previously declared integer variable. <start> is a starting value. <end> is the end value. <steps> is the amount to increment <integer_var> each time. The BY <steps> portion is optional and if not specified the value will be incremented by 1. Notice the use of the [ := ] assignment operator. This is because we are assigning the values specified by <start> To <end>. The FOR loop does also evaluate that i doesn't exceed <end> but we still use the assignment operator. So in other words what this does is successively assign the numbers from <start> to <end> to <integer_var> all the while making sure that <integer_var> never exceeds <end>. Once <integer_var> is greater than <end> the loop ends and the code following the loop is executed.

Here is an example:

ex4-5.gif (1477 bytes)
figure 4-5

This loop will run 10 times and each time the operation x := x + 1 will be performed. Assuming x is set to 0 at the beginning of this loop x will be 10 when the loop exits.

If we were to modify our example thus:

ex4-6.gif (1554 bytes)
figure 4-6

x would start at 0 and be incremented 5 times. The sequence would look like this i := 1, 3, 5, 7, 9. The next value would be 11 which is greater than the specified <end> value (10) so the loop ends. You can use the <integer_var> within the loop if you need to.

For example:

ex4-7.gif (7578 bytes)
figure 4-7

What this piece of code would do is use the i value to display a message. The first line of the <statements> converts the integer i to a string int_str so that we can use it in string functions. The next line sets msg_str to i := with a trailing space. If we didn't add the trailing space the output would look like i :=1. We prefer to space it so it looks nice. The third line joins msg_str and int_str together and the last <statement> line displays the results on the message line. The results would look like:

i := 1
i := 2
i := 3....etc.

You would use a FOR statement anywhere in your code that you want to repeat an action. A good example would be filling an ARRAY with data. Use of the FOR statement can significantly reduce code. Lets say you have an ARRAY of 100 integers and you want them to initially be the numbers from 1 to 100 you could write:

array_var[1] := 1;
array_var[2] := 2;
array_var[3] := 3;....etc.

but my math tells me that would require you to write the same statement somewhere around 100 times.

To simplify matters try this:

ex4-8.gif (1565 bytes)
figure 4-8

Three lines of code versus 100. You decide. You can use the <integer_var> in any way you would use any other integer variable. Be careful how you use it. You could stop the loop execution prematurely if you are not careful. For example:

ex4-9.gif (1437 bytes)
figure 4-9

While this doesn't do much, if it was supposed to do something 10 times it won't because you have set i to a number greater than the <end> value. The loop will be run once, setting i to 11. Then the loop will evaluate again at the top only to discover that the <end> value has already been passed. You can use this method to your advantage but the point here is you should be careful what you do with the <integer_var> The other thing to consider is that the <start>, <end> and <step> values may be any valid integer or statement that evaluates to an integer. So if we have a variable j that is an integer we could write:

ex4-10.gif (1528 bytes)
figure 4-10

This allows us to execute a series of statements a number of times that varies when the program is run. You as the programmer do not have to know how many times the loop will need to run. For instance if you wanted to write a macro to copy selected entities a user specified number of times you would use a loop such as this utilizing the number of times as the <end> value. Here is the full listing for a working FORTEST.DCS. Experiment with this to gain more understanding of how FOR loops work.

ex4-11.gif (7156 bytes)
figure 4-11
 
Complete listing for FORTEST.DCS.  Click the image to download the source code. If your browser prompts you to run the file or save it to a disk, select save to disk. If you create a file association for *.DCS files this file will automatically be opened in your editor.
 

< Previous Lesson   |   Next Lesson >
DCAL Tutorial Contents



SITE SEARCH