Understanding C++ Expressions and Statements

Understanding C++ Expressions and Statements

Expressions in C++

Expressions are the most fundamental building blocks in programming. In C++, an expression is defined as:

A sequence of operators and operands that specifies a computation.

Expressions compute values based on the operands provided. For example, consider the following expressions:

  • Literal expressions: The number 34 is an expression whose value is simply 34

  • Variable expressions: A variable such as favoriteNumber is also an expression since its value is the value stored in the variable

  • Mathematical expressions: Expressions such as 1.5 + 2.8 perform computations and return a result

  • Comparison expressions: An expression like A > B returns a boolean value (true or false)

  • Assignment expressions: The expression A = B assigns the value of B to A. Even though assignment modifies a variable, it is still an expression because it evaluates to the assigned value

Expressions can be combined using various operators in C++ to perform complex computations.

Statements in C++

A statement is a complete line of code that performs an action. In C++, statements are usually terminated with a semicolon (;).

Since statements often contain expressions, you can think of them as higher-level instructions composed of multiple expressions. C++ provides many types of statements, including:

  1. Declaration statements: Used to declare variables. Example:

     int x;
    
  2. Assignment statements: Used to assign values to variables. Example:

     favoriteNumber = 12;
    
  3. Expression statements: These are expressions followed by a semicolon. Example:

     2 + 3;
    

    Although this statement is syntactically correct, it is useless since the result is not stored or used.

  4. Compound statements: A block of statements enclosed in curly braces {}. Example:

     {
         int a = 5;
         int b = 10;
         int sum = a + b;
     }
    
  5. Selection statements: Control flow using conditions, such as if statements. Example:

     if (A > B) {
         std::cout << "A is greater than B";
     }
    
  6. Iteration statements: Used for loops, such as for, while, and do-while. Example:

     for (int i = 0; i < 5; i++) {
         std::cout << i << " ";
     }
    
  7. Jump statements: Used for altering control flow, such as break, continue, return, and goto.

     return 0;
    
  8. Try blocks: Used for handling exceptions.

     try {
         throw std::runtime_error("An error occurred");
     } catch (const std::exception &e) {
         std::cout << e.what();
     }
    
  9. Null statements: A statement consisting of just a semicolon (;). It performs no action but can be useful in specific scenarios. Example:

     ;
    

Operators in C++

C++ has a rich set of operators that can be used to build up expressions. Most C++ operators are binary operators, meaning they operate on two operands. For example, the multiplication operator (*).

However, some are unary operators, meaning they operate on only one operand. An example of this is the unary minus operator (-), which simply negates its operand.

There is even a ternary operator that operates on three operands. This is the conditional operator (?:).

The common operators in C++ can be grouped into several categories:

  • Assignment operators: Used to modify the value of an object by assigning a new value to it

  • Arithmetic operators: Used to perform mathematical operations on operands

  • Increment and decrement operators: These are interesting because part of them works like an assignment and part like arithmetic. They are commonly used

  • Relational (comparison) operators: Allow you to compare the values of two objects. These include ==, !=, <, >, <=, >=

  • Logical operators: Used to test for logical or Boolean conditions. These include logical NOT (!), AND (&&), and OR (||)

  • Member access operators: For example, the array subscript operator ([]) allows access to a specific element in an array. There are others used with objects and pointers, which will be covered later in the course

  • Other operators: These include operators that do not fit well into the above categories


Discover hands-on programming tutorials and resources! Visit my website: Fullstack Dev