1/51
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
what is a command in programming languages?
a construct that performs and action does not return a value
give examples of commands
assignment statements, if statements, while statements, void function/procedure calls
can commands be written where expressions normally appear?
depends on the language. some languages treat certain commands as expressions with values
basic form of an assignment?
l value = r-value
what is the difference between l-values and r-values?
l-value: identifies a storage location
r-value: provides a value to be stored there
what common error occurs due to = being allowed in expressions?
using = instead of == in conditionals
copy semantics?
assignment copies the value of the right side into the left side. subsequent changes do not affect each other
reference semantics?
assignment makes both variables refer to the same underlying object. changing one affects the other
languages that use copy semantics?
Pascal, C, Ada, most built-in types in Java, default behavior in C++
languages that use reference semantics?
LISP and Java for all objects
in copy semantics, what does x = y; do?
copies the value of y into x. they become independent
what can serve as an l-value?
x, A[i], rec.field
what can’t serve as an l-value?
expressions that only yield values, such as x+y, 23, foo(x)
what is a shallow copy?
copy the pointer value; the new object refers to the same inner objects.
what is a deep copy?
copy the entire object graph; internal referenced objects are duplicated
which languages normally perform shallow copies?
C, Pascal, Ada, Java
why may shallow copy be insufficient?
mutating a shared referenced object changes both copies — unexpected aliasing.
why does C++ allow custom assignment operators and copy instructors?
to allow the programmer to implement deep copy when needed
what distinguishes a command from an expression?
commands are statements with effects but no return value; expressions compute and return a value
in C/Java, is = an opeator?
yes — = returns the assigned value
what enables code like a = b = c = 0; ?
assignment being an expression that associates right-to-left
what is a danger of assignment-as-expression?
using = instead of == inside conditionals
what two features define imperative PLs?
assignment and control flow
what is control flow?
the specific statement currently being executed
what is a basic block?
a consecutive sequence of statements with single entry and single exit
what does GOTO do?
unconditional branch to labeled statement
what is an arithmetic IF?
IF (expr) s, t, u → go to label based on negative/zero/positive
why was early FORTRAN known for “spaghetti code”?
arbitrary jumps created tangled, nonstructured flow.
what is structured programming?
programs composed only of single-entry/single-exit constructs: sequencing, selection, iteration
what is sequencing?
grouping statements into ordered blocks with one entry and exit
what problem does structured programming solve in selection statements?
ensures nested structure matches control-flow intent
what is a dangling else?
ambiguity of which if an else belongs to
what is a definite loop?
number of iterations known at loop entry
why is the C for loop flexible?
initialization, condition, and increment are arbitrary statements/expressions
what are the three major syntactic forms of expressions?
infix, prefix, postfix
write x * (y+1) - z in postfix
x y 1 + * z -
write x * (y+1) - z in prefix
- * x + y 1 z
why can all three forms be generated from an AST?
because the AST captures structure independent of notation
what tree traversal produces postfix notation?
post-order traversal
why is postfix easy to evaluate?
because operators come after operands, enabling stack-based evaluation
what is the general algorithm for evaluating postfix?
read tokens left to right
push operands
on operator: pop two operands, apply operator, push result
why does short-circuiting change observable behavior?
because operands may have side effects; skipping evaluation means skipping side effects
why is short-circuiting important?
prevents invalid operations
prevents unwanted side effects
when does the second operand of expr1 || expr2 get evaluated?
only when expr1 is false
when does the second operand of expr1 && expr2 get evaluated?
only when expr1 is true
what does short-circuiting mean?
the second operand is evaluated only if needed
what must conditional operator code generation include?
branching logic (Jump to L1/L2)
what is the idea behind postfix code generation?
generate the code that pushes operands, then pops them wehn applying operators
what does the non-stack method use instead of stack pushes/pops?
temporary variables
why is this approach often faster?
accessing temporaries is quicker than stack operations
why is memory cost not as bad as it seems?
optimizers often merge temporary variables
what is a side effect?
any effect of evaluating an expression beyond producing a value