Control structures In
structured programming languages,
nesting is related to the
enclosing of
control structures one into another, usually indicated through different
indentation levels within the
source code, as it is shown in this simple
BASIC 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 languages, such as
Lisp, a
list data structure exists as does a simpler
atom 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==