Curly bracket programming language: Difference between revisions

Content deleted Content added
Dexbot (talk | contribs)
m Bot: Fixing broken section link
Tag: Redirect target changed
 
(47 intermediate revisions by 30 users not shown)
Line 1:
#REDIRECT [[List of programming languages by type#Curly bracket languages]]
{{Unreferenced|date=April 2007}}
'''Curly brace''' or '''bracket''' [[programming language]]s are those which use [[balance]]d [[bracket]]s ('''{''' and '''}''', also known as "squiggly brackets", "brace brackets" or simply "braces") to make [[block (programming)|block]]s in their [[syntax]] or [[formal grammar]], mainly due to being [[C (programming language)|C]]-influenced. The main alternate style is the use of paired keywords, although some languages (notably [[Python (programming language)|Python]] and [[Occam (programming language)|Occam]]) instead use an [[Off-side_rule|off-side]] style, and [[Lisp (programming language)|Lisp]] uses parentheses.
 
{{Redirect category shell|1=
== History ==
{{R to section}}
Curly-bracket syntax pre-dates C. [[BCPL]] was the first language to use curly brackets to outline multi-statement function bodies. [[Ken Thompson]] used the feature in [[B (programming language)|B]], his cut-down version of BCPL. Because [[C (programming language)|C]] was initially designed after B, it has retained the bracket syntax of B, as have many subsequent languages ([[C++]], [[Java (programming language)|Java]], [[JavaScript]] and its generalized standard [[ECMAScript]], [[C Sharp (programming language)|C#]], [[D (programming language)|D]], [[PHP]], etc.). [[Pico programming language|Pico]] is a non-C descendant that also uses this style.
}}
 
One common part of curly bracket style is the common style of terminating a statement with a semicolon (;), which is one way for languages to ignore whitespace. BCPL and Pico do not have this rule; a newline is used as a statement terminator in such languages. The [[Indent style#Pico style|Pico indent style]] is then used, as below (BCPL)
LET FUNC foo(a) = VALOF
{ b := a + 1
RESULTIS b }
 
== Statement blocks ==
The name derives from the common syntax of the languages, where blocks of [[statement (programming)|statement]]s are enclosed in curly brackets. For example (using [[Berkeley Software Distribution|BSD]]/[[Eric Allman|Allman]] [[indent style]], one of many stylistic ways to format a program):
 
for (int i = 0; i < 10; i++)
'''{'''
printf("%d", i);
doTask(i);
'''}'''
 
Languages in this family are sometimes referred to as '''C-style''', because they tend to have syntax that is strongly influenced by [[C syntax]]. Beside the curly brackets, they often inherit other syntactic features, such as using the semicolon as a statement terminator (not as a separator), and the three-part "for" statement syntax as shown above.
 
Generally, these languages are also considered "free-form languages", meaning that the compiler considers all whitespace to be the same as one blank space, much like [[HTML]]. Considering that, the above code could be written:
 
for(int i=0;i<10;i++)'''{'''printf("%d",i);doTask(i);'''}'''
 
but this is not recommended, as it becomes difficult to read after the program grows beyond a few statements.
 
A popular way to work with curly braces is with the [[K&R]] style:
 
int i;
for(i = 0; i < 10; i++) '''{'''
printf("%d", i);
doTask(i);
'''}'''
 
There are many other ways to identify statement blocks, such as ending keywords that may match beginning keywords (in [[Ada (programming language)|Ada]], [[Pascal (programming language)|Pascal]], [[REXX]], and [[Visual Basic]]), the [[Off-side rule]] of indentation (in [[Python (programming language)|Python]]), or other symbols such as parentheses (in [[Lisp programming language|Lisp]]). These ways are not necessarily exclusive: whereas indentation is the default in [[Haskell (programming language)|Haskell]], curly brackets can be used when desired.
 
=== Loops ===
In C, C++, C#, D, Java, PHP, Perl and JavaScript:
while (''boolean expression'')
'''{'''
''statement(s)''
'''}'''
 
do
'''{'''
''statement(s)''
'''}''' while (''boolean expression'');
 
for (''initialization''; ''continuation condition''; ''incrementing expr'')
'''{'''
''statement(s)''
'''}'''
 
=== Conditional statements ===
In C, C++, C#, D, PHP, and Java:
if (''boolean expression'')
'''{'''
''statement(s)''
'''}'''
 
if (''boolean expression'')
'''{'''
''statement(s)''
'''}'''
else
'''{'''
''statement(s)''
'''}'''
 
if (''boolean expression'')
'''{'''
''statement(s)''
'''}'''
else if (''boolean expression'')
'''{'''
''statement (s)''
'''}'''
...
else
'''{'''
''statement(s)''
'''}'''
 
switch (''integer expression'')
'''{'''
case ''constant integer expr'':
''statement(s)''
break;
...
default:
''statement(s)''
break;
'''}'''
 
=== Exception handling ===
In [[C Sharp (programming language)|C#]] and Java:
try
'''{'''
''statement(s)''
'''}'''
catch (exception type)
'''{'''
''statement(s)''
'''}'''
catch (exception type)
'''{'''
''statement(s)''
'''}'''
finally
'''{'''
''statement(s)''
'''}'''
 
[[Objective-C]] has the same syntax starting with gcc 3.3 and Apple Mac OS X 10.3 , but with an [[at sign]] in front of the keywords (<code>@try</code>, <code>@catch</code>, <code>@finally</code>).
 
C++ does not have <tt>finally</tt>, but otherwise looks similar. C has nothing like this, though some vendors have added the keywords <tt>__try</tt> and <tt>__finally</tt> to their [[compiler]]s.
 
== Typographical concerns ==
Some 7-bit national character sets [[ISO/IEC 646]] do not have curly bracket characters. To address this problem, BCPL has digraphs '''$(''' and '''$)''' for '''{''' and '''}''' and [[ANSI C]] introduced ''[[C trigraph|trigraph]]s'' that can be used instead of such problematic characters. All trigraphs consist of two [[question mark]]s followed by a character that is not redefined in the national 7 bit ASCII character sets. In C, the trigraphs for '''{''' and '''}''', respectively, are '''??<''' and '''??>'''.
 
== Languages ==
See [[:Category:Curly bracket programming languages]].
 
{{Programming language}}
 
[[Category:Curly bracket programming languages|*]]