Expression In C++

An expression is a combination of operators, constants and variables. An expression may consist of one or more operands, and zero or more operators to produce a value.

Expression

Types Of Expressions

  • Constant expressions

    Constant Expressions consists of only constant values. A constant value is one that doesn't change. It is an expression whose value is determined at the compile-time. It can be composed of integer, character, floating-point, and enumeration constants.

    Example:-

    #include <iostream>
    using namespace std;
     
    int main() {
      int a; // variable declaration.
      a = (6 / 3) + 2; // constant expression
      cout << "The value of a is: " << a; // displaying the value of a.
      return 0;
    }

    Output:-

    The value of a is: 4
  • Integral expressions

    Integral Expressions are those which produce integer results after implementing all the automatic and explicit type conversions.

    Example:-

    #include <iostream>
    using namespace std;
     
    int main() {
      int a = 7, b = 5, c = 9, d; // variable declaration.
      d = a + b + c;
      cout << "The value of d is: " << d; // displaying the value of a.
      return 0;
    }

    Output:-

    The value of d is: 21
  • Float expressions

    Float Expressions are which produce floating point results after implementing all the automatic and explicit type conversions.

    Example:-

    #include <iostream>
    using namespace std;
     
    int main() {
      float a = 7.5, b = 5.5, c = 9.5, d; // variable declaration.
      d = a + b + c;
      cout << "The value of d is: " << d; // displaying the value of d.
      return 0;
    }

    Output:-

    The value of d is: 22.5
  • Pointer expressions

    This particular pointer expression in C++ produces an address value as an output of the program.

    Example:-

    #include <iostream>
    using namespace std;
     
    int main() {
      int X[] = {1, 2, 3, 4, 5}; // array initialization
      int *ptr; // pointer declaration
      ptr = X; // assigning the base address of the array to the pointer ptr
      ptr = ptr + 1; // incrementing the value of the pointer
     
      cout << "Value of the second element of an array: " << *ptr << endl;
      return 0;
    }

    Output:-

    Value of the second element of an array: 2
  • Relational expressions

    A relational expression is an expression that produces a value of type bool, which can be either true or false. It is also known as a boolean expression. When arithmetic expressions are used on both sides of the relational operator, arithmetic expressions are evaluated first, and then their results are compared.

    Example:-

    #include <iostream>
    using namespace std;
     
    int main() {
      int x = 7, y = 4; // variable declaration.
      bool d = x > y; // relational expression
      cout << "The value of d is: " << d; // displaying the value of d.
      return 0;
    }

    Output:-

    The value of d is: 1
  • Logical expressions

    Logical Expressions combine two or more relational expressions and produces bool type results.

    Example:-

    #include <iostream>
    using namespace std;
     
    int main() {
      int x = 7, y = 4, z = 5; // variable declaration.
      bool d = (x > y); // relational expression
      cout << ((x > y) || (x > z));
      return 0;
    }

    Output:-

    1
  • Bitwise expressions

    Bitwise Expressions are used to manipulate data at bit level. They are basically used for testing or shifting bits.

    Example:-

    #include <iostream>
    using namespace std;
     
    int main() {
      int x = 7; // variable declaration.
      cout << (x >> 1) << endl;
      return 0;
    }

    Output:-

    3
  • Special assignment expressions

    Special assignments in C++ can be classified depending on the value of the variable that is assigned

    • Chained assignment expressions - In chained assignment expression assigns the same values more than once by using only one statement

      Example:-

      #include <iostream>
      using namespace std;
       
      int main()
      {
        int x; // variable declaration
        int y; // variable declaration
        x = y = 80; // chained assignment
        cout << "Values of 'x' and 'y' are: " << x << "," << y << endl;
        return 0;
      }

      Output:-

      Values of 'x' and 'y' are : 80,80
    • Embedded assignment expressions - In this embedded assignment expression one expression is enclosed within another assignment expressions

      Example:-

      #include <iostream>
      using namespace std;
       
      int main()
      {
        int x; // variable declaration
        int y; // variable declaration
        x = 10 + (y = 90); // embedded assignment expression
        cout << "Value of 'x' is " << x << endl;
        return 0;
      }

      Output:-

      Value of 'x' is 100
    • Compound Assignment - A compound assignment expression is an expression which is a combination of an assignment operator and bioy operator.

      Example:-

      #include <iostream>
      using namespace std;
       
      int main()
      {
        int x = 5; // variable declaration
        x+= 10;    //Here (+=) is compound assignment operator
        cout << "Value of 'x' is " << x << endl;
        return 0;
      }

      Output:-

      Value of 'x' is 15
How's article quality?

Last updated on -

Page Contents