0% found this document useful (0 votes)
274 views33 pages

Understanding Sequence Control Structures

Uploaded by

kunal kalra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
274 views33 pages

Understanding Sequence Control Structures

Uploaded by

kunal kalra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PROGRAMMING LANGUAGE

UNIT -3

Sequence control: It is the order in which the statements,operations or


expressions of a program are executed. It is also called Control Structure.
There are three basic types of sequence control: Sequential, Conditional, and
Iterative.
[Link] control means that the statements are executed one after
another in the order they appear in the program.
(also called sequence control).
[Link] control means that the execution of some statements depends
on the evaluation of a condition, such as an if-else or switch statement.
[Link] control means that some statements are repeated a number of
times, such as a for, while, or do-while loop.
WHY WE NEED CONTROL STRUCTURE? (ADVANTAGES)
Logic and Decision Making: They enable programmers to implement logic and
make decisions within the program, determining which code paths to follow
based on different conditions.
Iteration and Repetition: Control structures like loops facilitate the execution
of a set of statements multiple times, reducing redundancy in code and making
it more efficient.
Program Flow: They help control the order of execution, ensuring that
statements are processed in a meaningful and organized way, contributing to
the overall functionality of the program.
Data Control
Data control is defined as the control of the transmission of data for each
operation of a program. The data control features of a programming language
are concerned with the following aspects −
The approachability of information at several points during program execution.
The determination of how data can be supported for each operation, and how
a result of one operation can be stored and fetched for later use as an operand
by a subsequent operation.
DATA CONTROL VS SEQUENCE CONTROL
DATA CONTROL SEQUENCE CONTROL
The control of the communication The control of the procedure
of data among the subprograms of the execution of
of a program is defined as data operations, both primitive
control. and user-defined is defined
as sequence control.

Data control is ruled by the Sequence control is ruled by


dynamic and static scope rules notations in expressions and
for an identifier. the hierarchy of operations.
A data object can be made Sequence control structures
available through two methods can be either explicit or
such as −[Link] Transmission implicit.
[Link] through Reference.

Data control structures may be Sequence control structures


categorized according to the can be conventionally
referencing environment of data categorized into three
groups such as −

 Structures used in expressions.


 Structures used between
statements and
 Structures used between
subprograms.

Data control is concerned with Sequence control is


the binding of identifiers to concerned with decoding the
specific data objects and instruction and expressions
subprograms. into executable form.
Classification of Sequence Control : Sequence control structures are
categorized into following four groups:

1. Expressions : These form the basic building blocks for statements and
express how at are manipulated and changed by a program. Properties such as
precedence rules and parentheses determine how expressions become
evaluated.
2. Statement : Statements such as conditional or iterative statements,
determine how control flows from one segment of a program to another.
3. Declarative programming : It is an execution of programming model which
is independent of program statements.(Logic programming model of PROLOG)
4. Subprograms : Subprograms such as subprogram calls and coroutines, form
a way to transfer control from one segment of a program to another.

IMPLICIT AND EXPLICIT SEQUENCE CONTROL


Sequence Control Structures may be either Implicit or Explicit.
IMPLICIT SEQUENCE CONTROL
1.) It refers to the default flow of execution where statements are executed
one after the other in the order they appear in the code.
(Also called Default Sequence Control).
2.) The order of execution of the statements or expressions in a program is
determined by the compiler or the runtime system without any explicit
specification by the programmer.
3.) The program follows a linear path from the beginning to the end, and
each statement is executed in sequence without any special control flow
instructions which is easy to understand.
Basic Example of Implicit Sequence Control:
In this Python code snippet, the program follows a sequential order of
execution. First, the variable x is assigned the value 5, then the variable y is
assigned the value 10, and finally, the sum of x and y is calculated and
printed. The sequence control is implicit, as there are no control flow
statements like conditionals or loops that alter the natural order of
execution.
NOTE Implicit sequence control can occur in situations such as type
conversion, function overloading, operator overloading, exception
handling, etc¹. For example, the following code fragment uses implicit
sequence control to convert a double value to an int value:
CPP CODE
double x = 1.2;
int y = x; // implicit conversion
In this case, the compiler automatically converts the value of x from double
to int and assigns it to y and execute implicitly.

