Block (programming): Difference between revisions

Content deleted Content added
No edit summary
Tags: Reverted Visual edit Mobile edit Mobile web edit Disambiguation links added
Syntax: added Haskell to offside rule
 
(2 intermediate revisions by 2 users not shown)
Line 4:
{{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.
 
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.
Line 25:
 
==Syntax==
Blocks use different syntax in different languages. TwoSeveral broad families are:
 
* 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>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>