Content deleted Content added
No edit summary Tags: Reverted Mobile edit Mobile web edit |
JavaSucksMan (talk | contribs) →Syntax: added Haskell to offside rule |
||
(27 intermediate revisions by 21 users not shown) | |||
Line 1:
{{Short description|Lexical structure of source code grouped together}}
Blocks in programming is to enable groups of statements to be treated as if they were one statement and to narrow the of objects such as variables. Procedures and functions declared in a block so that they do not conflict with those having 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 declared with the same name.▼
{{distinguish|Block programming}}
{{redirect|Code block|the IDE|Code::Blocks|block-based programming|Visual programming language}}
{{refimprove|date=May 2010}}
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.
▲
==History==
Line 14 ⟶ 21:
| issue = 12
| pages = 8–22
|s2cid = 28755282 | doi-access = free
}}</ref> 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."<ref name="algol60_report">{{cite journal |last1=Backus |first1=J. W. |author-link1=John Backus |last2=Bauer |first2=F. L. |author-link2=Friedrich L. Bauer |last3=Green |first3=J. |last4=Katz |first4=C. |last5=McCarthy |first5=J. |last6=Perlis |first6=A. J. |last7=Rutishauser |first7=H. |author-link7=Heinz Rutishauser |last8=Samelson |first8=K. |last9=Vauquois |first9=B. |author-link9=Bernard Vauquois |last10=Wegstein |first10=J. H. |last11=van Wijngaarden |first11=A. |last12=Woodger |first12=M. |date=May 1960 |editor1-last=Naur |editor1-first=Peter |title=Report on the Algorithmic Language ALGOL 60 |url=http://www.masswerk.at/algol60/report.htm |journal=Communications of the ACM |___location=New York, NY, USA |publisher=ACM |volume=3 |issue=5 |pages=299–314 |doi=10.1145/367236.367262 |issn=0001-0782 |s2cid=278290 |access-date=2009-10-27 |doi-access=free |editor1-link=Peter Naur}}</ref>
==Syntax==
Blocks use different syntax in different languages.
* 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.
* 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>
* 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>
Line 56 ⟶ 39:
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
<syntaxhighlight lang="
C LANGUAGE: ANSI STANDARD FORTRAN 66
C INITIALIZE VALUES TO BE CALCULATED
Line 76 ⟶ 59:
</syntaxhighlight>
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:
Line 91 ⟶ 74:
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
else begin
pays_supertax := false;
supertax := 0
end▼
end
else begin
paystax := false; pays_supertax := false;
tax := 0; supertax := 0
taxed := wages - tax - supertax;
</syntaxhighlight>
Line 121 ⟶ 103:
(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,
Line 131 ⟶ 113:
</syntaxhighlight>
In the above [[Scheme (programming language)|Scheme]] fragment, empno is used to identify both the manager and
==Hoisting==
In some languages, a variable can be declared at function scope even within enclosed blocks. For example, in JavaScript, variables declared with <code>var</code> have function scope.
==See also==
Line 147 ⟶ 127:
==References==
{{Reflist}}
{{Programming paradigms navbox}}
{{DEFAULTSORT:Statement Block}}
[[Category:Programming constructs]]
|