0% found this document useful (0 votes)
38 views

Programming Languages - Assignment

The document summarizes the precedence levels, associativity rules, conditional evaluation, operator overloading, and coercion evaluation for 10 programming languages: C++, Java, Python, JavaScript, Ruby, Swift, Rust, and PHP. For each language, the author discusses the precedence hierarchy from highest to lowest, whether operators are left or right associative, how conditionals are evaluated, if operator overloading is supported, and how type coercion works.

Uploaded by

Peter Ngari
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Programming Languages - Assignment

The document summarizes the precedence levels, associativity rules, conditional evaluation, operator overloading, and coercion evaluation for 10 programming languages: C++, Java, Python, JavaScript, Ruby, Swift, Rust, and PHP. For each language, the author discusses the precedence hierarchy from highest to lowest, whether operators are left or right associative, how conditionals are evaluated, if operator overloading is supported, and how type coercion works.

Uploaded by

Peter Ngari
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

ASSIGNMENT 1

Name: Ngari Peter N.


Registration Number: C026-01-0910/2022
Unit: Programming Languages.

Task.

Choose ten languages and discuss the following in each: precedence level,
associativity rules, conditional evaluation, operator overloading and
coercion evaluation.

Solution.
1. C++:

a) Precedence Level: the precedence levels of some common operators in


C++ (from highest to lowest):Postfix operators ,Unary operators ,Multiplicative
operators ,Additive operators ,Shift operators ,Relational and comparison
operators ,Bitwise AND operator ,Bitwise XOR operator ,Bitwise OR operator,
Logical AND operator, Logical OR operator, Conditional operator, Assignment
operators , Comma operator.

b) Associativity: Similar to C, C++ has left-to-right associativity for most


operators e.g. Assignment operators (=, +=, -=, *=, /=, etc.), function call (),
member access . and ->, array subscript [], post-increment ++ and post-
decrement --. For right-to-left associativity we have: Unary operators (unary
minus -, unary plus +, logical negation !, bitwise complement ~, dereference *,
address-of &, pre-increment ++, and pre-decrement --), assignment
operators (=, +=, -=, *=, /=, etc.), exponentiation ^ (not present in C++),
conditional operator ?:

c) Conditional Evaluation: C++ uses if, else if, and else statements for
conditional evaluation.

d) Operator Overloading: C++ allows operator overloading, enabling users to


redefine operators for custom types.

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:

a. Precedence Level: Java has a well-defined precedence hierarchy, similar to


C++ i.e. ++ (from highest to lowest):Postfix operators ,Unary operators
,Multiplicative operators ,Additive operators ,Shift operators ,Relational
and comparison operators ,Bitwise AND operator ,Bitwise XOR operator
,Bitwise OR operator, Logical AND operator, Logical OR operator,
Conditional operator, Assignment operators/ternary operator.

b. Associativity: Java also has left-to-right associativity for almost all


operators except =, +=, -=,++, --
c. Conditional Evaluation: Java uses if, else if, and else statements for
conditional evaluation.
d. Operator Overloading: Java does not support operator overloading, except
for string concatenation.
e. Coercion Evaluation: Java performs both implicit and explicit type coercion
in expressions. Implicit conversions occur when the types are compatible
or there is a widening conversion. Explicit conversions are done using
casting, such as (type) expression

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.

You might also like