450 exam 3 Flashcards
unary operator
has one operand; e.g., ++, –
binary operator
has two operands; e.g., +, -, *, /
ternary operator
has three operands; e.g., ? : conditional expression syntax (ex: average = (count == 0) ? 0 : sum / count;)
operator precedence rules for expression evaluation define
the order in which “adjacent” operators of different precedence levels are evaluated
typical precedence levels (highest to lowest precedence)
parentheses unary operators ** (if the language supports it) *, / \+, -
operator associativity rules for expression evaluation define
the order in which adjacent operators with the same precedence level are evaluated
associativity for operators is typically ? except for ?
left to right; ** is right to left
what does 2 ** 3 ** 4 evaluate to using typical associativity rules?
2 ** 81
associativity for assignment is ?
right to left
what are the values of a=1, b=2, c=3 after executing a = b += c ?
a=5, b=5, c=3
functional side effects
occur when a function changes either one of its parameters or a global variable (declared outside the function but is accessible in the function)
functions in mathematics (do/do not) have side effects - why?
do not b/c there is no notion of variables in mathematics
functional programming languages have no variables, so do they have functional side effects?
no
a program has referential transparency 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
advantage of referential transparency
semantics easier to understand
programs in pure functional languages (are/are not) referentially transparent - why?
are; b/c they do not have variables (so, if a function uses an outside value, it must be a constant)
narrowing conversion
converts a value to a type that cannot include all of the value of the original type (ex: float to int)
widening conversion
a value is converted to a type that can include all of the value of the original type (ex: int to float)
coercion
implicit type conversion
casting
explicit type conversion
relational expressions
use relational operators/operands of various types; evaluate to true or false; operators !=, >=, <=, etc.
boolean expressions
operands are boolean and the result is boolean; operators &&, ||, !
short-circuit evaluation
the result is determined without evaluating all of the operands and/or operators
when would the following be an example of short-circuit evaluation?
(13 * a) * (b / 13 - 1)
if a=0, there is no need to evaluate (b / 13 -1) because the result will be 0
assignment statements general syntax
[target_var] [assign_operator] [expression]
compound assignment operator
a shorthand method of specifying a commonly needed form of assignment (e.g., +=)
unary assignment operators
in C-based languages; combine increment and decrement operations with assignment (e.g., sum = count++;)
multiple assignments
allowed by Perl, Python, and Ruby; e.g., ($first, $second, $third) = (20, 30, 40);
example of multiple assignment in Python
interchange: (a, b) = (b, a)
mixed-mode assignment in Fortran, C, Perl, and C++
any numeric type value can be assigned to any numeric type variable
mixed-mode assignment in Java and C#
only widening assignment coercions are done (e.g., int x = 3.5;) is valid in C++ but not in Java
control structure
a control statement and the statements whose execution it controls
selection statement
provides the means of choosing between two or more paths of execution
two general categories of selection statements
two-way selectors & multiple-way selectors
general form of two-way selection statements
if control_expression then
clause
else
clause
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
in what languages can the control expression be arithmetic? what is the type of a control expression in most other languages?
C/C++ and Python; Boolean
Python uses ? to define clauses and nest selectors
indentation
Java’s static semantics rule: else matches with which if? to force it to match with something else, use ?
the nearest previous if; braces
Ruby uses the keyword ? for nesting selectors
end
in ML, F#, and Lisp, the selector is
an expression
multiple-way selection statements
allow the selection of any number of statements or statement groups
C/C++, Java, and JavaScript use ? for multiple-way selection
switch statements
Ruby uses ? for multiple-way selection
case expression
Python uses ? for multiple-way selection
else-if clauses
iterative statement
one that causes a statement or collection of statements to be executed zero, one, or more times
iteration/recursion
the repeated execution of a statement or compound statement
an iterative statement is often called a
loop
counter-controlled loops: a counting iterative statement has
a loop variable, and a means of specifying the initial and terminal, and stepsize values
counter-controlled loops example
for loop
if the (first/second/third) expression is absent in a for loop, it is an infinite loop
second
counter-controlled loop syntax in C-based languages
for([expr1]; [expr2]; [expr3]) {
statement(s)
}
counter-controlled loop syntax in Python
for loop_variable in object:
loop body
the object in a Python counter-controlled loop is often a
range, which is either a list of values in brackets or a call to the range function
logically-controlled loops are also called
event-controlled (or condition-controlled) loops
repetition control in logically-controlled loops is based on
a Boolean expression
logically-controlled loops in C-based languages
while or do-while loops
C and C++ allow the control expression to be
arithmetic
Java is like C and C++ in regards to logically-controlled loops, except
the control expression must be Boolean and Java has no goto
unconditional branch statement
transfers execution control to a specified place in the program (e.g., goto statement)
major concerns of unconditional branching
readability and reliability
what represented one of the most heated debates in the 1960’s and 1970’s?
unconditional branching (e.g., goto statements)
do all languages support goto?
no; for instance, Java
guarded command language (GCL)
language defined by Dijkstra in 1975
purpose of guarded command language
to support a new programming methodology that supported verification (correctness) during program development
basic idea of guarded command language
if the order of evaluation is not important, the program should not specify one
guarded command
each line in the selection statement, consisting of a Boolean expression (a guard) and a statement or statement sequence
semantics of guarded command: when construct is reached,
evaluate all Boolean expressions
if more than one is true, choose one non-deterministically
if none of the conditions are true, error
subprogram definition
describes the interface to and the actions of the subprogram abstraction
subprogram call (or invocation)
an explicit request that the subprogram be executed
subprogram header
the first part of the definition, including the name, the kind of subprogram, and the formal parameters
subprogram profile/signature
the name of the subprogram and the parameter list (number, order, and types of its parameters)