Ex 01
Ex 01
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.
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
some other special elements, but never fully introduced some core concepts, like a
programming statement.
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.