Curly bracket programming language

From Wikipedia, the free encyclopedia

Curly brace or bracket programming languages are those which use balanced brackets ({ and }, also known as "brace brackets" or simply "braces") to make blocks in their syntax or formal grammar, mainly due to being C-influenced.

Contents

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 his B programming language. Because C was initially designed after B, it has retained the bracket syntax of B, as have many subsequent languages (C++, Java, JavaScript and its generalized standard ECMAScript, C#, D, etc.). 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 Pico indent style is then used, as below (BCPL):

LET FUNC foo(a) = VALOF
{ b := a + 1
  RESULTIS b }

The name derives from the common syntax of the languages, where blocks of statements are enclosed in curly brackets. For example (using BSD/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 nearly impossible for a person 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, Pascal, REXX, and Visual Basic), the Off-side rule of indentation (in Python), or other symbols such as parentheses (in Lisp). These ways are not necessarily exclusive: whereas indentation is the default in Haskell, curly brackets can be used when desired.

In C, C++, C#, and Java:

while (boolean expression)
{
    statement(s)
}
do
{
    statement(s)
} while (boolean expression);
for (initialisation; termination condition; incrementing expr)
{
    statement(s)
}

In C, C++, C#, 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;
}

In 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 (@try, @catch, @finally).

C++ does not have finally, but otherwise looks similar. C has nothing like this, though some vendors have added the keywords __try and __finally to their compilers.

Some 7 bit national ISO646 sets redefine curly brackets to characters that make programs hardly readable on such designed terminals. To address this problem, ANSI C introduced trigraphs that can be used instead of such problematic characters. All trigraphs consist of two question marks (“??”) followed by a character that is not redefined in the national 7 bit ASCII character sets. The trigraphs for ‘{’ and ‘}’, respectively, are “??<” and “??>”.

Advanced Search
Included Web Search Engines


Safe Search

close

Top Matching Results

Occasionally Search.com will highlight specialized results that are based on the context of your query. Examples of specialized results include specific links to news, images, or video.

Top Matching Results may highlight information from other Search.com pages, content from the CNET Network of sites, or third party content. The listings are based purely on relevance. Search.com does not receive payment for listings in this section but our partners that provide this data may get paid for listing these products.

Sponsored Links

This section contains paid listings which have been purchased by companies that want to have their sites appear for specific search terms and related content. These listings are administered, sorted and maintained by a third party and are not endorsed by Search.com.

Search Results

Search.com sends your search query to several search engines at one time and integrates the results into one list which has been sorted by relevance using Search.com's proprietary algorithm. You can customize the list of search engines included in your metasearch from the preferences.

The search engines that are used in your metasearch may allow companies to pay to have their Web sites included within the results. To view the Paid Inclusion policy for a specific search engine, please visit their Web site. Search.com does not accept payment or share revenue with any search engine partner for listings in this section.