Operator Precedence in Arithmetic Operators
Arithmetic operators follow the standard precedence rules that are used in mathematics. The precedence of arithmetic operators from highest to lowest is as follows:
- Parentheses ()
- Unary plus and minus + –
- Multiplication, division, and modulus * / %
- Addition and subtraction + –
Below is the implementation of Operator Precedence in Arithmetic Operators:
#include <iostream>
using namespace std;
int main()
{
int a = 10;
int b = 5;
int c = 2;
// Using arithmetic operators
// result1 is 20 because multiplication has higher
// precedence than addition
int result1 = a + b * c;
cout << "a + b * c = " << result1 << endl;
// result2 is 30 because parentheses change the order of
// operations
int result2 = (a + b) * c;
cout << "(a + b) * c = " << result2 << endl;
// result3 is 8 because division has higher precedence
// than subtraction
int result3 = a - b / c;
cout << "a - b / c = " << result3 << endl;
// result4 is 2 because parentheses
// change the order of operations
int result4 = (a - b) / c;
cout << "(a - b) / c = " << result4 << endl;
return 0;
}
// Note: the difference in output between the programs is due to the
//different precedence rules and how parentheses are used to override those
// rules in each language.
public class Main {
// Main method
public static void main(String[] args) {
// Variable declarations
int a = 10;
int b = 5;
int c = 2;
// Using arithmetic operators
// result1 is 20 because multiplication has higher precedence than addition
int result1 = a + b * c;
System.out.println("a + b * c = " + result1);
// result2 is 30 because parentheses change the order of operations
int result2 = (a + b) * c;
System.out.println("(a + b) * c = " + result2);
// result3 is 8 because division has higher precedence than subtraction
int result3 = a - b / c;
System.out.println("a - b / c = " + result3);
// result4 is 2 because parentheses change the order of operations
int result4 = (a - b) / c;
System.out.println("(a - b) / c = " + result4);
}
}
// Note: the difference in output between the programs is due to the
//different precedence rules and how parentheses are used to override those
// rules in each language.
# Initialize variables
a = 10
b = 5
c = 2
# Using arithmetic operators
# result1 is 20 because multiplication has higher
# precedence than addition
result1 = a + b * c
print("a + b * c =", result1)
# result2 is 30 because parentheses change the order of
# operations
result2 = (a + b) * c
print("(a + b) * c =", result2)
# result3 is 8 because division has higher precedence
# than subtraction, and in Python, division (/) returns
# a float result by default
result3 = a - b / c
print("a - b / c =", result3)
# result4 is 2 because parentheses
# change the order of operations
result4 = (a - b) / c
print("(a - b) / c =", result4)
# Note: The output may vary depending on the syntax used in the C++ code.
# In Python, division (/) returns a float result by default, which may lead to
# different results compared to C++ where integer division is used by default.
// Main function
function main() {
let a = 10;
let b = 5;
let c = 2;
// Using arithmetic operators
// result1 is 20 because multiplication has higher
// precedence than addition
let result1 = a + b * c;
console.log("a + b * c = " + result1);
// result2 is 30 because parentheses change the order of
// operations
let result2 = (a + b) * c;
console.log("(a + b) * c = " + result2);
// result3 is 8 because division has higher precedence
// than subtraction
let result3 = a - b / c;
console.log("a - b / c = " + result3);
// result4 is 2 because parentheses
// change the order of operations
let result4 = (a - b) / c;
console.log("(a - b) / c = " + result4);
}
// Call the main function to execute the example usage
main();
// Note: the difference in output between the programs is due to the
//different precedence rules and how parentheses are used to override those
// rules in each language.
Output
a + b * c: 20 (a + b) * c: 30 a - b / c: 8 (a - b) / c: 2
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