Block (programming): Difference between revisions

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search
imported>JavaSucksMan
Syntax: added Haskell to offside rule
 
imported>Curlyquote
m Fix incorrect semicolons
 
Line 1: Line 1:
{{Short description|Lexical structure of source code grouped together}}
{{Short description |Demarcated group of source code statements that run in sequence}}
{{distinguish|Block programming}}
{{distinguish|Block programming}}
{{redirect|Code block|the IDE|Code::Blocks|block-based programming|Visual programming language}}
{{redirect|Code block|the IDE|Code::Blocks|block-based programming|Visual programming language}}
{{refimprove|date=May 2010}}
{{refimprove|date=May 2010}}
In [[computer programming]], a '''block''' of [[source code]] is a grouping of [[Statement (computer science)|statements]] that [[Execution (computing)|execute]] in sequential order, top to bottom. The block structure is fundamental to [[structured programming]], where [[control structure]]s are formed with blocks. [[Syntax (programming languages)|Syntactically]], a block acts much like a single statement in that a control structure generally operates either on a single statement or a block.


In [[computer programming]], a '''block''' or '''code block''' or '''block of code''' is a lexical structure of [[source code]] which is grouped together. Blocks consist of one or more [[Declaration (computer programming)|declarations]] and [[Statement (computer science)|statements]]. A [[programming language]] that permits the creation of blocks, including blocks [[Nesting (computing)|nested]] within other blocks, is called a '''block-structured programming language'''. Blocks are fundamental to [[structured programming]], where [[control structure]]s are formed from blocks.
The nature of a block varies by [[programming language]], but there are typical features. A block usually provides for visibility [[scope (computer science)|scope]] such that an [[identifier]] defined in a block is not visible in outer, containing blocks, but is visible in inner, [[Nesting (computing)|nested]] blocks unless it is [[Name masking |masked]] by an identifier with the same name. A block usually provides for lifetime scope such that [[system resource |resources]] associated with an identifier are partially or fully released when [[control flow]] reaches the end of the block in which the identifier was defined.
 
Blocks have two functions: to group statements so that they can be treated as one statement, and to define [[scope (computer science)|scopes]] for [[name binding|names]] to distinguish them from the same name used elsewhere. In a block-structured programming language, the objects named in outer blocks are visible inside inner blocks, unless they are [[Name masking|masked]] by an [[Object (computer science)|object]] declared with the same name.