Advantages of Implicit Sequence Control:


1. Readability: Implicit sequence control makes the code more readable and
intuitive, as statements are executed in the order they are written.
2. Simplicity: The linear flow of execution is simple to understand, especially
for small programs with straightforward logic.
3. Predictability: With implicit sequence control, developers can easily
predict the order of execution, making it easier to troubleshoot and debug
issues.
4. Ease of Maintenance: Since the code follows a linear flow, it is relatively
easy to maintain and update.
Limitations of Implicit Sequence Control:
1. Lack of Flexibility: Implicit sequence control does not allow for branching
or repetitive actions, which limits the complexity of logic that can be
expressed.
2. Repetition of Code: In scenarios where the same sequence of code needs
to be executed multiple times, developers have to explicitly duplicate the
code, leading to code redundancy.
3. Limited Decision Making: Implicit sequence control lacks the ability to
make decisions based on conditions, making it less suitable for scenarios
where conditional branching is required.

EXPLICIT SEQUENCE CONTROL


1.) Explicit sequence control is a powerful approach in programming that
allows developers to customize the order of statement execution using
control flow statements
2.) Unlike the default linear flow of implicit sequence control, explicit
sequence control introduces conditional branching, looping, and jumping,
enabling the program to make decisions, repeat actions, and alter the
program flow based on specific conditions.
3.) Flow of statements has to set by the programmer before the run time
or compile time.
EXAMPLE
In this Python code the natural flow i.e. the linear flow is disturbed if age
input is less than 18 , it is done by using if –else statement by
programmer.

Advantages of Explicit Sequence Control:


1. Customized Logic: Explicit sequence control allows developers to
implement custom logic and decision-making in their programs, catering to
various real-world scenarios.
2. Code Reusability: By using control flow statements like loops, developers
can easily repeat code execution, avoiding code duplication and promoting
code reusability.
3. Dynamic Behavior: The ability to make decisions and change program flow
dynamically makes explicit sequence control well-suited for handling
changing data and input conditions.
4. Complexity Management: Explicit sequence control allows developers to
handle complex logic and improve code organization by structuring control
flow based on specific requirements.

Challenges of Explicit Sequence Control:


1. Readability Concerns: Excessive use of control flow statements can make
code harder to read and maintain, leading to potential issues with code
comprehension.
2. Potential Bugs: With more control flow statements, there is an increased
risk of introducing bugs and logic errors, which may be harder to debug.
3. Code Duplication: While control flow statements can promote code
reusability, improper use may lead to code duplication, affecting
maintainability and readability.
SEQUENCE CONTROL WITHIN EXPRESSION
Sequence control within expressions refers to the order in which operators
are evaluated in an expression. The order of evaluation is important because
it can affect the result of the expression. For example, in the following
expression, the multiplication operator will be evaluated before the addition
operator:
int x = 1 + 2 * 3;
This is because multiplication and division are evaluated before addition and
subtraction. As a result, the value of x will be 7.
EVALUATION OF SEQUENCE CONTROL OF EXPRESSION DEPENDS UPON
1.) TYPE OF EXPRESSION
2.) PRECEDENCE AND ASSOCIATIVITY
3.) NOTATION OF EXPRESSION

1.) TYPE OF EXPRESSION


An expression is a combination of operands and operators that
evaluates to a single value. In most programming languages, there are
three types of expressions:
*Arithmetic expressions: These expressions consist of some numeric value with
arithmetic operators. Example : a+b*c
*Relational expressions: A relational expression that compares two values to
determine the relationship between them. These expressions typically involve
relational operators, such as equality (==), inequality (!=), greater than (>), less
than (<), greater than or equal to (>=), and less than or equal to (<=) gives
Boolean result.
Example : c >= d

