1/98
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
Imperative programming languages
are, to varying degrees, abstractions of the underlying von Neumann computer architecture.
are designed in a way that closely follows how a computer actually works. They tell the computer step by step what to do, just like a set of instructions in a recipe.
is about giving the computer a sequence of commands to change its memory and perform tasks.
Focus
How to do something
State Changes
Uses variables that change
Examples
C, Java, Python (loops)
Code Style
Step-by-step instructions
Uses variables to store values and update them as needed.
Uses control structures such as loops (for, while) and conditional statements (if-else).
Focuses on changing the program’s state (i.e., modifying values in memory).
von Neumann architecture
is the basic design of most computers.
memory,
processor
von Neumann architecture’s primary components (2)
memory
stores both the instructions (commands) and the data (information) needed for a program.
processor
provides operations for modifying the contents of the memory
Functional programming languages
allow expressions to be named.
However, there is a key difference: once you assign a value to a name in functional programming, it cannot be changed.
This makes it more like a constant in imperative languages (where constants are values that don’t change).
Uses functions that avoid changing variables (e.g., Haskell, Lisp).
Focus
What to do
State Changes
Avoids changing variables
Examples
Haskell, SQL, Prolog
Code Style
Expresses logic or computations without steps
pure functional programming languages
do not have variables that are like those of the imperative languages.
However, many _____ do include such variables.
there are no traditional variables that store changing values.
But some functional languages do allow variables that behave more like those in imperative languages.
Non-imperative programming
is like stating what you want, without specifying how to do it.
The computer figures out the process itself.
Functional Programming,
Logic Programming,
Query-Based Programming
Non-imperative programming (3)
Logic Programming
Uses rules and facts to infer conclusions (e.g., Prolog).
Query-Based Programming
Focuses on retrieving data without specifying how (e.g., SQL).
Are names case sensitive?
Are the special words of the language reserved words or keywords?
Design Issues (2)
case sensitivity and reserved words
impact code readability, ease of use, error prevention, and compatibility.
These are considered issues because they can create challenges for programmers and influence how code is written and understood.
name
is a string of characters used to identify some entity in a program.
is technically called an identifier.
in most programming languages have the same form: a letter followed by a string consisting of letters, digits, and underscore characters ( _ ).
Fortran 95+
allows up to 31 characters in its names.
C99
has no length limitation on its internal names, but only the first 63 are significant.
Java,
C#,
Ada
Names in _____, _____, and _____ have no length limit, and all characters in them are significant.
C++
does not specify a length limit on names, although implementors sometimes do.
underscore
Although the use of _____ characters to form names was widely used in the 1970s and 1980s, that practice is now far less popular.
C-based,
camel notation
In the _____ languages, it has to a large extent been replaced by the so-called _____, in which all of the words of a multiple-word name except the first are capitalized, as in myStack.
programming style issue
Note that the use of underscores and mixed case in names is a _____, not a language design issue.
PHP
All variable names in _____ must begin with a dollar sign.
Perl
In _____, the special character at the beginning of a variable’s name, $, @, or %, specifies its type (although in a different sense than in other languages).
Ruby
In _____, special characters at the beginning of a variable’s name, @ or @@, indicate that the variable is an instance or a class variable, respectively.
C-based languages
In many languages, notably the _____, uppercase and lowercase letters in names are distinct; that is, names in these languages are case sensitive.
allow any compound statement (a statement sequence surrounded by matched braces) to have declarations and thereby defined a new scope.
case sensitivity
To some people, this is a serious detriment to readability, because names that look very similar in fact denote different entities.
In that sense, _____ violates the design principle that language constructs that look similar should have similar meanings. But in languages whose variable names are case-sensitive, although Rose and rose look similar, there is no connection between them.
Hard to Read
Names that look almost the same might mean totally different things.
Easy to Make Mistakes
A small typo can cause errors that are hard to find.
Reserved Words
Cannot be used as variable or function names.
In most languages, special words are classified as ______, which means they cannot be redefined by programmers
are better than keywords because the ability to redefine keywords can be confusing.
Keywords
Can sometimes be used as variable names if the context allows
is a word of a programming language that is special only in certain contexts.
Reserved Keywords
are words that have a special, predefined meaning.
Fortran or Formula Translation
is the only remaining widely used language whose special words are keywords.
is a "grandfather" of programming languages, and it handles this very differently.
words are often keywords, not reserved words.
Integer
In Fortran, the word _____, when found at the beginning of a statement and followed by a name, is considered a keyword that indicates the statement is a declarative statement.
However, if the word _____ is followed by the assignment operator, it is considered a variable name.
variable
is an abstraction of a computer memory cell or collection of cells
characterized by a collection of properties, or attributes,
are like labels for specific storage locations in a computer's memory.
They act as an abstraction, meaning they simplify how we interact with memory without dealing with its complexity.
name,
address,
value,
type,
lifetime,
scope
A variable can be characterized as a sextuple of attributes (6)
Address
is the machine memory address with which it is associated.
The actual location in memory where the variable's data is stored.
It is possible to have multiple variables that have the same _____
aliases
When more than one variable name can be used to access the same memory location, the variables are called _____
Aliasing
is a hindrance to readability because it allows a variable to have its value changed by an assignment to a different variable.
type
determines the range of values the variable can store and the set of operations that are defined for values of the type.
The kind of data the variable holds (e.g., integer, string, boolean).
a fundamental concept in programming languages.
determines how the computer interprets and uses the data.
Value
is the contents of the memory cell or cells associated with the variable.
The current data stored in the variable (e.g., age = 25).
abstract memory cell
has the size required by the variable with which it is associated.
binding
is an association between an attribute and an entity, such as between a variable and its type or value, or between an operation and a symbol.
binding time
time at which a binding takes place
language design time,
language implementation time,
compile time,
load time,
link time,
run time
Bindings can take place (binding time) at (6)
Language Design Time
When the rules of the language are created (e.g., deciding that int means integer).
Language Implementation Time
When compilers or interpreters are built to follow the language rules.
Compile Time
When the program is translated into machine code (e.g., checking variable types)
Load Time
When the program is loaded into memory before execution.
Link Time
When different parts of a program are combined (e.g., connecting external libraries).
Run Time
When the program is actually running (e.g., assigning a new value to a variable).
Identifier (The Name),
Entity (The Thing)
Parts of a Binding (2)
Identifier (The Name)
This is the word you type in your code (e.g., user_age).
Entity (The Thing)
This is the actual data, memory address, or function the name represents.
Different bindings (associations)
_____ happen at different times when the program is being written, compiled, and executed.
Static Binding,
Dynamic Binding
Binding Types (2)
Static Binding
This happens before the program even runs (usually while the code is being compiled).
It is permanent and fast.
Dynamic Binding
This happens while the program is running.
The name can be linked to different things at different times.
the type of a variable is not specified by a declaration statement, nor can it be determined by the spelling of its name.
the variable is bound to a type when it is assigned a value in an assignment statement.
explicit declaration
is a statement in a program that lists variable names and specifies that they are a particular type.
make code clearer and safer, preventing mistakes.
This means you clearly specify a variable’s type when declaring it.
Implicit declarations
is a means of associating variables with types through default conventions, rather than declaration statements.
make coding faster and more flexible, but may introduce errors if the wrong type is assumed
This means the compiler automatically assigns a type based on how the variable is used.
Some of the problems here can be avoided by requiring names for specific types to begin with particular special characters.
mean that a variable’s type is not explicitly stated when it is first used.
names of variables,
data itself
In reality, the _____ are never bound to types.
Names can be bound to variables and variables can be bound to types.
Instead, a variable name is connected to a memory location (where the data is stored).
The _____ determines the type of the variable at any given time.
allocation
The process where a memory cell to which a variable is bound somehow must be taken from a pool of available memory.
is the technical term for reserving memory for a variable.
The operating system or programming language runtime manages memory _____ automatically or manually (depending on the language).
Deallocation
is the process of placing a memory cell that has been unbound from a variable back into the pool of available memory.
When a variable is no longer needed, its memory should be released so that other parts of the program can use it.
lifetime
is the time during which the variable is bound to a specific memory location.
begins when it is bound to a specific cell and ends when it is unbound from that cell.
In a Java method, it is the period of time beginning when the method is entered and ending when execution of the method terminates
The variable comes into existence when the method starts.
It disappears from memory when the method finishes execution
Static variables
are those that are bound to memory cells before program execution begins and remain bound to those same memory cells until program execution terminates.
scope
is the range of statements in which the variable is visible.
A variable is visible in a statement if it can be referenced in that statement.
In a Java method, it is from its declaration to the end of the method
Local variable
is local in a program unit or block if it is declared there.
Non-local variable
of a program unit or block are those that are visible within the program unit or block but are not declared there.
Usually, they are global variables (declared outside functions).
ALGOL 60
introduced the method of binding names to non-local vars is called static scoping.
Static scoping
method of binding names to non-local vars
is named because the scope of a variable can be statically determined – that is prior to execution.
This permits a human program reader (and a compiler) to determine the type of every variable in the program simply by examining its source code.
Python mainly uses static scoping, meaning a variable’s scope is determined by its location in the code, not by which functions are currently running.
Nested Subprograms,
Subprograms that cannot be nested
categories of static scoped languages (2)
Nested Subprograms (The "Matryoshka Doll" Style)
Functions (or procedures) can be defined inside other functions.
Inner functions have access to the variables of their outer functions.
Subprograms That Cannot Be Nested (The "Apartment Building" Style)
Functions cannot be defined inside other functions.
Each function is declared separately and does not automatically access variables from other functions.
ALGOL 60
allows a section of code to have its own local variables whose scope is minimized.
stack dynamic
From ALGOL 60, allows a section of code to have its own local variables whose scope is minimized.
Such variables are _____, so they have their storage allocated when the section is entered and deallocated when the section is exited.
C99,
C++,
Java,
C#
allow variable declarations to appear anywhere a statement can appear (4)
C99,
C++,
Java
the scope of all local variables is from the declaration to the end of the block (3)
C#
the scope of any variable declared in a block is the whole block, regardless of the position of the declaration in the block
However, a variable still must be declared before it can be used
C++,
Java,
C#
variables can be declared in for statements (3)
The scope of such variables is restricted to the for construct
C,
C++,
PHP,
Python
support a program structure that consists of a sequence of function definitions in a file (4)
These languages allow variable declarations to appear outside function definitions
Dynamic scoping
is a method of determining which variables a function can access based on the order of function calls at runtime, rather than where the variables are written in the code.
looks at the most recent functions that were called and allows the current function to use variables from those functions, even if the variables were declared outside the current function.
When a variable is used in _____, the program searches backward through all active function calls to find where it was last declared.
This means that a function can use variables from functions that called it, even if those variables were never passed as parameters.
The program does not check the written structure but instead looks at which functions are currently running.
the referencing environment is the local variables plus all visible variables in all active subprograms."
APL,
SNOBOL4,
early versions of LISP
The scope of variables in _____, _____, and the _____ is dynamic.
Perl,
Common Lisp
_____ and _____ allow variables to be declared to have dynamic scope, although the default scoping mechanism is these languages is static.
References to variables
are connected to declarations by searching back through the chain of subprogram calls that forced execution to this point.
static variable in a C or C++ function
Statically bound to the scope of that function and is also statically bound to storage
The scope of a static variable is limited to the function where it is declared.
However, it is stored permanently (not created and destroyed with function calls).
Its scope is static and local to the function but its lifetime extends over the entire execution of the program of which it is a part
variable can only be accessed within its function (local scope).
However, it stays in memory for the entire runtime of the program.
This means even if the function is called multiple times, the variable retains its value from previous calls.
referencing environment
of a statement is the collection of all names that are visible in the statement
of a statement is needed while that statement is being compiled,
so code and data structures can be created to allow references to variables from other scopes during run time.
refers to the set of variables that are visible and accessible at a particular point in a program.
It defines where and how a variable can be accessed within a program's structure.
variables,
data structures,
during execution
Why is the Referencing Environment Needed During Compilation?
When the compiler reads the program, it needs to determine which _____ a statement can access.
The compiler uses this information to set up _____ (such as symbol tables and memory locations) that will allow the program to correctly reference variables at runtime.
This ensures that, _____, the program can retrieve the correct variable value, even if the variable is declared in another scope.
subprogram
is active if its execution has begun but has not yet terminated.
While it is active, it can still use and modify variables.
Local (L),
Enclosing (E),
Global (G),
Built-in (B)
In Python, the LEGB Rule determines the referencing environment
Local (L)
Variables defined inside a function.
Enclosing (E)
Variables in an outer function when using nested functions.
Global (G)
Variables defined at the top level of the script.
Built-in (B)
Predefined Python functions and constants (like print() or len()).
During Compilation
How Referencing Environment is Used During Compilation and Runtime
The Python interpreter analyzes which variables are available for each statement.
During Runtime
How Referencing Environment is Used During Compilation and Runtime
When inner() executes, Python follows the LEGB rule to find variable values dynamically.
Named Constants
It is a variable that is bound to a value only at the time it is bound to storage; its value cannot be change by assignment or by an input statement.
Readability,
Modifiability
Named Constants Advantages (2)
Readability
Instead of using the number 100 everywhere in your code, you can use LEN, which makes it easier to understand.
Modifiability
If you need to change the value, you only have to update it in one place (where it is first defined) instead of searching for every 100 in your code.
initialization
binding of a variable to a value at the time it is bound to storage
is often done on the declaration statement.