### expression

I studied in reference book that Any arrangement of variables, constants, and operators that specifies a computation is called an expression. I also studied that Even single variables and constants, like alpha and 37, are considered to be expressions.

 cout << "the result is" << number;

In the above statement the variable number is the expression?
number is an expression. See:
https://en.cppreference.com/w/cpp/language/expressions

"the result is" is also an expression and perhaps maybe surprising:

cout << "the result is" << number

is also an expression as it returns a type/value (type std::ostream) - not used here.
Last edited on
Basically anything that gives you a value that you can use or print is an expression. Function calls are always expressions even if the return type is void.
> cout << "the result is" << number;

cout - id-expression (primary expression)

"the result is" - literal (primary expression)

cout << "the result is" - subexpression

number - id-expression (primary expression)

cout << "the result is" << number - full-expression (discarded-value expression)

cout << "the result is" << number; - expression statement
JLBorges wrote:
cout << "the result is" - subexpression

A subexpression is essentially an expression that is part of another expression so cout, "the result is" and number are also subexpressions.
Last edited on
What is the difference between statements and expressions?
hassan236 wrote:
What is the difference between statements and expressions?

The most important difference is that an expression has a type and will give you a value of that type unless the type is void. A statement does not have a type and does not result in a value.

Examples of expressions:
 x+9
 std::gcd(1, 100)
 foo()

Any expression can be turned into a statement by putting a semicolon after it.

This is an expression:
 foo()

This is a statement (that contains an expression):
 foo();

There are also other kind of statements, some of which does not end in a semicolon.

If statements:
 1234 if (cond) { doSomething(); }

Loop statements:
 1234 while (x < 100) { std::cout << ++x << "\n"; }

Variable declaration statements:
 int x = 5;

Note that some of these statements contain other statements and/or expressions.

In some places you are expected to use statements and in other places you are expected to use expressions.

The body of a function contains a list of statements. If you instead write a list of expressions (without semicolons) you would get a compilation error.
 123456 void bar() { 5+9 std::gcd(1, 100) // ERROR! foo() }

The arguments that you pass to functions should be expressions. You cannot write statements instead. That won't work.
 12 // This is an error no matter how someFunction is defined: someFunction(foo();, if(cond){doSomething();});

Last edited on
Statements are fragments of the C++ program that are executed in sequence. An expression statement is an expression followed by a semicolon. See:
https://en.cppreference.com/w/cpp/language/statements
cout is also the expression? how?
Last edited on
As in mine and others previous posts,

 cout << "the result is" << number

is an expression - as cout returns a type and a value.

 cout << "the result is" << number;

is a statement (with the ;) where the returned value from cout expression is not used. Only the expression side-effects are utilised here (output to the console).
Let's say you using a C++ container like a std::vector, you can overload the insertion operator (<<) so writing an output statement for a container is as simple as outputting a built-in C++ type like an int.

 1234567891011121314151617181920212223242526272829 #include #include // 1D template std::ostream& operator<<(std::ostream& os, const std::vector& v) { for ( auto const& x : v ) { os << x << ' '; } return os; } // 2D, which ends up calling the 1D overload as well template std::ostream& operator<<(std::ostream& os, const std::vector>& v) { for ( auto const& x : v ) { os << x << '\n'; } return os; } int main() { std::vector vec1D { 1, 2, 3, 4, 5 }; std::cout << vec1D << "\n\n"; std::vector> vec2D { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; std::cout << vec2D << '\n'; }

Returning the std::ostream object in the overload functions chains up the output for proper functioning of the insertion operator.

How the 2 container objects at lines 22 & 26 are instantiated requires C++17 or later, the rest of the code is C++11.

A 3D container will use the 1D and 2D overloads, no need to write an overload for 3D unless it requires different output handling (C++11 or later):
 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 #include #include template std::ostream& operator<<(std::ostream&, const std::vector&); template std::ostream& operator<<(std::ostream&, const std::vector>&); int main() { std::cout << "Creating a 3-dimensional vector, enter depth size: "; int depth; std::cin >> depth; std::cout << "Enter row size: "; int row; std::cin >> row; std::cout << "Enter column size: "; int col; std::cin >> col; std::cout << "\n"; // create a 3 dimensional int vector with known dimensions using std::vector; vector>> aVector(depth, vector>(row, vector(col, 0))); // let's display the initial 3D vector std::cout << aVector << '\n'; // initialize the vector with some values for (int depth_loop = 0; depth_loop < depth; depth_loop++) { for (int row_loop = 0; row_loop < row; row_loop++) { for (int col_loop = 0; col_loop < col; col_loop++) { aVector[depth_loop][row_loop][col_loop] = (((depth_loop + 1) * 100) + ((row_loop + 1) * 10) + col_loop + 1); } } } // let's display the filled 3D vector std::cout << aVector << '\n'; } template std::ostream& operator<<(std::ostream& os, const std::vector& v) { for (auto const& x : v) { os << x << ' '; } return os; } template std::ostream& operator<<(std::ostream& os, const std::vector>& v) { for (auto const& x : v) { os << x << '\n'; } return os; }

You can overload the extraction operator (>>) as well.

Dealing with a custom class is another use for writing I/O stream overloads.