*Logical expressions: Logical expressions are expressions that use operators


such as &&, ||, !, etc. to combine two or more relational expressions and
produce a boolean [Link]: (x < y) && (y > z)
2.) PRECEDENCE AND ASSOCIATIVITY

EXAMPLE

Let's evaluate the logical expression (3 < 6) && (2 > 5) step by step:

1. Evaluate 3 < 6: This is true because 3 is less than 6.


2. Evaluate 2 > 5: This is false because 2 is not greater than 5.

Now, combine the results using the logical AND (&&) operator:

 true && false evaluates to false.

Therefore, the overall result of the expression (3 < 6) && (2 > 5) is false.
NOTATIONS OF EXPRESSION

Notation of any expression can be classified into three type –

1. Infix Notation:
 In infix notation, operators are placed between their operands.
 This is the most common way expressions are written, such as in
everyday mathematical notation.
 Examples: a*b+(c-d)

2. Prefix Notation (or Polish Notation):


 In prefix notation, the operator comes before its operands.
 It is also known as Polish notation.
 Examples: *a+b-cd

3. Postfix Notation (or Reverse Polish Notation):


 In postfix notation, the operator comes after its operands.
 It is also known as Reverse Polish notation.
 Examples: ab+cd−*

NOTE Prefix and Postfix expressions can be evaluated faster than an infix
expression. This is because we don’t need to process any brackets or follow
operator precedence rule.

SEQUENCE CONTROL WITH IN STATEMENTS


Sequence control of statements is the order in which the statements in a
program are executed. It can be implicit or explicit, depending on the
programming language and the programmer's choice. Here are some examples
of different types of statements that can affect the sequence control of a
program:

1.*Assignment statements* assign a value to a variable or a constant. They are


usually executed in the order they appear in the program, unless modified by
other statements. For example C code

2.*Conditional statements* execute a block of code based on a condition.


They can alter the sequence control of a program by skipping or branching to
different parts of the code. For example C code
3. *Iterative statements* execute a block of code repeatedly until a condition
is met. They can alter the sequence control of a program by looping through
the same part of the code multiple times. For example: C code

4. *Jump statements* transfer the control of the program to another part of


the code. They can alter the sequence control of a program by breaking out of
a loop, returning from a function, or going to a specific label. For example C
code
5.*Function calls* invoke a subprogram that performs a specific task. They can
alter the sequence control of a program by passing the control to another part
of the code and returning it back when the function is done.
For example Ccode
SUBPROGRAM SEQUENCE CONTROL
A Subprogram is a set of statements that can be reused at multiple places in a
program when convenient.
Subprograms can be of two types: Procedures and Functions.
Procedures are subprograms that define parameterized computations and are
executed by a call statement.
Functions are subprograms that evaluate a value and are semantically
modeled on mathematical functions.
KEY POINTS
1.) Subprogram sequence control is the control of the order of
execution of the operations and the transmission of data among
subprograms of a program.
2.) Subprogram sequence control can be achieved by using expressions,
statements, and subprogram calls.
3.) Expressions are formulas that use operators and operands to
compute a value.
4.) Statements are instructions that determine how control flows from
one part of the program to another. (Recursive subprograms)
5.) Subprogram calls are expressions that invoke a subprogram and
pass arguments to it. (Simple call return)

SIMPLE CALL RETURN


1) Simple call return is a subprogram control structure that
involves a single main program that calls various
subprograms, which may call other subprograms and so on.
2) Each subprogram returns the control to the program or
subprogram that called it after execution.
3) The execution of the calling program is temporarily stopped
during the execution of the subprogram
4) After the subprogram is completed, execution of the calling
program resumes at the point immediately following the
call
#include <iostream>

int main() {
// Declare a function
int add(int x, int y) {
return x + y;
}

// Call the function


int sum = add(2, 3);

// Print the result


std::cout << sum << std::endl;

return 0;
}

