CPP Statements and Expressions

Working with Expressions & Statements in C++

  • A statement controls the sequence of execution.
  • The Statement uses expression to evaluate a resulting outcome.
  • The Statement must end with a semicolon.
  • Whitespace such as tabs, spaces, and new lines is generally ignored in statements.
  • Blocks with statements begins with an opening brace ({) and ends with a closing brace (}) and is not ended with a semicolon.
  • In C++ will anything that evaluates to a value be an expression. Thus, the statement 4+9; returns the value 13, so it is an expression.
  • Expressions that evaluate mathematically to zero return false; all others return true..
  • You can consider all expressions as statements in C++. For example :
    Source code Result
    #include <iostream>
    int main() {
      using std::cout;
      using std::endl;
    
      int a=0, b=0, c=0, d=25;
      cout << "a: " << a << " b: " << b;
      cout << " c: " << c << " d: " << d << endl;
      a = 3;
      b = 9;
      d = c = a+b;
      cout << "a: " << a << " b: " << b;
      cout << " c: " << c << " d: " << d << endl;
      return 0;
    }
    a: 0 b: 0 c: 0 d: 25
    a: 3 b: 9 c: 12 d: 12
    

    You can download this example here (needed tools can be found in the right menu on this page).

  • In expression we are using mathematical operators which can be:
    Operator Description Example Result
    + Addition res= 20+3; 23
    - Subtraction res= 20-3; 17
    * Multiplication res= 20*3; 60
    / Division res= 20/3; 6
    % Modulus res= 20/3; 2
© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.