Podcast
Questions and Answers
Which of the following languages use !== and != for inequality?
Which of the following languages use !== and != for inequality?
What is the operator for 'not' in C?
What is the operator for 'not' in C?
Which of the following operators has the highest precedence in C-based languages?
Which of the following operators has the highest precedence in C-based languages?
What is the term for an expression that is determined without evaluating all of the operands and/or operators?
What is the term for an expression that is determined without evaluating all of the operands and/or operators?
Signup and view all the answers
In which of the following languages are all logical operators short-circuit evaluated?
In which of the following languages are all logical operators short-circuit evaluated?
Signup and view all the answers
What is the primary role of expressions in programming languages?
What is the primary role of expressions in programming languages?
Signup and view all the answers
What is the result of the expression "7" == 7 in JavaScript?
What is the result of the expression "7" == 7 in JavaScript?
Signup and view all the answers
What is the primary motivation behind the development of the first programming languages?
What is the primary motivation behind the development of the first programming languages?
Signup and view all the answers
What is the term for an operator that has one operand?
What is the term for an operator that has one operand?
Signup and view all the answers
What is the operator for 'and' in Perl and Ruby?
What is the operator for 'and' in Perl and Ruby?
Signup and view all the answers
What determines the order in which adjacent operators of different precedence levels are evaluated?
What determines the order in which adjacent operators of different precedence levels are evaluated?
Signup and view all the answers
What type of operator has three operands?
What type of operator has three operands?
Signup and view all the answers
What is the typical precedence level of the '*' operator?
What is the typical precedence level of the '*' operator?
Signup and view all the answers
What determines the order in which adjacent operators with the same precedence level are evaluated?
What determines the order in which adjacent operators with the same precedence level are evaluated?
Signup and view all the answers
What is the essence of imperative languages?
What is the essence of imperative languages?
Signup and view all the answers
What is the primary difference between the assignment operator in FORTRAN, BASIC, and the C-based languages, and the assignment operator in ALGOLs, Pascal, and Ada?
What is the primary difference between the assignment operator in FORTRAN, BASIC, and the C-based languages, and the assignment operator in ALGOLs, Pascal, and Ada?
Signup and view all the answers
What is the purpose of using == as the relational operator in the C-based languages?
What is the purpose of using == as the relational operator in the C-based languages?
Signup and view all the answers
What is the equivalent of the conditional target statement ($flag ?$total : $subtotal) = 0; in Perl?
What is the equivalent of the conditional target statement ($flag ?$total : $subtotal) = 0; in Perl?
Signup and view all the answers
What is the purpose of compound assignment operators?
What is the purpose of compound assignment operators?
Signup and view all the answers
What is the effect of the statement sum = ++count;
What is the effect of the statement sum = ++count;
Signup and view all the answers
What is the primary risk of using assignment as an expression in C?
What is the primary risk of using assignment as an expression in C?
Signup and view all the answers
What is the purpose of the statement ch = getchar () in the context of the while loop?
What is the purpose of the statement ch = getchar () in the context of the while loop?
Signup and view all the answers
What is the associativity rule for the ** operator in most programming languages?
What is the associativity rule for the ** operator in most programming languages?
Signup and view all the answers
What is the difference between the statements sum = ++count and sum = count++?
What is the difference between the statements sum = ++count and sum = count++?
Signup and view all the answers
What is the purpose of using parentheses in an expression?
What is the purpose of using parentheses in an expression?
Signup and view all the answers
What is the order of evaluation for operands in an expression?
What is the order of evaluation for operands in an expression?
Signup and view all the answers
What is a functional side effect in programming?
What is a functional side effect in programming?
Signup and view all the answers
What is the problem with functional side effects in programming?
What is the problem with functional side effects in programming?
Signup and view all the answers
What is operator overloading in programming?
What is operator overloading in programming?
Signup and view all the answers
Which programming languages allow user-defined overloaded operators?
Which programming languages allow user-defined overloaded operators?
Signup and view all the answers
What is the purpose of the ternary operator in conditional expressions?
What is the purpose of the ternary operator in conditional expressions?
Signup and view all the answers
What is the primary concern with users defining nonsense operations in a programming language?
What is the primary concern with users defining nonsense operations in a programming language?
Signup and view all the answers
What is the term for a type conversion that converts a value to a type that cannot include all of the values of the original type?
What is the term for a type conversion that converts a value to a type that cannot include all of the values of the original type?
Signup and view all the answers
What is the term for an implicit type conversion in an expression?
What is the term for an implicit type conversion in an expression?
Signup and view all the answers
What is the disadvantage of using coercions in expressions?
What is the disadvantage of using coercions in expressions?
Signup and view all the answers
What is the term for a result of an operation that cannot be represented in the memory cell where it must be stored?
What is the term for a result of an operation that cannot be represented in the memory cell where it must be stored?
Signup and view all the answers
What is the term for the limitations of arithmetic that result in a runtime error?
What is the term for the limitations of arithmetic that result in a runtime error?
Signup and view all the answers
What is the primary purpose of relational expressions in programming?
What is the primary purpose of relational expressions in programming?
Signup and view all the answers
What is the term for the process of converting a value to a type that can include at least approximations of all of the values of the original type?
What is the term for the process of converting a value to a type that can include at least approximations of all of the values of the original type?
Signup and view all the answers
Study Notes
Assignment Statements
- The general syntax of assignment statements varies across programming languages.
- The assignment operator is
=
in FORTRAN, BASIC, and C-based languages, while it is:=
in ALGOLs, Pascal, and Ada. - Using
=
as both the assignment and relational operator can lead to ambiguity, which is why C-based languages use==
for relational equality.
Conditional Targets
- Conditional targets allow assignment statements to be executed based on a condition.
- Example:
($flag ? $total : $subtotal) = 0;
in Perl, which is equivalent to anif-else
statement.
Compound Assignment Operators
- Compound assignment operators are a shorthand for specifying a common form of assignment.
- Introduced in ALGOL and adopted by C and other languages.
- Example:
a = a + b
can be written asa += b
.
Unary Assignment Operators
- Unary assignment operators in C-based languages combine increment and decrement operations with assignment.
- Examples:
sum = ++count
,sum = count++
,count++
, and-count++
.
Assignment as an Expression
- In C and C++, the assignment statement produces a result and can be used as an operand.
- This can lead to loss of error detection and program errors.
- Example:
while ((ch = getchar ()) != EOF) {…}
.
Boolean Expressions
- Operands are Boolean, and the result is Boolean.
- Example operators:
&&
and||
in C,and
andor
in Ada, andand
andor
in Perl and Ruby.
Short-Circuit Evaluation
- An expression where the result is determined without evaluating all operands and operators.
- Example:
(13 * a) * (b / 13 - 1)
- ifa
is zero, there is no need to evaluate(b / 13 - 1)
. - C, C++, and Java use short-circuit evaluation for Boolean operators
&&
and||
.
Introduction
- Expressions are a fundamental means of specifying computations in a programming language.
- To understand expression evaluation, one needs to be familiar with the orders of operator and operand evaluation.
- The essence of imperative languages is the dominant role of assignment statements.
Arithmetic Expressions
- Arithmetic expressions consist of operators, operands, parentheses, and function calls.
- Arithmetic operators: unary, binary, and ternary operators.
Operator Precedence Rules
- The operator precedence rules define the order in which adjacent operators of different precedence levels are evaluated.
- Typical precedence levels: parentheses, unary operators,
**
,*
,/
,+
, and-
.
Operator Associativity Rules
- The operator associativity rules define the order in which adjacent operators with the same precedence level are evaluated.
- Typical associativity rules: left to right, except for
**
which is right to left.
Conditional Expressions
- Conditional expressions:
if-then-else
statements in C-based languages. - Example:
average = (count == 0) ? 0 : sum / count
.
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 operators before evaluating the operand.
Functional Side Effects
- 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.
Overloaded Operators
- Use of an operator for more than one purpose is called operator overloading.
- Example:
&
in C++ as a binary operator for bitwise logical AND operation and as a unary operator for the address of operator.
Type Conversions
- A narrowing conversion is one that converts a value to a type that cannot include all of the values of the original type.
- A widening conversion converts a value to a type that can include at least approximations of all of the values of the original type.
- Explicit type conversion: casting in C-based languages.
Coercion in Expressions
- A mixed-mode expression is one that has operands of different types.
- A coercion is an implicit type conversion.
- In most languages, all numeric types are coerced in expressions, using widening conversions.
Errors in Expressions
- Causes: overflow or underflow, and division by zero.
- These are examples of run-time errors, which are called exceptions.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
This quiz covers the general syntax of assignment statements, assignment operators, and conditional targets in programming languages.