This program will print the number 5, which is the sum of 2


and 3 using simple call return method.

RECURSIVE SUBPROGRAMS
1 A recursive subprogram is a subprogram that calls itself.
2 It is a method of solving a problem by breaking it down
into smaller problems of the same type, and then solving
the smaller problems using the same method.
3 Process is repeated until the smallest problems are
solved, at which point the solution to the original problem
can be constructed from the solutions to the smaller
problems.
4 Recursion can substitute iteration in program design and
can simplify some problems, such as factorial, Fibonacci,
and tree traversal.
5 Recursive subprogram mainly cosist of a Base case defines
the stopping condition for a recursive function. It's the
case for which the solution can stated non recursively. A
base case is a known small value for which the result is
returned.A Recursive case computes a result by calling the
same function on a smaller value. A recursive case is the
case for which the solution is expressed in terms of a
smaller version of itself.
EXCEPTION AND EXCEPTION HANDLERS
Errors are mistakes or problems that occur when a program is executed. There are
different types of errors that can happen in programming, such as syntax errors,
logical errors, runtime errors, compilation errors, and time limit exceeded errors¹.
Here are some examples of each type of error:

1. Syntax errors: These are errors that occur when the code violates the rules or
grammar of the programming language, such as missing semicolons, brackets, or
indentation. For example, in C++, the following code will cause a syntax error
because of the missing semicolon after the cout statement:

cpp
#include <iostream>
using namespace std;

int main() {
cout << "Hello World" // missing semicolon
return 0;
}

2. Logical errors: These are errors that occur when the code has incorrect logic or
algorithm and produces unexpected or wrong results. For example, in C++, the following
code will cause a logical error because the condition in the if statement is wrong and will
always print "Odd" regardless of the value of n:

cpp
#include <iostream>
using namespace std;

int main() {
int n = 10;
if (n % 2 != 0 || n == 0) { // wrong condition
cout << "Odd" << endl;
}
else {
cout << "Even" << endl;
}
return 0;
}

3. Runtime errors: These are errors that occur during the execution of the program and
cause the program to terminate abnormally. These errors are often caused by invalid
input, memory allocation, division by zero, etc. For example, in C++, the following
code will cause a runtime error because of the division by zero:

cpp
#include <iostream>
using namespace std;

int main() {
int a = 10, b = 0;
int c = a / b; // division by zero
cout << c << endl;
return 0;
}

4. Compilation errors: These are errors that occur during the compilation of the
program and prevent the program from generating an executable file. These errors
are often caused by missing header files, undefined variables, incompatible data
types, etc. For example, in C++, the following code will cause a compilation error
because of the missing header file for the string class:

cpp
// missing #include <string>
using namespace std;

int main() {
string s = "Hello"; // string is not defined
cout << s << endl;
return 0;
}

5. Time limit exceeded errors: These are errors that occur when the program takes
too long to execute and exceeds the given time limit in a coding contest or online
judge. These errors are often caused by inefficient algorithms, infinite loops,
excessive input/output, etc. For example, in C++, the following code will cause a time
limit exceeded error because of the infinite loop:

cpp
#include <iostream>
using namespace std;

int main() {
int n = 1;
while (n > 0) { // infinite loop
cout << n << endl;
n++;
}
return 0;
}

Exception in programming language: An exception is an event or condition that


occurs during the execution of a program and disrupts the normal flow of the
program. Exceptions are often used to handle runtime errors or abnormal situations
that may arise in the program. For example, in C++, an exception can be thrown
when a file is not found, a memory allocation fails, an array index is out of bounds,
etc.

Types of exception in programming with example of each: Exceptions can be


categorized into different types depending on the source, nature, or severity of the
exception. Some common types of exceptions are:
* Built-in exceptions: These are exceptions that are predefined and provided by
the programming language or its standard libraries. These exceptions are suitable to
handle common error situations that may occur in the program. For example, in C++,
some built-in exceptions are:

