Operator Associativity in C++
Here are the implementation of Operator Associativity in C++ language:
#include <iostream>
using namespace std;
int main() {
// Introduction to Operator Associativity
// No code example for introduction
// Operator Associativity in Arithmetic Operators
int result_arithmetic = 5 + 3 * 2; // result will be 11 (5 + (3 * 2))
// Operator Associativity in Relational Operators
int condition_relational = 2 < 5 == 1; // condition will be true ((2 < 5) == 1)
// Operator Associativity in Logical Operators
int bool_result_logical = 1 || 0 && 1; // bool_result will be true (1 || (0 && 1))
// Operator Associativity in Assignment Operators
int a = 0, b = 0;
a = b = 5; // Both a and b will be assigned the value 5 (b = 5, then a = b)
// Operator Associativity in Bitwise Operators
int result_bitwise = 1 | 2 & 3; // result will be 3 (1 | (2 & 3))
// Operator Associativity in Conditional (Ternary) Operator
int max_value = (a > b) ? a : b; // max_value will be the greater of a and b
// Operator Associativity in Unary Operators
int result_unary = -(-5); // result will be 5 (-(-5))
// Print results
cout << "Arithmetic Operators: " << result_arithmetic << endl;
cout << "Relational Operators: " << condition_relational << endl;
cout << "Logical Operators: " << bool_result_logical << endl;
cout << "Assignment Operators: " << a << " " << b << endl;
cout << "Bitwise Operators: " << result_bitwise << endl;
cout << "Ternary Operator: " << max_value << endl;
cout << "Unary Operators: " << result_unary << endl;
return 0;
}
Output
Arithmetic Operators: 11 Relational Operators: 1 Logical Operators: 1 Assignment Operators: 5 5 Bitwise Operators: 3 Ternary Operator: 5 Unary Operators: 5
Operator Associativity in Programming
Operator associative refers to the order in which operators of the same precedence are used in a word. In a programming language, it is important to understand the interactions between operators to properly define and test expressions. In this article, we will discuss operator associativity in programming.
Table of Content
- Operator Associativity in Arithmetic Operators
- Operator Associativity in Relational Operators
- Operator Associativity in Logical Operators
- Operator Associativity in Assignment Operators
- Operator Associativity in Bitwise Operators
- Operator Associativity in Conditional (Ternary) Operator
- Operator Associativity in Unary Operators
- Operator Associativity in C
- Operator Associativity in C++
- Operator Associativity in Java
- Operator Associativity in Python
- Operator Associativity in C#
- Operator Associativity in Javascript
Here is a table illustrating the Operator Associativity in Programming:
Operators | Associativity |
---|---|
Arithmetic | Left to right |
Relational | Left to right |
Logical | Left to Right |
Assignment | Right to Left |
Bitwise | Left to Right |
Conditional (Ternary) | Right to Left |
Unary | Right to Left |