User:PHenry/Rescue Squad/Curly bracket programming language

Curly brace or bracket programming languages are those which use balanced brackets ({ and }), also known as "squiggly brackets", "brace brackets" or simply "braces", to delimit blocks 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 words 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, his cut-down version of BCPL. 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, PHP, etc.). 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 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 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); }

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 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 and Occam), 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.

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#, D and Java: try {    statement(s) } catch (exception_type exception_variable) {    statement(s) } catch (exception_type exception_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.

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.

Other constructs
The special statement  may be used to exit early from a loop, and   to skip to the next iteration. Labels may be declared by  and the syntax   is used for the goto statement. Java only supports labels denoting entire blocks, with the special syntax  indicating escape from the labeled loop.

Comments
By convention, most programming languages in this class use  and   as delimiters in block comments. and  to indicate single-line comments. Versions of C prior to C99 did not support  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, not structural. Sometimes type inference is supported in limited contexts; if so, it is denoted by special keywords such as  or.

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

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:

More complex types, such as pointers and arrays, are declared by means of other modifiers. and  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  and. as dereferencing operators:

However, various C-like languages, including Java and C#, may separate their declarations into a type followed by a list of variable names:

User-defined types
Type synonyms may be declared by using a syntax such as  or.

Simple composite types are declared by such syntaxes as, where struct denotes a record type. Some languages also support union types, denoted by the  keyword.

Functions
Functions are defined by the special syntax. Return to the calling function is denoted by the  statement. Functions which do not return any value are denoted by the special return type.

Other syntax
Square brackets are used for indexing into an array, as in:. (In most cases, the base index for arrays is 0, not 1).

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

The dot operator 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 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 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. In C, the trigraphs for { and }, respectively, are ??< and ??>.