ArticleslgStudy

science

Block (programming)

Block (programming) is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Block (programming) rather than just read about it. In short: In computer programming, a block of source code is a grouping of statements that execute in sequential order, top to bottom. The block structure is fundamental to structured programming, where control structures are formed with blocks.

Key takeaways

  • Block (programming) belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Block (programming) to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Block (programming) from memory before moving on to harder problems.

Reference excerpt

In computer programming, a block of source code is a grouping of statements that execute in sequential order, top to bottom. The block structure is fundamental to structured programming, where control structures are formed with blocks. Syntactically, a block acts much like a single statement in that a control structure generally operates either on a single statement or a block. The nature of a block varies by programming language, but there are typical features. A block usually provides for visibility scope such that an identifier defined in a block is not visible in outer, containing blocks, but is visible in inner, nested blocks unless it is masked by an identifier with the same name. A block usually provides for lifetime scope such that resources associated with an identifier are partially or fully released when control flow reaches the end of the block in which the identifier was defined.

History The block concept was developed in the 1950s during the development of the first autocodes, and was formalized in the Algol 58 and Algol 60 reports. Algol 58 introduced the notion of the "compound statement", which was related solely to control flow. The subsequent Revised Report which described the syntax and semantics of Algol 60 introduced the notion of a block and block scope, with a block consisting of " A sequence of declarations followed by a sequence of statements and enclosed between begin and end..." in which "[e]very declaration appears in a block in this way and is valid only for that block."

Syntax A block is demarcated in code as a separate unit, but the syntax varies by language. Notable examples include:

Curly braces C and many related languages delimit a block with { and }. In fact, this is the defining quality of a curly brace language. Indentation Python and Haskell use indentation to demarcate a block instead of delimiters as most languages do. Parentheses The batch file syntax uses ( and ). begin/end The ALGOL family generally delimits a block with the keywords begin and end; ALGOL 68 uses parentheses. Control verb reversed With ALGOL 68, then in Edsger W. Dijkstra's 1974 Guarded Command Language the conditional and iterative code block are alternatively terminated with the block reserved word reversed: e.g. if ~ then ~ elif ~ else ~ fi, case ~ in ~ out ~ esac and for ~ while ~ do ~ od. S-expression With a syntactic keyword such as prog or let as in the Lisp family.

Limitations Some languages which support declarations in a block do not support all declarations. For instance, many C-related languages do not permit a function definition within a block. And unlike its ancestor Algol, Pascal does not support the use of blocks with their own declarations inside the begin and end of an existing block, only compound statements enabling sequences of statements to be grouped together in if, while, repeat and other control statements.

Examples In the early days of computing, many languages such as FORTRAN IV and BASIC lacked block syntax except sometimes for rudimentary loop constructs. A conditional was encoded using the goto statement. In the following FORTRAN 66 code, the logical structure is less than clear due to the lack of blocks.

Blocks allow the programmer to treat a group of statements as a unit, and the default values which had to appear in initialization in this style of programming can, with a block structure, be placed closer to the decision. The following code in Jensen and Wirth Pascal shows that block structure makes it easier to see how the code could be refactored for clarity, and also makes it easier to do, because the structure of the inner conditional can easily be moved out of the outer conditional altogether and the effects of doing so are easily predicted. Use of blocks in the fragment below clarifies the programmer's intent, and enables combining the resulting blocks into a nested hierarchy of conditional statements. The structure of the code reflects the programmer's thinking more closely, making it easier to understand and modify. The code can be made even clearer by taking the inner if statement out of the outer one altogether, placing the two blocks one after the other to be executed consecutively. Semantically, there is little difference in this case, and the use of block structure, supported by indenting for readability, makes it easy for the programmer to refactor the code.

In primitive languages, variables had broad scope. For instance, an integer variable called IEMPNO might be used in one part of a Fortran subroutine to denote an employee social security number (SSN), but during maintenance work on the same subroutine, a programmer might accidentally use the same variable, IEMPNO, for a different purpose, and this could result in a bug that was difficult to trace. Block structure makes it easier for programmers to control scope. In the following R5RS Standard Scheme fragment, empno is used to identify both the manager and their underlings each by their respective SSN, but because the underling SSN is declared within an inner block it does not interact with the variable of the same name that contains the manager's SSN. In practice, considerations of clarity would probably lead the programmer to choose distinct variable names, but they have the choice and it is more difficult to introduce a bug inadvertently. Within the lambda expression, the variable empno refers to the SSN of an underling. The variable empno in the outer expression, referring to the manager's SSN, is shadowed.

Hoisting In some languages, a variable can be declared at function scope even within enclosed blocks. For example, in JavaScript, variables declared with var have function scope.

See also

Basic block Block scope Closure (computer programming) Decomposition (computer science) Early exit

References

Worked examples

Example 1 — a first encounter with Block (programming)

Start with the simplest possible case. Write down what Block (programming) claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Block (programming) before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Block (programming) ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Block (programming)

In research
Block (programming) appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Block (programming) in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Block (programming) is common in secondary-school and first-year university syllabi. It links to neighbouring topics Programming constructs, so understanding it makes those chapters shorter.
In everyday life
Look for Block (programming) outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Block (programming)” →

How to study Block (programming) in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Block (programming) means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Block (programming) out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Block (programming) in simple terms?

In computer programming, a block of source code is a grouping of statements that execute in sequential order, top to bottom. The block structure is fundamental to structured programming, where control structures are formed with blocks.

Why does Block (programming) matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Block (programming)?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Block (programming).

Tags

  • Programming constructs

Keep exploring