Page Content

Tutorials

Expressions in C++: How Code Calculates Values

Expressions in C++

Expressions are essential program building blocks in C++. When evaluated, it is characterized as a series of operators and operands (which may be literals or variables) that produce a value or result. A single variable or constant can be regarded as an expression. An example of an expression is 6 * 7.

Components of Expressions

The components of expressions are:

Operands: This is what the operators do to these values or objects.

Operators: These unique symbols control the action that an expression takes. A large collection of built-in operators is offered by C++. The number of operands an operator takes can be used to categorise them:

  • Some examples of unary operators that act on a single operand include & for address-of, * for dereference,! for logical NOT, ++ for increment, — for decrement, sizeof for object/type size, ~ for bitwise NOT, unary +, and unary -.
  • e.g., +, -, *, /, %, ==,!=, <, >, <=, >=, &&, ||, &, |, ^, <<, >>, =, +=, -=, etc. Binary operators act on two operands.
  • The conditional operator?: is the only ternary operator where three operands are accepted.
  • There is no limit on how many operands the function call operator () can accept.

Evaluation of Expressions

The precedence and associativity of a compound expression dictate the critical order in which its components are evaluated.

  • Precedence: Operators with greater precedence are executed before those with lower precedence and are clustered closer together. Multiplication (*), for instance, is carried out before addition (+) in the equation a = b + c * d due to its greater precedence. The default priority can be expressly overridden by using brackets ().
  • Associativity: An operand is grouped with the operator to its left or right based on associativity if an expression contains operators of equal precedence. Most operators are associative from left to right, but others are right-to-left, such as the conditional operator and assignment operators.
  • Order of Evaluation: In most situations, operands are evaluated before the operator itself, but the compiler can evaluate them in any order. But the operators comma (,), conditional (? ), logical AND (&&), and logical OR (||) ensure a particular order of evaluation. Short-circuiting operators are so named because they do not evaluate the second operand if the first operand is sufficient to determine the overall result.

You can also read What Is C Operator Precedence And Associativity?

Expressions vs Statements

Differentiating between statements and phrases is crucial:

  • A calculation and its outcome are specified by an expression.
  • An action is specified by a statement, which is a code segment that usually ends with a semicolon.
  • When a semicolon is placed after an expression, it becomes an expression statement. Unless the expression has a side effect (e.g., carrying out I/O or assigning a new value to a variable), such a statement evaluates the expression and typically discards the result. A null statement is just “do nothing” followed by a semicolon.
Agarapu Geetha
Agarapu Geetha
My name is Agarapu Geetha, a B.Com graduate with a strong passion for technology and innovation. I work as a content writer at Govindhtech, where I dedicate myself to exploring and publishing the latest updates in the world of tech.
Index