Jump to content

Curly bracket programming language: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
DangerousPanda (talk | contribs)
Line 1: Line 1:
#REDIRECT [[List of programming languages by category#Curly-bracket languages ]]
<!-- Please do not remove or change this AfD message until the issue is settled -->
{{AfDM|page=Curly bracket programming language|year=2010|month=May|day=5|substed=yes|help=off}}
<!-- For administrator use only: {{oldafdmulti|page=Curly bracket programming language|date=5 May 2010|result='''keep'''}} -->
<!-- End of AfD message, feel free to edit beyond this point -->
{{unencyclopedic}}
{{Unreferenced|date=April 2007}}
{{example farm}}

'''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 delimit [[block (programming)|block]]s in their [[syntax]] or [[formal grammar]], mainly due to being influenced by the [[C programming language]]. Thus, languages in this family are sometimes referred to as '''C-style'''. Besides the use of curly brackets, they generally inherit other syntax features from C, such as using the semicolon as a statement terminator (not as a separator), ignoring runs of [[whitespace]] for syntactical purposes (termed "free-form style"), and often using a terse style with relatively few [[reserved word]]s and special operators.

== History ==
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 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);
'''}'''

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.

Another popular way to work with curly braces is with the [[Indent style#K&R style|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]] and [[Occam (programming language)|Occam]]), 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)''
'''}'''

Java and [[C++0x]] also support a [[foreach]]-style loop:

for (''type'' ''item'' : ''container'')
'''{'''
''statement(s)''
'''}'''

C-sharp uses the following syntax instead:

foreach (''type'' ''item'' in ''container'')
'''{'''
''statement(s)''
'''}'''

Perl uses the following syntax:
foreach ''item'' (''container'')
'''{'''
''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#]], D and Java:
try
'''{'''
''statement(s)''
'''}'''
catch (exception_type execption_variable)
'''{'''
''statement(s)''
'''}'''
catch (exception_type execption_variable)
'''{'''
''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.

=== Other constructs ===
The special statement <code>break;</code> may be used to exit early from a loop, and <code>continue;</code> to skip to the next iteration. Labels may be declared by <code>''label'': ''statement'';</code> and the syntax <code>goto ''label'';</code> is used for the [[goto statement]]. Java only supports labels denoting entire blocks, with the special syntax <code>break ''label'';</code> indicating escape from the labeled loop.

== Comments ==
By convention, most programming languages in this class use <code>/*</code> and <code>*/</code> as delimiters in block comments. and <code>//</code> to indicate single-line comments. Versions of C prior to C99 did not support <code>//</code> comments, but this feature was provided as an extension by most compilers.

== Variable declaration ==
Many C-style programming languages use [[static typing]] and require all variables to have an explicit type, even though the guarantees provided by the type systems are quite variable. Typing is generally [[nominative typing|nominative]], not structural. Sometimes [[type inference]] is supported in limited contexts; if so, it is denoted by special keywords such as <code>auto</code> or <code>var</code>.

Basic types are usually denoted by simple lowercase words, such as <code>int</code> or <code>char</code>, optionally decorated with modifiers such as <code>unsigned</code>. In addition, declarations may also be marked by various type and storage modifiers: for instance, a constant variable may be indicated by the modifier <code>const</code>.

Variables are declared with a syntax which is similar to their use. In a basic declaration, the type is given first, followed by the name of the variable and an optional initial value. Multiple variables may be separated by a comma:

<source lang="c">
unsigned char red, blue;
int green = 0;
</source>

More complex types, such as pointers and arrays, are declared by means of other modifiers. <code>*</code> and <code>[]</code> are the modifiers for pointers and arrays in many C-style programming languages. A confusing feature of C syntax is that these modifiers are affixed to the variable being declared rather than the basic type, reflecting the use of <code>*</code> and <code>[]</code>. as dereferencing operators:

<source lang="c">
char *foo; //foo is a pointer to chars
int bar[10]; //bar is an array of ints
char **baz; //baz is a pointer to a char-pointer
</source>

However, various C-like languages, including [[Java (programming language)|Java]] and C#, may separate their declarations into a type followed by a list of variable names:

<source lang="c">
int[10] bar; // not valid C: bar is an array of ints
</source>

=== User-defined types ===
Type synonyms may be declared by using a syntax such as <code>typedef ''type'' ''synonym''</code> or <code>using ''synonym'' = ''type''</code>.

Simple composite types are declared by such syntaxes as <code>struct { ''type'' ''name''; ''type'' ''name''; ... };</code>, where ''struct'' denotes a [[record type]]. Some languages also support [[union type]]s, denoted by the <code>union</code> keyword.

== Functions ==
Functions are defined by the special syntax <code>''return_type'' ''funcname''(''params'') { ''statement''; ''statement''; ... }</code>. Return to the calling function is denoted by the <code>return ''value'';</code> statement. Functions which do not return any value are denoted by the special return type <code>void</code>.

==Other syntax==
Square brackets (<code>[]</code>) are used for indexing into an array, as in: <code>''name''[''index'']</code>. (In most cases, the base index for arrays is 0, not 1).

In some languages, support for [[associative array]]s (also known as dictionaries) is implemented by the same syntax. For instance, in [[C++]]:

<source lang="cpp">
std::map<std::string, std::string> dict;

dict["foo"] = "bar";
dict["fred"] = "barney";

std::cout << dict["fred"] << std::endl;
</source>

The dot operator (<code>''object''.''field''</code>) is used for accessing fields of a composite type (a struct or a union) or invoking methods associated with an object. In C, structures are commonly accessed through pointers, and the arrow operator (<code>''struct_ptr''->''field''</code>) is provided for this purpose.

C and C++ provide a variety of operators (see [[Operators in C and C++]]), some of which may be supported with similar syntax in other C-style languages.

== 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 '''??>'''.

==See also ==

;Curly-bracket languages
* [[List of programming languages by category#Curly-bracket languages|List of curly-bracket programming languages]]

;Non-curly-bracket languages
Most non-curly-bracket languages derive from languages that predate [[BCPL]]:

* [[COBOL]]
* [[Fortran]]
* [[Lisp (programming language)|Lisp]]

{{Programming language}}

[[Category:Programming languages]]

[[it:Curly bracket]]

Revision as of 11:07, 13 May 2010