==History==
==History==
Ideas of block structure were developed in the 1950s during the development of the first [[autocode]]s, and were formalized in the [[Algol 58]] and [[Algol 60]] reports. Algol 58 introduced the notion of the "compound statement", which was related solely to [[control flow]].<ref name="algol58_report">{{cite journal
The block concept was developed in the 1950s during the development of the first [[autocode]]s, and was formalized in the [[Algol 58]] and [[Algol 60]] reports. Algol 58 introduced the notion of the "compound statement", which was related solely to [[control flow]].<ref name="algol58_report">{{cite journal
|last1= Perlis |first1 = A. J. |author-link1=Alan Perlis
|last1= Perlis |first1 = A. J. |author-link1=Alan Perlis
|last2= Samelson |first2= K.
|last2= Samelson |first2= K.
Line 25: Line 24:


==Syntax==
==Syntax==
Blocks use different syntax in different languages. Several broad families are:
A block is demarcated in code as a separate unit, but the syntax varies by language. Notable examples include:
 
; Curly braces: [[C (programming language)|C]] and many related languages delimit a block with <code>{</code> and <code>}</code>. In fact, this is the defining quality of a [[curly brace language]].
 
; Indentation: [[Python (programming language)|Python]] and [[Haskell]] use [[off-side rule |indentation]] to demarcate a block instead of delimiters as most languages do.
 
; Parentheses: The [[batch file]] syntax uses <code>(</code> and <code>)</code>.
 
; begin/end: The [[ALGOL]] family generally delimits a block with the keywords <code>begin</code> and <code>end</code>; [[ALGOL 68]] uses parentheses.
 
; Control verb reversed: With [[ALGOL 68]], then in [[Edsger W. Dijkstra]]'s 1974 [[Guarded Command Language#Selection: if|Guarded Command Language]] the conditional and iterative code block are alternatively terminated with the block reserved word ''reversed'': e.g. <code>'''if''' ~ then ~ elif ~ else ~ '''fi'''</code>, <code>'''case''' ~ in ~ out ~ '''esac'''</code> and <code>for ~ while ~ '''do''' ~ '''od'''</code>.


* the [[ALGOL]] family in which blocks are delimited by the keywords "<code>begin</code>" and "<code>end</code>" or equivalent. In [[C (programming language)|C]], blocks are delimited by curly braces - "<code>{</code>" and "<code>}</code>". [[ALGOL 68]] uses parentheses.
; [[S-expression]]: With a syntactic keyword such as <code>prog</code> or <code>let</code> as in the [[Lisp (programming language)|Lisp]] family.
* Parentheses - "<code>(</code>" and "<code>)</code>", are used in the MS-DOS [[batch language]]
* [[off-side rule|indentation]], as in [[Python (programming language)|Python]] and [[Haskell]]
* [[s-expression]]s with a syntactic keyword such as <code>prog</code> or <code>let</code> (as in the [[Lisp (programming language)|Lisp]] family)
* In 1968 (with [[ALGOL 68]]), then in [[Edsger W. Dijkstra]]'s 1974 [[Guarded Command Language#Selection: if|Guarded Command Language]] the conditional and iterative code block are alternatively terminated with the block reserved word ''reversed'': e.g. <code><u>'''if'''</u> ~ <u>then</u> ~ <u>elif</u> ~ <u>else</u> ~ <u>'''fi'''</u></code>, <code><u>'''case'''</u> ~ <u>in</u> ~ <u>out</u> ~ <u>'''esac'''</u></code> and <code><u>for</u> ~ <u>while</u> ~ <u>'''do'''</u> ~ <u>'''od'''</u></code>


==Limitations==
==Limitations==
Some languages which support blocks with declarations do not fully support all declarations; for instance many C-derived languages do not permit a function definition within a block ([[nested function]]s). And unlike its ancestor Algol, Pascal does not support the use of blocks with their own declarations inside the begin and end of an existing block, only compound statements enabling sequences of statements to be grouped together in '''if''', '''while''', '''repeat''' and other control statements.
Some languages which support declarations in a block do not support all declarations. For instance, many C-related languages do not permit a [[nested function |function definition within a block]]. And unlike its ancestor Algol, Pascal does not support the use of blocks with their own declarations inside the begin and end of an existing block, only compound statements enabling sequences of statements to be grouped together in {{code |if}}, {{code |while}}, {{code |repeat}} and other control statements.{{clarify| What does 'existing block' mean? what does this mean: 'only compound statements enabling sequences of statements to be grouped together ...'? |date=September 2025}}


==Basic semantics==
==Examples==
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 the early days of computing, many languages such as [[FORTRAN IV]] and [[BASIC]] lacked block syntax except sometimes for rudimentary loop constructs. A conditional was encoded using the [[goto]] statement. In the following FORTRAN 66 code, the logical structure is less than clear due to the lack of blocks.
 
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">
<syntaxhighlight lang="fortranfixed">
C    LANGUAGE: ANSI STANDARD FORTRAN 66
C    INITIALIZE VALUES TO BE CALCULATED
C    INITIALIZE VALUES TO BE CALCULATED
       PAYSTX = .FALSE.
       PAYSTX = .FALSE.
Line 59: Line 61:
</syntaxhighlight>
</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. The following code in Jensen and Wirth Pascal shows that 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. Use of blocks in the fragment below 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 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.
 
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">
<syntaxhighlight lang="pascal">
    { Language: Jensen and Wirth Pascal }
     if wages > tax_threshold then
     if wages > tax_threshold then
         begin
         begin
         paystax := true;
         paystax := true;
         tax := (wages - tax_threshold) * tax_rate
         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
         if wages > supertax_threshold then begin
             pays_supertax := true;
             pays_supertax := true;
Line 90: Line 84:
</syntaxhighlight>
</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.
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.


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 the following R5RS Standard [[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. Within the 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.
 
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">
<syntaxhighlight lang="scheme">
;; Language: R5RS Standard Scheme
(let ((empno (ssn-of employee-name)))
(let ((empno (ssn-of employee-name)))
   (while (is-manager empno)
   (while (is-manager empno)
Line 104: Line 95:
       (for-each
       (for-each
         (lambda (empno)
         (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~%"
           (printf "Name: ~a, role: ~a~%"
                   (name-of empno)
                   (name-of empno)
Line 112: Line 100:
         (underlings-of empno)))))
         (underlings-of empno)))))
</syntaxhighlight>
</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.


==Hoisting==
==Hoisting==
Line 123: Line 109:
* [[Block scope]]
* [[Block scope]]
* [[Closure (computer programming)]]
* [[Closure (computer programming)]]
* [[Control flow]]
* [[Decomposition (computer science)]]


==References==
==References==

Latest revision as of 01:05, 18 December 2025

Template:Short description Script error: No such module "Distinguish". Script error: No such module "redirect hatnote". Template:Refimprove In computer programming, a block of source code is a grouping of statements that execute in sequential order, top to bottom. The block structure is fundamental to structured programming, where control structures are formed with blocks. Syntactically, a block acts much like a single statement in that a control structure generally operates either on a single statement or a block.

The nature of a block varies by programming language, but there are typical features. A block usually provides for visibility scope such that an identifier defined in a block is not visible in outer, containing blocks, but is visible in inner, nested blocks unless it is masked by an identifier with the same name. A block usually provides for lifetime scope such that resources associated with an identifier are partially or fully released when control flow reaches the end of the block in which the identifier was defined.

History

The block concept was developed in the 1950s during the development of the first autocodes, and was formalized in the Algol 58 and Algol 60 reports. Algol 58 introduced the notion of the "compound statement", which was related solely to control flow.[1] The subsequent Revised Report which described the syntax and semantics of Algol 60 introduced the notion of a block and block scope, with a block consisting of " A sequence of declarations followed by a sequence of statements and enclosed between begin and end..." in which "[e]very declaration appears in a block in this way and is valid only for that block."[2]

Syntax

A block is demarcated in code as a separate unit, but the syntax varies by language. Notable examples include:

Curly braces
C and many related languages delimit a block with { and }. In fact, this is the defining quality of a curly brace language.
Indentation
Python and Haskell use indentation to demarcate a block instead of delimiters as most languages do.
Parentheses
The batch file syntax uses ( and ).
begin/end
The ALGOL family generally delimits a block with the keywords begin and end; ALGOL 68 uses parentheses.
Control verb reversed
With ALGOL 68, then in Edsger W. Dijkstra's 1974 Guarded Command Language the conditional and iterative code block are alternatively terminated with the block reserved word reversed: e.g. if ~ then ~ elif ~ else ~ fi, case ~ in ~ out ~ esac and for ~ while ~ do ~ od.
S-expression
With a syntactic keyword such as prog or let as in the Lisp family.

Limitations

Some languages which support declarations in a block do not support all declarations. For instance, many C-related languages do not permit a function definition within a block. And unlike its ancestor Algol, Pascal does not support the use of blocks with their own declarations inside the begin and end of an existing block, only compound statements enabling sequences of statements to be grouped together in if, while, repeat and other control statements.Script error: No such module "Unsubst".

Examples

In the early days of computing, many languages such as FORTRAN IV and BASIC lacked block syntax except sometimes for rudimentary loop constructs. A conditional was encoded using the goto statement. In the following FORTRAN 66 code, the logical structure is less than clear due to the lack of blocks.

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

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. The following code in Jensen and Wirth Pascal shows that 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. Use of blocks in the fragment below 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. The 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.

    if wages > tax_threshold then
        begin
        paystax := true;
        tax := (wages - tax_threshold) * tax_rate
        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;

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.

In the following R5RS Standard 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. Within the 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.

(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)
          (printf "Name: ~a, role: ~a~%"
                  (name-of empno)
                  (role-of empno)))
        (underlings-of empno)))))

Hoisting

In some languages, a variable can be declared at function scope even within enclosed blocks. For example, in JavaScript, variables declared with var have function scope.

See also

Script error: No such module "Portal".

References

<templatestyles src="Reflist/styles.css" />

  1. Script error: No such module "Citation/CS1".
  2. Script error: No such module "Citation/CS1".

Script error: No such module "Check for unknown parameters".

Template:Programming paradigms navbox