Content deleted Content added
→Syntax: Combine the Algol-family examples into one bullet point |
m Task 70: Update syntaxhighlight tags - remove use of deprecated <source> tags |
||
Line 64:
In primitive languages such as early [[Fortran]] and [[BASIC]], there were a few built-in statement types, and little or no means of extending them in a structured manner. For instance, until 1978 standard Fortran had no "block if" statement, so to write a standard-complying code to implement simple decisions the programmer had to resort to [[goto]]s:
<
C LANGUAGE: ANSI STANDARD FORTRAN 66
C INITIALIZE VALUES TO BE CALCULATED
Line 80:
SUPTAX = (WAGES - SUPTHR) * SUPRAT
100 TAXED = WAGES - TAX - SUPTAX
</syntaxhighlight>
Even in this very brief Fortran fragment, written to the Fortran 66 standard, it is not easy to see the structure of the program, because that structure is not reflected in the language. Without careful study it is not easy to see the circumstances in which a given statement is executed.
Line 86:
Blocks allow the programmer to treat a group of statements as a unit, and the default values which had to appear in initialization in this style of programming can, with a block structure, be placed closer to the decision:
<
{ Language: Jensen and Wirth Pascal }
if wages > tax_threshold then
Line 112:
end;
taxed := wages - tax - supertax;
</syntaxhighlight>
Use of blocks in the above fragment of [[Pascal (programming language)|Pascal]] clarifies the programmer's intent, and enables combining the resulting blocks into a nested hierarchy of conditional statements. The structure of the code reflects the programmer's thinking more closely, making it easier to understand and modify.
Line 120:
In primitive languages, variables had broad scope. For instance, an integer variable called IEMPNO might be used in one part of a Fortran subroutine to denote an employee social security number (ssn), but during maintenance work on the same subroutine, a programmer might accidentally use the same variable, IEMPNO, for a different purpose, and this could result in a bug that was difficult to trace. Block structure makes it easier for programmers to control scope to a minute level.
<
;; Language: R5RS Standard Scheme
(let ((empno (ssn-of employee-name)))
Line 135:
(role-of empno)))
(underlings-of empno)))))
</syntaxhighlight>
In the above [[Scheme (programming language)|Scheme]] fragment, empno is used to identify both the manager and his or her underlings each by their respective ssn, but because the underling ssn is declared within an inner block it does not interact with the variable of the same name that contains the manager's ssn. In practice, considerations of clarity would probably lead the programmer to choose distinct variable names, but he or she has the choice and it is more difficult to introduce a bug inadvertently.
|