Operator Precedence in Assignment Operators
Assignment operators are used to assign values to variables. The assignment operator = has the lowest precedence.
Below is the implementation of Operator Precedence in Assignment Operators:
#include <iostream>
using namespace std;
int main()
{
int a = 5;
int b = 10;
// Using assignment operators
a = b; // a is now 10
cout << "a = b: " << a << endl;
a += b; // a is now 20
cout << "a += b: " << a << endl;
a -= b; // a is now 10
cout << "a -= b: " << a << endl;
a *= b; // a is now 100
cout << "a *= b: " << a << endl;
a /= b; // a is now 10
cout << "a /= b: " << a << endl;
a %= b; // a is now 0
cout << "a %= b: " << a << endl;
return 0;
}
public class Main {
public static void main(String[] args) {
int a = 5;
int b = 10;
// Using assignment operators
a = b; // a is now 10
System.out.println("a = b: " + a);
a += b; // a is now 20
System.out.println("a += b: " + a);
a -= b; // a is now 10
System.out.println("a -= b: " + a);
a *= b; // a is now 100
System.out.println("a *= b: " + a);
a /= b; // a is now 10
System.out.println("a /= b: " + a);
a %= b; // a is now 0
System.out.println("a %= b: " + a);
}
}
# Initialize variables
a = 5
b = 10
# Using assignment operators
a = b # a is now 10
print("a = b: ", a)
a += b # a is now 20
print("a += b: ", a)
a -= b # a is now 10
print("a -= b: ", a)
a *= b # a is now 100
print("a *= b: ", a)
a /= b # a is now 10
print("a /= b: ", a)
a %= b # a is now 0
print("a %= b: ", a)
Output
a = b: 10 a += b: 20 a -= b: 10 a *= b: 100 a /= b: 10 a %= b: 0
Operator Precedence in Programming
Operator Precedence, also known as operator hierarchy, is a set of rules that controls the order in which operations are performed in an expression without parentheses. It is a fundamental concept in programming languages and is crucial for writing correct and efficient code.
Table of Content
- What is Operator Precedence?
- Operator Precedence in Arithmetic Operators
- Operator Precedence in Relational Operators
- Operator Precedence in Logical Operators
- Operator Precedence in Assignment Operators
- Operator Precedence in Bitwise Operators
- Operator Precedence in Conditional (Ternary) Operator
- Operator Precedence in Unary Operators
- Operator Precedence in Member Access Operators
- Operator Precedence in Type Cast Operators
- Importance of Operator Precedence
Contact Us