0% found this document useful (0 votes)
16 views4 pages

Ex 01

Uploaded by

skamelrech2020
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views4 pages

Ex 01

Uploaded by

skamelrech2020
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

78 - 02: Variables and Data Types

Direct type casting is a dangerous programming practice, because it allows you to


access a value as if it represented something else. Since the internal representations
of data types generally do not match (and might even change depending on the tar-
get platform), you risk accidentally creating hard-to-track errors. For this reason,
you should generally avoid type casting.
The second choice to assign a variable to one of a different type is to use a type con-
version function. A list of functions allowing you to convert between various basic
types is summarized below (and I've already used some of these functions in the
demos of this chapter):
Chr Converts an ordinal number into a character.
Ord Converts an ordinal-type value into the number indicating its order.
Round Converts a real-type value into an Integer-type value, rounding its
value (also see the following note).
Trunc Converts a real-type value into an Integer-type value, truncating its
value.
Int Returns the Integer part of the floating-point value argument.
FloatToDecimal Converts a floating-point value to record including its decimal rep-
resentation (exponent, digits, sign).
FloatToStr Converts a floating-point value to its string representation using
default formatting.
StrToFloat Converts a string to a floating-point value.

note The implementation of the Round function is based on the native implementation offered by the
CPU. Moderns processors generally adopts the so-called "Banker's Rounding", which rounds mid-
dle values (such as 5.5 or 6.5) up and down depending whether they follow an odd or an even
number. There are other rounding functions, such as RoundTo, that offer you more control on the
actual operation.

As mentioned earlier in this chapter, some of these conversion functions are also
available as direct operations on the data type (thanks to the type helper mecha-
nism). While there are classic conversions like IntToStr, you can apply the ToString
operation to most numeric types to convert them to a string representation. There
are many conversions you can apply directly to variables using type helpers, and that
should be your preferred coding style.
Some of these routines work on the data types that we'll discuss in the following sec-
tions. Notice that the table doesn't include routines for special types (such as
TDateTime or variant) or routines specifically intended for formatting more than
conversion, like the powerful Format and FormatFloat routines.

Marco Cantù, Object Pascal Handbook


03: Language Statements - 79

03: language
statements

If the concept of data type was one of the breakthrough of the Pascal programming
language when it was first invented, the other side is represented by the code or pro-
gramming statements. At that time, this idea was clarified by Nicklaus Wirth's
outstanding book “Algorithms + Data Structures = Programs”, published by Prentice
Hall in February 1976 (a classic book, still reprinted and available). While this book
predates object-oriented programming by many years, it can be considered one of
the foundations of modern programming, based on a strong notion of data type, and
in this way a foundation of the concepts that lead to object-oriented programming
languages.
Statements of the programming language are based on keywords (covered in Chap-
ter 1) and other elements which allow you to indicate to a compiler a sequence of
operations to perform. Statements are often enclosed in procedures or functions, as
we'll start to see in more detail in the next chapter. For now, we'll just focus on the
basic types of instructions you can write to create a program.
As we saw in Chapter 1 (in the section covering white space and code formatting),
the actual program code can be written quite freely. I also covered comments and

Marco Cantù, Object Pascal Handbook


80 - 03: Language Statements

some other special elements, but never fully introduced some core concepts, like a
programming statement.

Simple and Compound Statements


Programming instructions are generally called statements. A program block can be
made of a several statements. There are two types of statements, simple and com-
pound.
A statement is called simple when it doesn't contain any other sub-statements.
Examples of simple statements are assignment statements and procedure calls. In
Object Pascal simple statements are separated by a semicolon:
X := Y + Z; // assignment
Randomize; // procedure call
...
To define a compound statement, you can include one of more statements within the
keywords begin and end, which act as brackets. A compound statement can appear
anywhere a simple Object Pascal statement can appear. Here is an example:
begin
A := B;
C := A * 2;
end;
The semicolon after the last statement of the compound statement (that is, before
the end) isn't required, as in the following:
begin
A := B;
C := A * 2
end;
Both versions are correct. The first version has a useless (but harmless) final semi-
colon. This semicolon is, in fact, a null statement or an empty statement; that is, a
statement with no code. This is significantly different from many other program-
ming languages (like those based on the C syntax), in which the semicolon is a
statement terminator (not a separator) and is always required at the end of a state-
ment.
Notice that, at times, a null statement can be specifically used inside loops or in
other particular cases in place of an actual statement, as in:
while condition_with_side_effect do
; // null or empty statement

Marco Cantù, Object Pascal Handbook


03: Language Statements - 81

Although these final semicolons serve no purpose, most developers tend to use them
and I suggest you to do the same. Sometimes after you've written a couple of lines
you might want to add one more statement. If the last semicolon is missing you have
to remember to add it, so it is usually better to add it in the first place. As we'll see
right away, there is an exception to this rule of adding extra semicolons, and that is
when the next element is an else statement inside a condition.

The If Statement
A conditional statement is used to execute either one of the statements it contains or
none of them, depending on a specific test (or condition). There are two basic flavors
of conditional statements: if statements and case statements.
The if statement can be used to execute a statement only if a certain condition is
met (if-then) or to choose between two different alternatives (if-then-else). The
condition is defined with a Boolean expression.
A simple Object Pascal example, called IfTest, will demonstrate how to write condi-
tional statements. In this program we'll use a checkbox to get user input, by reading
its IsChecked property (and storing it to a temporary variable, although this isn't
strictly required, as you could directly check the property value in the conditional
expression):
var
isChecked: Boolean;
begin
isChecked := CheckBox1.IsChecked;
if isChecked then
Show ('Checkbox is checked');
If the checkbox is checked, the program will show a simple message. Otherwise
nothing happens. By comparison, the same statement using the C language syntax
will look like the following (where the conditional expression must be enclosed
within parentheses):
if (isChecked)
Show ("Checkbox is checked");
Some other languages have the notion of an endif element to allow you to write
multiple statements, where in Object Pascal syntax the conditional statement is a
single statement by default. You use a begin-end block to execute more than one
statement as part of the same condition.

Marco Cantù, Object Pascal Handbook

You might also like