Introduction
Operator precedence tells the computer which math or logic operation to do first when there are many in one line.
expression with multiple operators, e.g. a + b * c - d / e
int result = 2 + 3 * 4;
int result = (2 + 3) * 4;
bool check = true || false && false;
bool check = (true || false) && false;
#include <iostream> int main() { int a = 5, b = 10, c = 2; int result1 = a + b * c; // 5 + (10*2) = 25 int result2 = (a + b) * c; // (5+10)*2 = 30 bool val1 = true || false && false; // true || (false && false) = true bool val2 = (true || false) && false; // (true || false) && false = false std::cout << "result1: " << result1 << "\n"; std::cout << "result2: " << result2 << "\n"; std::cout << std::boolalpha; std::cout << "val1: " << val1 << "\n"; std::cout << "val2: " << val2 << "\n"; return 0; }
3 + 4 * 5?int a = 2, b = 3, c = 4; int result = a + b * c / a - b; std::cout << result;
int x = 10; int y = 5; int z = x + y * / 2; std::cout << z;
x + y * / 2 has two operators (*) and (/) together without an operand, which is invalid syntax.int result = 10 - 3 * 2 + 4 / 2;. How can you change it using parentheses to make addition happen before multiplication and division?(2 + 4) first, so addition happens before multiplication and division.Pass the test and this pattern moves unknown