Download Essential Pascal

Transcript
Button1.Color := clRed; // error!
the compiler would have issued an error. In general, we can say that since the with
statement introduces new identifiers in the current scope, we might hide existing identifiers, or
wrongfully access another identifier in the same scope (as in the first version of this code
fragment). Even considering this kind of drawback, I suggest you get used to with statements,
because they can be really very handy, and at times even make the code more readable. You
should, however, avoid using multiple with statements, such as:
with ListBox1, Button1 do...
The code following this would probably be highly unreadable, because for each property
defined in this block you would need to think about which component it refers to, depending on the
respective properties and the order of the components in the with statement.
Speaking of readability, Pascal has no endif or endcase statement. If an if statement has
a begin-end block, then the end of the block marks the end of the statement. The case
statement, instead, is always terminated by an end. All these end statements, often found one
after the other, can make the code difficult to follow. Only by tracing the indentations can you see
which statement a particular end refers to. A common way to solve this problem and make the
code more readable is to add a comment after the end statement indicating its role, as in:
end; // if
Conclusion
This chapter has described how to code conditional statements and loops. Instead of
writing long lists of such statements, programs are usually split in routines, procedures or
functions. This is the topic of the next chapter, which introduces also some advanced elements.
Essential Pascal [Copyright 1995-2003 Marco Cantù] www.marcocantu.com/epascal
47