Programming Languages - Midterm

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/61

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

62 Terms

1
New cards

Arithmetic Expression

Was one of the motivations for the development of the first programming languages

It consist of operators, operands, parentheses, and function calls

2
New cards

Arithmetic Expressions Design Issues

  • Operator precedence rules?

  • Operator associativity rules?

  • Order of operand evaluation?

  • Operand evaluation side effects?

  • Operator overloading?

  • Type mixing in expressions?

3
New cards

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

4
New cards

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

5
New cards

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

6
New cards

Conditional Expressions

A type of arithmetic expression where a statement evaluates to either true or false, based on a condition

7
New cards

Operand Evaluation Order

  1. Variables: fetch the value from memory

  2. Constants: sometimes a fetch from memory; sometimes the constant is in the machine language instruction

  3. 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

8
New cards

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

9
New cards

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

10
New cards

Overloaded Operators

Use of an operator for more than one purpose

11
New cards

Type Conversion

It has two types: narrowing conversion and widening conversion

12
New cards

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

13
New cards

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

14
New cards

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

15
New cards

Explicit Type Conversions

Called casting or type casting in C-based languages

  • (int)angle

  • Number(variable)

16
New cards

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

17
New cards

Boolean Expressions

  • Operands and the result is Boolean

  • Example operators are AND, OR, and NOT

18
New cards

Short Circuit Evaluation

An expression in which the result is determined without evaluating all of the operands and/or operators

19
New cards

Assignment Statements

  • General syntax:

    • <target_var> <assign_operator> <expression>

  • Assigning a variable with a value

20
New cards

Conditional Targets

You can assign a value when the condition is fulfilled

21
New cards

Compound Assignment Operators

A shorthand method of specifying a commonly needed form of assignment

  • a += b === a = a+b

22
New cards

Unary Assignment Operators

Combines increment and decrement operations with assignments

23
New cards

Multiple Assignments

Can assign multiple statements all at onnce

(first, second, third) = (20, 30, 40)

24
New cards

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

25
New cards

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

26
New cards

Selection statement

Provides the means of choosing between two or more paths of execution

Has 2 general categories:

  1. Two-way selectors

  2. Multiple-way selectors

27
New cards

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

28
New cards

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

29
New cards

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

30
New cards

Nesting Selectors

Refers to placing control statements, functions inside others — usually to represent hierarchy, scope or conditional logic

31
New cards

Selector Expressions

It is typically an if-then-else construct that is treated as an expression, meaning that it returns a value

32
New cards

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");
}

33
New cards

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

34
New cards

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);
}	

35
New cards

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();
}

36
New cards

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"

37
New cards

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

38
New cards

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

39
New cards

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

40
New cards

Subprogram call

Explicit request that the subprogram be executed

41
New cards

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

42
New cards

Subprogram Basic Definitions

  1. Subprogram definition

  2. Subprogram Call

  3. Subprogram Header

43
New cards

Subprogram Definition

Describes the actions of the subprogram abstraction

44
New cards

Subprogram Parameters

  1. Formal Parameters

  2. Actual Parameters

  3. Positional Parameters

  4. Keyword Parameters

  5. Mixed Positional and Keyword Parameter

45
New cards

Formal Parameters

Parameters in the subprogram header

46
New cards

Actual Parameters

Parameters that are supplied during subprogram calls

47
New cards

Positional Parameter

The binding of actual parameters to formal parameters is done by simple position

48
New cards

Keyword Parameters

Name of the formal parameter to which an actual parameter is to be bound is specified with the actual parameter

49
New cards

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

50
New cards

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

  1. If there are variables that are not formal parameters but are still visible to the procedure

  2. 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

51
New cards

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

52
New cards

Design issues for Subprograms

  1. What parameter-passing method or methods are used?

  2. Are the type of actual parameters checked against the types of the formal parameters?

  3. Are local variables statically or dynamically allocated?

  4. What is the referencing environment of a subprogram that has been passed as a parameter?

  5. If subprograms can be passed as parameters, are the types of parameters checked in calls to the passed subprogram

  6. Can subprograms be overloaded?

  7. Can subprograms be generic

  8. Is either separate or independent compilation possible?

53
New cards

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

54
New cards

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

55
New cards

Pass-by-value

It is a parameter passing method wherein

  1. The value of the actual parameter is used to initialize the corresponding formal parameter

  2. Normally Implemented by actual data transfer

  3. If physical moves are done, additional storage is required for formal parameter

  4. 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

56
New cards

Pass-by-result

It is a parameter passing method wherein:

  1. Implementation model for out-mode parameters

  2. No value is transmitted to the subprogram

  3. Value of the formal parameter is passed back to the actual parameter before control is transferred back to the caller

  4. 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

57
New cards

Pass-by-value-result

It is a parameter passing method wherein:

  1. An implementation model for inout-mode parameters in which actual values are moved

  2. The value of the actual parameter is used to initialize the corresponding formal parameter

  3. At subprogram termination, the value of formal parameter is transmitted back to the actual parameter

  4. 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)

58
New cards

Pass-by-reference

It is a parameter passing method wherein:

  1. An implementation of inout-mode parameters

  2. Rather than transmitting data back and forth, pass-by-reference method transmits an access path, usually just an address

  3. Passing process is efficient, in terms of both time and space

  4. 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

59
New cards

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

60
New cards

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

61
New cards

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

62
New cards

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