- std::bad_alloc: This exception is thrown when a memory allocation fails, such


as when using the new operator.
- std::out_of_range: This exception is thrown when an invalid index is used to
access an element of a container, such as a vector or a string.
- std::invalid_argument: This exception is thrown when an invalid argument is
passed to a function, such as when using the stoi function to convert a string to an
integer.

* User-defined exceptions: These are exceptions that are defined and created by
the programmer to handle specific or custom error situations that may occur in the
program. These exceptions are often derived from the built-in exception classes or
the base class std::exception. For example, in C++, a user-defined exception can be:

- class DivideByZeroException: public std::exception {


public:
const char* what() const noexcept override {
return "Division by zero";
}
};

This exception can be used to handle the case when a division by zero occurs in
the program.

* Checked and unchecked exceptions: These are exceptions that are checked or
not checked by the compiler at compile time. Checked exceptions are those that
must be either handled or declared by the programmer using the try-catch blocks or
the throws keyword. Unchecked exceptions are those that are not checked by the
compiler and do not need to be handled or declared explicitly. For example, in Java,
IOException is a checked exception and NullPointerException is an unchecked
exception.

-
Exception handlers and way to deal with exception handling in C++: Exception
handlers are blocks of code that are used to handle or catch the exceptions that are
thrown by the program. The way to deal with exception handling in C++ is to use the
try, catch, and throw keywords. The syntax is:

cpp
try {
// code that may throw an exception
throw exception; // optional
}
catch (exception_type exception) {
// code to handle the exception
}
The try block contains the code that may throw an exception. The catch block
contains the code that handles the exception that is thrown by the try block or by
the throw keyword. The throw keyword is used to throw an exception manually
when a problem is detected. The exception_type can be any built-in or user-defined
exception class. There can be multiple catch blocks for different types of exceptions.
For example, in C++, a simple exception handling program can be: cpp
COROUTINE SEQUENCE CONTROL
A coroutine is a function that can suspend its execution and resume it later, without losing
its state or context. Coroutines are generalizations of subroutines, which are functions that
have a single entry point and exit point. Coroutines can have multiple entry points and exit
points, and can cooperate with other coroutines by transferring control among them¹².
Some properties of coroutines are:
* Coroutines are stackless, meaning they do not use the call stack to store their local
variables and return addresses. Instead, they use a separate data structure, such as a heap-
allocated object, to store their state and context³.
* Coroutines are user-level constructs, meaning they are not managed by the operating
system or the runtime environment. They are created and controlled by the programmer
and the programming language⁴.
* Coroutines are lightweight, meaning they consume less resources than processes or
threads. They can be created and destroyed quickly, and can run many instances
concurrently without much overhead⁵.
* Coroutines are cooperative, meaning they voluntarily yield control to other coroutines
or the main program. They do not preempt each other like processes or threads, and they
do not need synchronization mechanisms like locks or semaphores⁶.
* Coroutines are flexible, meaning they can be used for various purposes, such as
concurrent programming, asynchronous programming, event-driven programming,
generator functions, iterators, pipelines, etc

SEQUNCE CONTROL

Python, coroutines are often implemented using generators and the yield keyword. The
coroutine's main body contains yield statements to produce values.
Sending Values In: Use the yield expression to send values into the coroutine using the
[Link](value) method.
Receiving Values Out: Capture the values sent into the coroutine using the variable = yield
syntax. This allows the coroutine to receive inputs and process them.
CODE IN PYTHON

