Curly bracket programming language

From Seo Wiki - Search Engine Optimization and Programming Languages
Jump to navigationJump to search

Curly brace or bracket programming languages are those which use balanced brackets ({ and }), also known as "squiggly brackets", "brace brackets" or simply "braces") to make blocks in their syntax or formal grammar, mainly due to being C-influenced. The main alternate style is the use of paired keywords, although some languages (notably Python and Occam) instead use an off-side style, and Lisp uses parentheses.

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);
}

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, 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.

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)
{
    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.

Other constructs

The special statement break; may be used to exit early from a loop, and continue; to skip to the next iteration. Labels may be declared by label: statement; and the syntax goto label; is used for the goto statement. Java only supports labels denoting entire blocks, with the special syntax break label; 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 auto or var.

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

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:

unsigned char red, blue;
int green = 0;

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:

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

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

int[10] bar; // not valid C: bar is an array of ints

User-defined types

Type synonyms may be declared by using a syntax such as typedef type synonym or using synonym = type.

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

Functions

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

Other syntax

Square brackets ([]) are used for indexing into an array, as in: name[index]. (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++:

std::map<std::string, std::string> dict;

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

std::cout << dict["fred"] << std::endl;

The dot operator (object.field) 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 (struct_ptr->field) 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 ??>.

See also

Curly-bracket languages
Non-curly-bracket languages

Most non-curly-bracket languages derive from languages that predate BCPL:

If you like SEOmastering Site, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...