05.05.2014 Views

csmstr - Omega Engineering

csmstr - Omega Engineering

csmstr - Omega Engineering

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

PROGRAMMING TIPS<br />

CRIMSON USER MANUAL - MODULAR CONTROLLER<br />

too intimidating, a series of if statements can be used instead to produce the same results, but<br />

with marginally lower performance, and somewhat less readability.)<br />

LOCAL VARIABLES<br />

Some programs use variables to store intermediate results, or to control one of the various<br />

loop constructs described below. Rather than defining a tag to hold these values, you can<br />

declare what are known as local variables using the syntax shown below…<br />

int a; // Declare local integer ‘a’<br />

float b; // Declare local real ‘b’<br />

cstring c;<br />

// Declare local string ‘c’<br />

Local variables may optionally be initialized when they are declared by following the variable<br />

name with := and the value to be assigned. Variables that are not initialized in this manner<br />

are set to zero, or an empty string, as appropriate.<br />

Note that local variables are truly local in both scope and lifetime. This means that they<br />

cannot be referenced outside the program, and they do not retain their values between<br />

function invocations. If a function is called recursively, each invocation has its own variables.<br />

LOOP CONSTRUCTS<br />

The three different loop constructs can be used to perform a given section of code while a<br />

certain condition is true. The while loop tests its condition before the code is executed, while<br />

the do loop tests the condition afterwards. The for loop is a quicker way of defining a while<br />

loop, allowing you to combine three common elements into one statement.<br />

You should note that some care is required when using loops within your programs, as you<br />

may make a programming error which results in a loop that never terminates. Depending on<br />

the situation in which the program is invoked, this may seriously disrupt the Master’s user<br />

interface activity, or its communications. Loops which iterate too many times may also cause<br />

performance issues for the subsystem that invokes them.<br />

THE WHILE LOOP<br />

This type of loop repeats the action that follows it while the condition in the while statement<br />

remains true. If the condition is never true, the action will never be executed, and the loop<br />

will perform no operation beyond evaluating the controlling condition. If you want more than<br />

one action to be included in the loop, be sure to surround the multiple statements in curlybrackets,<br />

as with the if statement. The example below initializes a pair of local variables, and<br />

then uses the first to loop through the contents of an array, totaling the first ten elements, and<br />

returning the total value to the caller…<br />

The architecture of the while loop statement is as follow…<br />

while ( condition ){<br />

Action;<br />

}<br />

PAGE 232<br />

http://www.redlion.net/controller

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!