Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Block (programming)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Basic semantics== The semantic meaning of a block is twofold. Firstly, it provides the programmer with a way for creating arbitrarily large and complex structures that can be treated as units. Secondly, it enables the programmer to limit the scope of variables and sometimes other objects that have been declared. In early languages such as [[Fortran IV]] and [[BASIC]], there were no statement blocks or control structures other than simple forms of loops. Conditionals were implemented using conditional [[goto]] statements: <syntaxhighlight lang="fortranfixed"> C LANGUAGE: ANSI STANDARD FORTRAN 66 C INITIALIZE VALUES TO BE CALCULATED PAYSTX = .FALSE. PAYSST = .FALSE. TAX = 0.0 SUPTAX = 0.0 C SKIP TAX DEDUCTION IF EMPLOYEE EARNS LESS THAN TAX THRESHOLD IF (WAGES .LE. TAXTHR) GOTO 100 PAYSTX = .TRUE. TAX = (WAGES - TAXTHR) * BASCRT C SKIP SUPERTAX DEDUCTION IF EMPLOYEE EARNS LESS THAN SUPERTAX THRESHOLD IF (WAGES .LE. SUPTHR) GOTO 100 PAYSST = .TRUE. SUPTAX = (WAGES - SUPTHR) * SUPRAT 100 TAXED = WAGES - TAX - SUPTAX </syntaxhighlight> The logical structure of the program is not reflected in the language, and analyzing when a given statement is executed can be difficult. 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: <syntaxhighlight lang="pascal"> { Language: Jensen and Wirth Pascal } if wages > tax_threshold then begin paystax := true; tax := (wages - tax_threshold) * tax_rate { The block structure makes it easier to see how the code could be refactored for clarity, and also makes it easier to do, because the structure of the inner conditional can easily be moved out of the outer conditional altogether and the effects of doing so are easily predicted. } if wages > supertax_threshold then begin pays_supertax := true; supertax := (wages - supertax_threshold) * supertax_rate end else begin pays_supertax := false; supertax := 0 end end else begin paystax := false; pays_supertax := false; tax := 0; supertax := 0 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 (computer programming)|conditional]] statements. The structure of the code reflects the programmer's thinking more closely, making it easier to understand and modify. The above source code can be made even clearer by taking the inner if statement out of the outer one altogether, placing the two blocks one after the other to be executed consecutively. Semantically there is little difference in this case, and the use of block structure, supported by indenting for readability, makes it easy for the programmer to refactor the code. 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. <syntaxhighlight lang="scheme"> ;; Language: R5RS Standard Scheme (let ((empno (ssn-of employee-name))) (while (is-manager empno) (let ((employees (length (underlings-of empno)))) (printf "~a has ~a employees working under him:~%" employee-name employees) (for-each (lambda (empno) ;; Within this lambda expression the variable empno refers to the ssn ;; of an underling. The variable empno in the outer expression, ;; referring to the manager's ssn, is shadowed. (printf "Name: ~a, role: ~a~%" (name-of empno) (role-of empno))) (underlings-of empno))))) </syntaxhighlight> In the above [[Scheme (programming language)|Scheme]] fragment, empno is used to identify both the manager and their 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 they have the choice and it is more difficult to introduce a bug inadvertently.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)