In this program, the coroutine() function is a coroutine. It defines a generator that yields a
value. When the coroutine() function is called, it prints "Coroutine started" and then
yields. The next() function is then called on the coroutine object. This causes the coroutine
to suspend its execution. The main program then prints "Main program running". When
the next() function is called again on the coroutine object, the coroutine resumes its
execution and prints "Coroutine resumed".
CONCURRENCY AND SUBPROGARM LEVEL CONCURRENCY
Concurrency is the ability of different parts or units of a program, algorithm, or
problem to be executed out-of-order or in partial order, without affecting the
outcome.
This allows for parallel execution of the concurrent units, which can significantly
improve overall speed of the execution in multi-processor and multi-core systems.
Concurrency can increase the performance, responsiveness, and scalability of a
program, but it also introduces challenges such as Synchronization, communication,
coordination, and data races.
Concurrency requires careful design and testing of concurrent systems to ensure
correctness, reliability, and efficiency.
Concurrency can be implemented using various methods, such as
Multi-processing,Multi-threading, etc
- Multiprocessing*: This involves creating multiple processes that run on different
CPUs or cores.
Each process has its own memory space and can communicate with other processes
through inter-process communication (IPC) mechanism.
Multiprocessing can take advantage of parallelism and avoid blocking I/O operations,
but it also has some drawbacks, such as high overhead, complex synchronization,
and potential security risks.
Examples of languages that support multiprocessing are Python, Java, and C.
- Multithreading*: This involves creating multiple threads that run within a single
process.
Each thread shares the same memory space and can access the same variables and
resources.
Multithreading can reduce the overhead of creating and switching processes, and
increase the responsiveness of the system, but it also introduces challenges, such as
race conditions, deadlocks, and memory leaks.
Examples of languages that support multithreading are Java, C#, and C++.
**A Thread is the smallest unit of execution within a process. It represents an
independent sequence of instructions that can be scheduled and executed by the
operating system.
Threads facilitate concurrency by allowing multiple tasks to be executed concurrently, even
on a single processor, through interleaved execution.
Subprogram-level concurrency is a form of concurrency where a program consists
of multiple subprograms or tasks that can be executed concurrently by different
threads or processes
Example of concurrency using threads

SYNCHRONISATION
Synchronization is the process of coordinating the execution of multiple processes or
threads that share resources or data.

There are two types of Processes in an Operating Systems:-


1. Independent Process –
The process that does not affect or is affected by the other process while
its execution then the process is called Independent Process. Example The
process that does not share any shared variable, database, files, etc.
2. Cooperating Process –
The process that affect or is affected by the other process while execution,
is called a Cooperating Process. Example The process that share file,
variable, database, etc are the Cooperating Process

Synchronization ensures that concurrent processes or threads access shared resources in a


controlled and consistent manner, avoiding problems such as Race conditions, Deadlocks,
and Data inconsistency.

**A Race Condition typically occurs when two or more threads try to read, write and
possibly make the decisions based on the memory that they are accessing
concurrently.
**A Deadlock occurs in a computer system when two or more processes are unable
to proceed because each is waiting for the other to release a resource.

** Data inconsistency happens when there are conflicting or contradictory data


values within a system or database.

Synchronisation is done to avoid race condition, in which we allow only one


process to enter and manipulates the shared data in Critical Section.

Synchronisation set up has four


parts –

1)Entry Section –It is part of the


process which decide the entry
of a particular process in the
Critical Section, out of many
other processes.
2)Critical Section –It is the part
in which only one process is
allowed to enter and modify
the shared [Link] part
ensures that only no other
process can access the resource
of shared data.
3)Exit Section –
This process allows the other process that are waiting in the Entry Section, to
enter into the Critical Sections. It checks that a process that after a process
has finished execution in Critical Section can be removed through this Exit
Section.
4)Remainder Section –
The other parts of the Code other than Entry Section, Critical Section and Exit
Section are known as Remainder Section.
Requirements of Synchronization mechanisms

Primary

1. Mutual Exclusion
Our solution must provide mutual exclusion. By Mutual Exclusion, we
mean that if one process is executing inside critical section then the
other process must not enter in the critical section.

2. Progress
Progress means that if one process doesn't need to execute into critical
section then it should not stop other processes to get into the critical
section.

Secondary

1. Bounded Waiting

We should be able to predict the waiting time for every process to get
into the critical section. The process must not be endlessly waiting for
getting into the critical section.

2. Architectural Neutrality

