Statement block

From Wikipedia, the free encyclopedia

(Redirected from Block (programming))
Jump to: navigation, search

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 braces {}. In Ada, Pascal, and some other languages, blocks 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 often have subtle but important differences in semantics. In languages in the C tradition, they define identifier scope. In C++ they can be used to define object lifetime (creation and destruction). In some languages (such as Pico) they are merely used for grouping expressions without notions of variable scope. 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

int main()
{
  return 0;
}

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

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 use Begin ... End blocks

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

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.