2.2 The Statement
The text of any program can be printed on a piece of paper. The text of the program can be read, studied, and analyzed. When we write a program or understand a program’s text, we usually do so by imagining in our head the way the the program is executed. A program consists of a set of precisely written steps for the implementation of an objective. The steps are executed in a certain order. The execution order does not need to be the same as the textual order, i.e., the order in which the program is written or printed on paper. Only in an extremely simple program, the execution order of statements is sequential. In such a simple program, the steps in the program are executed one after the other. In most programs, the sequential and default execution order of statements is altered using what are called control structures.
Statements are of two types: simple and compound. The statements can be organized into related groups or compound statements, and the statements—simple or compound—can be executed in an order that is different from the print order. A block of statements is simply a sequence of statements separated by ; and enclosed within braces. A block is a compound statement. Blocks of statements can be repeatedly executed, or sometimes skipped based on the evaluation of conditional statements. Subroutines, which are an essential part of any realistic program, are usually placed at the end or the beginning of the program, but called or executed from almost anywhere in the program, including from the same or other subroutines.
An individual executable component or building block of a program is called a statement. A statement must be independently executable. Thus,
$temperature = 10;
or
$temperature =
is a statement. In particular, each one of the above is an assignment statement. In each case, the statement can be executed independently. In Perl, each statement is ended by ;. The ; is like the period (.) in a declarative English sentence. A ; is necessary unless the statement is the last statement of a block, discussed later in this chapter. We emphasize the need for statements to be independently executable as opposed to expressions. An expression is executable as well, but is usually a part of statement. For example, in the statement
$celsius = 5/9 * ($fahrenheit - 32);
what we have to the right of the = sign, viz.,
5/9 * ($fahrenheit - 32)
is an expression and not a statement. It normally does not make sense to write it as an independent statement as given below.
5/9 * ($fahrenheit - 32);
Of course, if we write a statement as the one given immediately above, the computation is performed, but the result is wasted without the assignment. In particular, 5/9 * ($fahrenheit- 32) is an arithmetic expression.