Our mechanism must be architectural natural. It means that if our


solution is working fine on one architecture then it should also run on
the other ones as well.

DIFFERENT METHODS OF SYNCHRONISATION

There are different methods to achieve synchronization, such as Semaphores,


Monitors, and Message passing.

SEMAPHORES
MONITORS
MESSAGE PASSING
1.) Message passing is a synchronization technique that allows
processes or threads to communicate and synchronize by sending
and receiving messages.
2.) A message is a data structure that contains information about the
sender, the receiver, and the content of the communication.
3.) Message passing can be implemented using various methods, such
as shared memory, message queues, pipes, sockets, etc.
4.) Message passing can be synchronous or asynchronous, depending
on whether the sender and the receiver have to wait for each other
or not.
5.) Message passing can be used to implement distributed systems,
inter-process communication, and event-driven programming.
DATA CONTROL

In computer science, data control refers to the methods used to manage the
flow of data between different parts of a program. Two methods of data
control are direct transmission and referencing environment .
Direct transmission is a method of data control where a data object computed
at one point as the result of an operation may be directly transmitted to
another operation as an operand. For example, consider the following code
snippet in Python:
x = y + 2*z
Here, the result of multiplication is transmitted directly as an operand of the
addition operation .
Referencing environment is a method of data control where a data object may
be given a name when it is created, and the name may then be used to
designate it as an operand of an operation. Program elements that may be
named include variables, formal parameters, subprograms, defined types,
defined constants, labels, exception names, primitive operations, and literal
constants 1. For example, consider the following code snippet in C:
int x = 10;
int *p = &x;
Here, x is a named data object, and p is a pointer that references the memory
location of x
In programming languages, a name is an identifier that is used to refer to a
variable, function, class, or other entity in the program. There are different
types of names in programming languages, some of which are:
1. Variable names: These are used to store values and can be assigned to
different data types such as integers, strings, and floats. Examples of variable
names include x, y, and z.
2. Function names: These are used to define a block of code that performs a
specific task. Examples of function names include print(), sqrt(), and len().
3. Class names: These are used to define a blueprint for creating objects that
share common attributes and methods. Examples of class names
include Person, Car, and Animal.
4. Module names: These are used to organize code into separate files or modules
that can be imported into other programs. Examples of module names
include math, random, and os.
5. Label names: These are used to mark a specific location in the program and are
often used in conjunction with control flow statements such
as goto or break. Examples of label names include start, end, and loop
REFERENCING ENVIRONMENT

A Referencing environment is a set of associations between identifiers and their values. It is


the collection of all names that are visible in a statement 1. The referencing environment of
a statement includes the local variables plus all of the visible variables in all of the enclosing
scopes 1.
For example, in C programming language, the local referencing environment is used to store
variables that are declared within a function. The nonlocal referencing environment is used
to store variables that are declared outside a function but are used within it. The global
referencing environment is used to store variables that are declared outside a function and
are used throughout the program. The predefined referencing environment includes built-in
functions such as printf() and scanf()

There are different types of referencing environments in programming languages, some of


which are:
1. Local referencing environment: This is the set of associations created on entry to a
subprogram that represent formal parameters, local variables, and subprograms defined
only within that subprogram 1. For example, in C programming language, the local
referencing environment is used to store variables that are declared within a function.
2. Nonlocal referencing environment: This is the set of associations for identifiers that may be
used within a subprogram but that are not created on entry to it. It can be global or
predefined 1. For example, in Python programming language, the nonlocal referencing
environment is used to store variables that are declared outside a function but are used
within it.
3. Global referencing environment: This is the set of associations created at the start of
execution of the main program, available to be used in a subprogram 1. For example, in C
programming language, the global referencing environment is used to store variables that
are declared outside a function and are used throughout the program.
4. Predefined referencing environment: This is the set of predefined associations in the
language definition 1. For example, in Python programming language, the predefined
referencing environment includes built-in functions such as print() and len().

You might also like