Statement block

Article on other languages:

del.icio.us del.icio.us
Digg Digg
Furl Furl
Reddit Reddit
Rojo Rojo
Add to OnlyWire

In computer programming, a statement block (or code block) is a section of code which is grouped together, much like a paragraph; such blocks consist of one, or more, statements. Statement blocks help make code more readable by breaking up programs into logical work units.

In C, C++, Java and some other languages, statement blocks are enclosed by curly braces {}. In Algol, Pascal, Ada, and some other languages, they are denoted by "begin" and "end" statements. In Python they are indicated by indentation (the Off-side rule). Unlike paragraphs, statement blocks can be nested; that is, with one block inside another. Blocks often define the scope of the identifiers used within.

Blocks become more independent when they can have their own variables, i.e. if identifiers defined inside a block cannot be referred to from outside that block. Languages without lexical scoping such as Javascript and Pico cannot support this in principle, but many languages with lexical scoping still don't support it for nested blocks (e.g., some dialects of C, C#), while others do (e.g. Algol 68, other dialects of C, VB.NET).

In C++, blocks can be used to define object lifetime (creation and destruction). In languages such as Smalltalk, blocks are objects in their own right, extended with a reference to their environment of definition, i.e. closures.

Contents

A typical statement block

int main()
{
  return 0;
}

A nested statement block

int main()
{
  int x = 1;
 
  if (x == 1)
  {
     x++;
  }
  return 0;
}

Other formats

Java programmers typically use a slightly different convention for placing the braces. The opening brace is on the same line as the method declaration:

int main() {
  return 0;
}
 
int main() {
 
  int x=1;
 
  if (x == 1) {
     x++;
  }
 
  return 0;
}

Visual Basic requires an explicit End statement, as follows:

If x > 0 Then
 
  y = y + x
 
End If
 
For i = 1 To 10
 
  DoSomething(i)
 
Next ' or Next i

SQL Server and some other languages (e.g. Pascal) use Begin ... End blocks

IF y IS NOT NULL
BEGIN
  SELECT * FROM employee WHERE name = y
END

See also

This article is from Wikipedia. All text is available under the terms of the GNU Free Documentation License.


Giant Panda

Mercedes Car
James Bond Guide
This site monitored by SitePinger.net