In
computing science and
informatics, nesting
[https://study.com/academy/lesson/nesting-loops-statements-in-c-programming.html, title=Nesting Loops & Statements in C Programming] is where information is organized in layers, or where
objects contain other similar objects. It almost always refers to
self-similar or
recursive structures in some sense.
Terminology
Nesting can mean:
* nested calls:
** using several levels of
subroutine
In computer programming, a function or subroutine is a sequence of program instructions that performs a specific task, packaged as a unit. This unit can then be used in programs wherever that particular task should be performed.
Functions ma ...
s
**
recursive calls
* nested levels of parentheses in arithmetic expressions
* nested blocks of imperative
source code
In computing, source code, or simply code, is any collection of code, with or without comment (computer programming), comments, written using a human-readable programming language, usually as plain text. The source code of a Computer program, p ...
such as nested if-clauses, while-clauses, repeat-until clauses etc.
*
information hiding
In computer science, information hiding is the principle of segregation of the ''design decisions'' in a computer program that are most likely to change, thus protecting other parts of the program from extensive modification if the design decisio ...
:
**
nested function definitions with
lexical scope
In computer programming, the scope of a name binding (an association of a name to an entity, such as a variable) is the part of a program where the name binding is valid; that is, where the name can be used to refer to the entity. In other part ...
** nested data structures such as records, objects, classes, etc.
*
nested virtualization, also called recursive virtualization: running a
virtual machine
In computing, a virtual machine (VM) is the virtualization/ emulation of a computer system. Virtual machines are based on computer architectures and provide functionality of a physical computer. Their implementations may involve specialized har ...
inside another virtual machine
In spreadsheets
In a
spreadsheet
A spreadsheet is a computer application for computation, organization, analysis and storage of data in tabular form. Spreadsheets were developed as computerized analogs of paper accounting worksheets. The program operates on data entered in ce ...
functions can be ''nested'' one into another, making complex formulas. The function wizard of the
OpenOffice.org Calc application allows to navigate through multiple levels of nesting, letting the user to edit (and possibly correct) each one of them separately.
For example:
=IF(SUM(C8:G8)=0,"Y","N")
In this
Microsoft Excel
Microsoft Excel is a spreadsheet developed by Microsoft for Windows, macOS, Android and iOS. It features calculation or computation capabilities, graphing tools, pivot tables, and a macro programming language called Visual Basic for ...
formula, the SUM function is nested inside the IF function. First, the formula calculates the sum of the numbers in the cells from C8 to G8. It then decides whether the sum is 0, and it displays the letter Y if the sum is 0, and the letter N if it is not.
Naturally, to allow the mathematical resolution of these chained (or better: ''nested'') formulas, the inner expressions must be previously evaluated, and this outward direction is essential because the results that the ''internal'' functions return are temporarily used as entry data for the ''external'' ones.
Due to the potential accumulation of parentheses in only one code line, editing and error detecting (or
debugging
In computer programming and software development, debugging is the process of finding and resolving ''bugs'' (defects or problems that prevent correct operation) within computer programs, software, or systems.
Debugging tactics can involve in ...
) can become somehow ''awkward''. That is why modern programming environments -as well as spreadsheet programs- highlight in bold type the pair corresponding to the current editing position. The (automatic) balancing control of the opening and closing parenthesis is known as ''brace match checking''.
In programming
Control structures
In
structured programming
Structured programming is a programming paradigm aimed at improving the clarity, quality, and development time of a computer program by making extensive use of the structured control flow constructs of selection ( if/then/else) and repetition (w ...
languages, ''nesting'' is related to the ''enclosing'' of
control structures one into another, usually indicated through different ''indentation'' levels within the
source code
In computing, source code, or simply code, is any collection of code, with or without comment (computer programming), comments, written using a human-readable programming language, usually as plain text. The source code of a Computer program, p ...
, as it is shown in this simple
BASIC
BASIC (Beginners' All-purpose Symbolic Instruction Code) is a family of general-purpose, high-level programming languages designed for ease of use. The original version was created by John G. Kemeny and Thomas E. Kurtz at Dartmouth College ...
function:
function LookupCode(sCode as string) as integer
dim iReturnValue as integer
dim sLine, sPath as string
sPath="C:\Test.dsv"
if FileExists(sPath) then
open sPath for input as #1
do while not EOF(1)
line input #1, sLine
if sCode=left(sLine, 3) then
'Action(s) to be carried out
End if
loop
close #1
End if
LookupCode=iReturnValue
end function
In this small and simple example, the conditional block “if... then... end if” is nested inside the “do while... loop” one.
Some languages such as
Pascal and
Ada have no restrictions on declarations depending on the nesting level, allowing precisely nested subprograms or even nested packages (Ada). Here is an example of both (simplified from a real case):
-- Getting rid of the global variables issue (cannot be used in parallel)
-- from a set of old sources, without the need to change that code's
-- logic or structure.
--
procedure Nesting_example_1 is
type Buffer_type is array(Integer range <>) of Integer;
procedure Decompress(
compressed : in Buffer_type;
decompressed: out Buffer_type
)
is
-- Here are the legacy sources, translated:
package X_Globals is
index_in, index_out: Integer;
-- *** ^ These variables are local to Decompress.
-- *** Now Decompress is task-safe.
end X_Globals;
-- Methods 1,2,3,... (specifications)
package X_Method_1 is
procedure Decompress_1;
end X_Method_1;
-- Methods 1,2,3,... (code)
package body X_Method_1 is
use X_Globals;
procedure Decompress_1 is
begin
index_in:= compressed'First;
-- Here, the decompression code, method 1
end Decompress_1;
end X_Method_1;
-- End of the legacy sources
begin
X_Method_1.Decompress_1;
end Decompress;
test_in, test_out: Buffer_type(1..10_000);
begin
Decompress(test_in, test_out);
end Nesting_example_1;
Data structures
Nested
data structures are also commonly encountered in programming.
Lisp
In the
functional programming
In computer science, functional programming is a programming paradigm where programs are constructed by applying and composing functions. It is a declarative programming paradigm in which function definitions are trees of expressions that ...
languages, such as
Lisp
A lisp is a speech impairment in which a person misarticulates sibilants (, , , , , , , ). These misarticulations often result in unclear speech.
Types
* A frontal lisp occurs when the tongue is placed anterior to the target. Interdental lispi ...
, a
list
A ''list'' is any set of items in a row. List or lists may also refer to:
People
* List (surname)
Organizations
* List College, an undergraduate division of the Jewish Theological Seminary of America
* SC Germania List, German rugby uni ...
data structure exists as does a simpler
atom
Every atom is composed of a nucleus and one or more electrons bound to the nucleus. The nucleus is made of one or more protons and a number of neutrons. Only the most common variety of hydrogen has no neutrons.
Every solid, liquid, gas ...
data structure.
*Simple lists hold only atoms.
( A T O M S )
The atoms in the list are A, T, O, M, and S.
*Nested lists hold both atoms and other lists.
( ( ( N E S T E D ) L I S T S ) ( C A N ) ( B E ) U N N E C E S S A R I L Y ( C O M P L E X ) )
See also
*
Flow control
*
For loop
In computer science a for-loop or for loop is a control flow statement for specifying iteration. Specifically, a for loop functions by running a section of code repeatedly until a certain condition has been satisfied.
For-loops have two par ...
*
Pseudocode
In computer science, pseudocode is a plain language description of the steps in an algorithm or another system. Pseudocode often uses structural conventions of a normal programming language, but is intended for human reading rather than machine re ...
*
Structured programming
Structured programming is a programming paradigm aimed at improving the clarity, quality, and development time of a computer program by making extensive use of the structured control flow constructs of selection ( if/then/else) and repetition (w ...
References
{{reflist
Computer data
Computer programming