1/61
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Arithmetic Expression
Was one of the motivations for the development of the first programming languages
It consist of operators, operands, parentheses, and function calls
Arithmetic Expressions Design Issues
Operator precedence rules?
Operator associativity rules?
Order of operand evaluation?
Operand evaluation side effects?
Operator overloading?
Type mixing in expressions?
Operator
A unary __ has one operand
A binary __ has two operand
A ternary __ has three operand
It is symbols or keywords used to perform operations on data values or variables. They are the building blocks of expressions
Operator Precedence Rules
It is when expression evaluation define the order in which “adjacent” operators of different precedence levels are evaluated
Typical ___ levels
parentheses
unary operators
** (if the language supports it)
*, /
+, -
It determines the order in which operators are evaluated in an expression when multiple operators appear together — it helps the program to decide which operations to perform first
Operator Associativity Rule
It is when expression evaluation define the order in which adjacent operators with the same precedence level are evaluated
Typical ___ rules
left to right, except **, which is right to left
sometimes unary operators associate right to left (e.g in FORTRAN)
It defines the direction in which operators of the same precedence level are evaluated in an expression when no parentheses are used
Conditional Expressions
A type of arithmetic expression where a statement evaluates to either true or false, based on a condition
Operand Evaluation Order
Variables: fetch the value from memory
Constants: sometimes a fetch from memory; sometimes the constant is in the machine language instruction
Parenthesized expressions: evaluate all operands and operators first
It refers to the sequence in which a programming language evaluates the operands of an operator in an expression — especially when there are multiple operands and side effects involved
Functional side effects
When a function changes a two-way parameter or a non-local variable
Problems: when a function referenced in an expression alters another operand of the expression
Solutions:
Write the language definition to disallow functional __ __
Write the language definition to demand that operand evaluation order to be fixed
This occurs when a function modifies some state or interacts with the outside world outside of returning a value. In other words, the function does more than just compute and return a result — it affects something external to its local scope
Referential transparency
A program has this property if any two expressions in the program that have the same value can be substituted for one another anywhere in the program, without affecting the action of the program
It is a property in programming where an expression can be replaced with its value without changing the program’s behavior
Overloaded Operators
Use of an operator for more than one purpose
Type Conversion
It has two types: narrowing conversion and widening conversion
Narrowing Conversion
Is a type conversion that converts an object to a type that cannot include all of the values of the original type e.g., float to int
Widening Conversion
Is one in which an object is converted to a type that an include at least approximations to all of the values of the original type e.g., int to float
Mixed Mode
This expression is one that has operands of different types
A coercion is an implicit type conversion
Refers to expressions or operations that involve more than one data type — for example, combining int and float numbers in one calculation
Explicit Type Conversions
Called casting or type casting in C-based languages
(int)angle
Number(variable)
Relational Expressions
Use relational operators and operands of various types
Evaluate to some Boolean Representation
It is a type of logical expression that compares 2 values using a relational operator and evaluates to a Boolean result
Boolean Expressions
Operands and the result is Boolean
Example operators are AND, OR, and NOT
Short Circuit Evaluation
An expression in which the result is determined without evaluating all of the operands and/or operators
Assignment Statements
General syntax:
<target_var> <assign_operator> <expression>
Assigning a variable with a value
Conditional Targets
You can assign a value when the condition is fulfilled
Compound Assignment Operators
A shorthand method of specifying a commonly needed form of assignment
a += b === a = a+b
Unary Assignment Operators
Combines increment and decrement operations with assignments
Multiple Assignments
Can assign multiple statements all at onnce
(first, second, third) = (20, 30, 40)
Control Statements
This control the flow of execution in a program.
They let the program make decisions, repeat tasks, or jump to different parts of code
Control Structure
It is a control statement and the statements whose execution it controls
It is a programming construct that determines the flow of control or execution order of statements in a program. It allows you to make decisions, repeat tasks, and control the logical behavior of your code
Selection statement
Provides the means of choosing between two or more paths of execution
Has 2 general categories:
Two-way selectors
Multiple-way selectors
Two-Way Selection Statements
This is a type of conditional statement that allows a program to choose between two paths of execution based on whether a condition is true or false
If Else statements
The Control Expression
If the then reserved word or some other syntactic marker is not used to introduce the then clause, the control expression is placed in parentheses
It decide what happens next in a program, it evaluates to either true or false
if
while
for
Clause Form
In many contemporary languages, the then and else clauses can be single statements or compound statements
It is a way of writing logical expressions in a standardized format
Nesting Selectors
Refers to placing control statements, functions inside others — usually to represent hierarchy, scope or conditional logic
Selector Expressions
It is typically an if-then-else construct that is treated as an expression, meaning that it returns a value
Multiple-Way Selection Statements
Allows the selection of any number of statements or statement groups
Its a conditional structure that allow a program to choose among more than two more possible paths based on different conditions
Extended form of if-else
if grade == "A":
print("Excellent")
elif grade == "B":
print("Good")
elif grade == "C":
print("Average")
else:
print("Fail")
switch (grade) {
case 'A':
System.out.println("Excellent");
break;
case 'B':
System.out.println("Good");
break;
case 'C':
System.out.println("Average");
break;
default:
System.out.println("Fail");
}
Iterative Statements
They are also called loops which are control structures that repeat a block of code multiple times until a condition is no longer true
The repeated execution of a statement or compound statement is accomplished either by iteration or recursion
Counter-Controlled Loops
A counting iterative statement has a loop variable, and a means of specifying the initial and terminal, and step-size values
It is a type of iterative statement where the number of repetitions is know ahead of time and controlled by a counter variable
for i in range(5): # i goes from 0 to 4
print(i)
for (int i = 1; i <= 10; i++) {
printf("%d\n", i);
}
Logically-Controlled Loops
Repetition control is based on a Boolean expression
It repeats based on a logical condition, not a counter, it continues as long as the condition remains true, and may run an unknown number of times
Ideal when you don’t know how many times the loop will run - like user input, searching, or waiting for a specific event
password = ""
while password != "secret":
password = input("Enter password: ")
Scanner sc = new Scanner(System.in);
int num = 0;
while (num >= 0) {
System.out.print("Enter a negative number to stop: ");
num = sc.nextInt();
}
User-Located Loop Control Mechanisms
Sometimes it is convenient for the programmers to decide a location for loop control
It is specific keywords or statements that give the programmer direct control over how a loop behaves — such as when to skip, stop, or exit the loop manually
These are placed explicitly within the loop body by the user to influence its flow
for i in range(10):
if i == 5:
break
print(i)
# Stops at 4
for i in range(5):
if i == 2:
continue
print(i)
# Prints 0, 1, 3, 4
def search_for_five(numbers):
for n in numbers:
if n == 5:
return "Found"
return "Not Found"
Iteration Based on Data Structures
The number of elements in a data structure controls loop iteration
Looping directly over elements of a structure, letting the data control the flow instead of using just counters or logical conditions
Unconditional Branching
Transfers execution control to a specified place in program
Jumping from one part of a program to another without any condition — the program always jumps when the statement is executed, regardless of any logic or test
Some common examples are C’s goto and function calls + return
Subprograms
Each ___ has a single entry point
The calling program unit is suspended during execution of the ___
Control always return to the caller when ___ execution terminates
It is a self-contained block of code designed to perform a specific task, and can be called or reused from different parts of a program
Subprogram call
Explicit request that the subprogram be executed
Subprogram header
First line of the subprogram definition
Specifies that the following syntactic unit is a subprogram definition of some particular kind, often accomplished with a special word
Provide a name for subprogram
May optionally specify list of parameters
Subprogram Basic Definitions
Subprogram definition
Subprogram Call
Subprogram Header
Subprogram Definition
Describes the actions of the subprogram abstraction
Subprogram Parameters
Formal Parameters
Actual Parameters
Positional Parameters
Keyword Parameters
Mixed Positional and Keyword Parameter
Formal Parameters
Parameters in the subprogram header
Actual Parameters
Parameters that are supplied during subprogram calls
Positional Parameter
The binding of actual parameters to formal parameters is done by simple position
Keyword Parameters
Name of the formal parameter to which an actual parameter is to be bound is specified with the actual parameter
Parameter
In some languages, formal ___ can have default values
They are placeholders used in subprograms to receive input values when the subprogram is called. They let you pass data into a function so it can work with dynamic values
Procedures
A subprogram category which is a collections of statements that define parameterized computation
Can produce result in the calling program unit by two methods
If there are variables that are not formal parameters but are still visible to the procedure
If the subprogram has formal parameters that allow transfer of data to the caller
A type of subprogram that performs a specific task but does not return a value directly to the caller — for example, displaying output, updating a file, or modifying data
Functions
Its structure resemble procedure but are modeled after mathematical functions
The value produced by its execution is returned to the calling code, effectively replacing the call itself
Functions define new user-defined operator
A type of subprogram that performs a task and returns a value to the part of the program that called it — it takes input (parameters), performs operations, and returns a value
Design issues for Subprograms
What parameter-passing method or methods are used?
Are the type of actual parameters checked against the types of the formal parameters?
Are local variables statically or dynamically allocated?
What is the referencing environment of a subprogram that has been passed as a parameter?
If subprograms can be passed as parameters, are the types of parameters checked in calls to the passed subprogram
Can subprograms be overloaded?
Can subprograms be generic
Is either separate or independent compilation possible?
Local Referencing Environment
Variables that are declared inside subprograms are called local variables
Local variables can be either statically or dynamically bound to storage
It includes all names (identifiers) the subprogram can refer to locally — like parameters, local variables
Parameter-passing methods
Ways in which parameters are transmitted to and/or from called subprograms
It describe how arguments (inputs) are sent from a calling program to a subprogram. These methods affect whether or not the subprogram can change the original values from the caller
Pass-by-value
It is a parameter passing method wherein
The value of the actual parameter is used to initialize the corresponding formal parameter
Normally Implemented by actual data transfer
If physical moves are done, additional storage is required for formal parameter
If physical moves are done, the move operation can be costly if the parameter is a large object such as a long array
A copy of the value is passed
Changes inside the subprogram do not affect the original value
Pass-by-result
It is a parameter passing method wherein:
Implementation model for out-mode parameters
No value is transmitted to the subprogram
Value of the formal parameter is passed back to the actual parameter before control is transferred back to the caller
Also requires extra storage and copy operations that are required by pass-by-value
Does not receive an initial value for the parameter, but it is expected to return a value back to the caller after the subprogram finishes execution
Pass-by-value-result
It is a parameter passing method wherein:
An implementation model for inout-mode parameters in which actual values are moved
The value of the actual parameter is used to initialize the corresponding formal parameter
At subprogram termination, the value of formal parameter is transmitted back to the actual parameter
Shares with pass-by-value and pass-by-result the disadvantages of requiring multiple storage for parameters and time for copying values
The subprogram receives a copy of the argument (like pass-by-value) and then, at the end of the subprogram, the final value is copied back to the original variable (like pass-by-result)
Pass-by-reference
It is a parameter passing method wherein:
An implementation of inout-mode parameters
Rather than transmitting data back and forth, pass-by-reference method transmits an access path, usually just an address
Passing process is efficient, in terms of both time and space
Aliases can be created
It is a method where the actual memory address of the argument is passed to the subprogram. This means the subprogram can directly access and modify the original variable
Type Checking Parameters
Software reliability demands that the types of actual parameters be checked for consistency with corresponding formal parameter
It refers to the process of verifying that the data types of arguments (actual parameters) match the expected types of parameters (formal parameters) when calling a subprogram
Overloaded Subprograms
A subprogram that has the same name as another subprogram in the same referencing environment
Every incarnation of an overloaded procedure must be unique in the types of its parameters
The meaning of a call to this subprogram is determined by the actual parameter list
These are functions or procedures that share the same name but have different parameter lists — meaning they can perform similar actions with different types of numbers of inputs
Separate Compilation
It means that the compilation units can be compiled at different times, but their compilation are not independent of each other if their accesses or uses any entities of the other
It is a programming technique where a large program is divided into multiple source files, and each file is compiled independently into object code before being linked together into a final executable
Independent Compilation
Program units can be compiled without information about other program units
Source files are compiled completely on their own, without needing information from other source files during compilation