Programming Languages - Assignment
Programming Languages - Assignment
Task.
Choose ten languages and discuss the following in each: precedence level,
associativity rules, conditional evaluation, operator overloading and
coercion evaluation.
Solution.
1. C++:
c) Conditional Evaluation: C++ uses if, else if, and else statements for
conditional evaluation.
e) Coercion Evaluation: C++ performs both implicit and explicit type coercion in
expressions. Implicit conversions are similar to C, while explicit conversions
can be done using type casting operators like static_cast and
reinterpret_cast.
2. Java:
3. Python:
a) Precedence Level: Python has a well-defined precedence hierarchy for
operators i.e. from highest to lowest is : Parentheses: ( ), Exponentiation:
**,Unary Operators: +x, -x, ~x, Multiplication, Division, Modulo: *, /, //,
%,Addition and Subtraction: +, -,Bitwise Shifts: <<, >>,Bitwise AND: &,Bitwise
XOR: ^,Bitwise OR: |, Comparison Operators: ==, !=, >, <, >=, <=,is, is not, in,
not in.
b) Associativity: Python operators follow left-to-right associativity, except for
exponentiation (**) which is right-to-left.
c) Conditional Evaluation: Python uses if, elif, and else statements for
conditional evaluation.
d) Operator Overloading: Python supports operator overloading, allowing
users to redefine operators for custom types.
e) Coercion Evaluation: Python supports dynamic typing, which means that it
performs implicit coercion based on the context of the expression. For
example, it automatically converts between numeric types or concatenates
strings when necessary.
4. JavaScript:
a) Precedence Level: JavaScript has a well-defined precedence hierarchy for
operators. Here is a brief overview of the precedence levels in JavaScript,
from highest to lowest: Grouping: Parentheses () , Member Access: The
dot operator . , Function Call/Creation: The function call () , Postfix
Increment/Decrement , Logical NOT, Multiplication/Division/Remainder,
Addition/Subtraction, Relational Operators, Equality Operators, Strict
Equality Operators, Logical AND, Logical OR, Conditional (Ternary)
Operator, Assignment Operators
b) Associativity: Most JavaScript operators are left-to-right associative e.g.
Arithmetic operators: Addition (+), subtraction (-), multiplication (*), division (/),
remainder (%),(&&),(||),Assignment (=), addition assignment (+=), subtraction
assignment (-=), multiplication assignment (*=), division assignment (/=),
remainder assignment (%=), exponentiation assignment (**=),(&=), (|=),(^=),
except the exponentiation operator and Unary operators which follow right-
to-left.
c) Conditional Evaluation: JavaScript uses if, else if, and else statements
similar to C and Java.
d) Operator Overloading: JavaScript allows limited operator overloading for
built-in types but not for user-defined types.
e) Coercion Evaluation: JavaScript performs both implicit and explicit type
coercion in expressions. Implicit conversions can happen during arithmetic
operations or when comparing values of different types. Explicit
conversions can be done using functions like parseInt() or
parseFloat().
5. Ruby:
a) Precedence Level: Ruby has a well-defined precedence hierarchy for
operators from highest to lowest: ( ),unary +,unary -,!,~,**,*,/,%,+,-
,<<,>>,<,<=,>,>=,==,!=,===,=~,&&,||,=,+=,-=,*=,/=,%=.
b) Associativity: Most Ruby operators are left-to-right associative e.g.
Multiplicative operators (*, /, and %),Addition and subtraction operators (+ and -
),Comparison operators (<, >, <=, >=, ==, !=, ===, =~),Logical operators (&&, ||, and,
or).The right-to-left are assignment operator (=),Unary operators such as the
unary minus (-) and unary plus (+),The exponentiation operator (**)
c) Conditional Evaluation: Ruby uses if, elsif, and else statements for
conditional evaluation.
d) Operator Overloading: Ruby allows operator overloading, enabling users to
redefine operators for custom types.
e) Coercion Evaluation: Ruby performs implicit type coercion in expressions
based on defined rules. It supports a wide range of automatic conversions
between numeric types, as well as string-to-number and vice versa
conversions.
6. Swift:
a) Precedence Level: Swift has a well-defined precedence hierarchy for
operators. From highest to lowest : Postfix Operators, Unary Operators
,multiplicative operators, Additive Operators, Range Operators, Relational
Operators, Equality Operators, Logical AND Operator, Logical OR Operator, Nil-
Coalescing Operator, Ternary Conditional Operator, Assignment Operators
b) Associativity: Most Swift operators are left-to-right associative Addition (+)
and Subtraction (-),Multiplication (*), Division (/), and Remainder (%),Range
operators (... and ..<), Nil-Coalescing (??) operator, Comparison operators (<, >,
<=, >=, ==, !=), Logical AND (&&) and Logical OR (||) operators, Assignment
operators (=, +=, -=, *=, /=, %=).Swift does not have any built-in right-associative
operators.
c) Conditional Evaluation: Swift uses if, else if, and else statements for
conditional evaluation.
d) Operator Overloading: Swift supports operator overloading, allowing users
to redefine operators for custom types.
e) Coercion Evaluation: Swift provides strict type checking and does not
perform implicit type coercion. Explicit type conversions are required using
casting operators, like as, to convert between compatible types.
7. Rust:
a) Precedence Level: Rust has a well-defined precedence hierarchy for
operators. From highest to lowest we have : postfix operators, unary
,multiplicative ,additive ,bitwise shift, relational ,equality ,bitwise logical ,
logical and assignment operators.
b) Associativity: Most Rust operators are left-to-right associative e.g. :+,-,*,/,%
except:=,+=,-=,*=,/=,%= which follow right-to-left evaluation.
c) Conditional Evaluation: Rust uses if, else if, and else statements for
conditional evaluation.
d) Operator Overloading: Rust allows limited operator overloading for built-in
types, but not for user-defined types.
e) Coercion Evaluation: Rust supports explicit type coercion, but implicit
8. PHP:
a) Precedence Level: PHP has a well-defined precedence hierarchy for
operators. From highest to lowest we have: Clone, Array Subscripting,
Function/Method Call, Post-increment/Post-decrement, Pre-increment/Pre-
decrement/Logical NOT/Bitwise NOT, Exponentiation,
Multiplication/Division/modulus, Addition/Subtraction/String Concatenation,
Relational Operators.
b) Associativity: Most PHP operators are left-to-right associative except the
exponentiation operator which is evaluated from right to left.
c) Conditional Evaluation: PHP uses if, elseif, and else statements for
conditional evaluation.
d) Operator Overloading: PHP allows limited operator overloading for built-in
types, but not for user-defined types.
e) Coercion Evaluation: : PHP performs both implicit and explicit type
coercion in expressions. It has a flexible type system and automatically
converts between numeric types, as well as performing string-to-number
and vice versa conversions.
9. MATLAB:
a) Precedence Level: MATLAB has a well-defined precedence hierarchy for
operators. From highest to lowest we have: Parentheses, Exponentiation,
Unary operators ,division and multiplication ,addition and subtraction ,relational
and logical operators.
b) Associativity: Most MATLAB operators are left-to-right associative eg:
assignment ,arithmetic ,relational ,logical and bitwise operators. The
exponential operator follows the right-to-left evaluation.
c) Conditional Evaluation: MATLAB uses if, elseif, and else statements for
conditional evaluation.
d) Operator Overloading: MATLAB supports limited operator overloading for
built-in types but not for user-defined types.
e) Coercion Evaluation: : MATLAB performs implicit type coercion in
expressions based on defined rules. It supports automatic conversions
between compatible numeric types, but explicit conversions using
functions like int8(), double(), etc., are also available.
10. Kotlin:
a) Precedence Level: Kotlin has a well-defined precedence hierarchy for
operators i.e. from highest to lowest : Unary Operators, Arithmetic Operators,
Comparison Operators, Equality Operators, Logical Operators, Assignment
Operators.
b) Associativity: Most Kotlin operators are left-to-right associative e.g.
Arithmetic Operators: + (addition), - (subtraction), * (multiplication), / (division),
and % (modulus) and Assignment Operators: +=, -=, *=, /=, and %=.Right-to-Left
Associativity include: Assignment Operators: +=, -=, *=, /=, and %=.
c) Conditional Evaluation: Kotlin uses if, else if, and else statements for
conditional evaluation, similar to Java.
d) Operator Overloading: Kotlin supports operator overloading, allowing
users to redefine operators for custom types.
e) Coercion Evaluation: Kotlin provides strict type checking and does not
perform implicit type coercion. Explicit type conversions, known as type
casts, are required using the as operator or casting functions.