difference between a statement and expression

A

arnuld

i am unable to understand the difference between a "C++ expression"
and a "C++ statement". this is what i get from C++ Primer:


expression

The smallest unit of computation. An expression consists of one or
more operands and usually an operator. Expressions are evaluated to
produce a result. For example, assuming i and j are ints, then i + j
is an arithmetic addition expression and yields the sum of the two int
values. Expressions are covered in more detail in Chapter 5.


statement

The smallest independent unit in a C++ program. It is analogous to a
sentence in a natural language. Statements in C++ generally end in
semicolons.


..... now that is not clear to me. from Google i get this:
http://www.perlmonks.org/?node_id=101595 (that is for Perl not for C++)
 
S

s_amrollahi

i am unable to understand the difference between a "C++ expression"
and a "C++ statement". this is what i get from C++ Primer:

expression

The smallest unit of computation. An expression consists of one or
more operands and usually an operator. Expressions are evaluated to
produce a result. For example, assuming i and j are ints, then i + j
is an arithmetic addition expression and yields the sum of the two int
values. Expressions are covered in more detail in Chapter 5.

statement

The smallest independent unit in a C++ program. It is analogous to a
sentence in a natural language. Statements in C++ generally end in
semicolons.

.... now that is not clear to me. from Google i get this:http://www.perlmonks.org/?node_id=101595(that is for Perl not for C++)

Of course the C++ Primer by S. Lippman is very good and informative. I
use the 3rd version (1991) of book yet.
Expression is a combination of operators and operands like: 1 + 2 or
cout << "Hello, world!" or max = (a > b) ? a : b.
If you want to use them in a C++ program, you should use ; at the end
of each:
1 + 2;
cout << "Hello, world!";
max = (a > b) ? a : b;
they are statements.
In other words, expression is the smallest unit of computation and
statement is the smallest unit of execution.
the smallest statement in C++ is just a single ;

Regards,
S. Amrollahi
 
R

Robert Bauck Hamar

arnuld said:
i am unable to understand the difference between a "C++ expression"
and a "C++ statement". this is what i get from C++ Primer:

In C++, expressions and statements are syntactic elements of the language.
You don't really need to know the exact difference to write code. The
standard uses three pages of EBNF to describe expressions, and one to
describe statements. EBNF is a language to describe syntaxes, but since you
probably don't know that either, I'm not going to quote the four pages.
expression

The smallest unit of computation. An expression consists of one or
more operands and usually an operator.

Yes. And the operands may themselves be expressions.
Expressions are evaluated to produce a result.

Or to get a side effect. In C++, also the = operator can be part of an
expression (which has the value of the assigned value).
For example, assuming i and j are ints, then i + j
is an arithmetic addition expression and yields the sum of the two int
values. Expressions are covered in more detail in Chapter 5.

An expression has a type and a value (the type may be void, in that case,
there is no value). If you have a variable, let's call it v, of correct
type, you can (unless the type is a class type that prohibits assignment,
or unless the type is void) put "v = " in front of the expression.

Expressions include: Variable name, this, numbers, function calls, addition,
multiplication, new, delete, any grouping of expressions with use of
operators, and more.
statement

The smallest independent unit in a C++ program. It is analogous to a
sentence in a natural language. Statements in C++ generally end in
semicolons.

The most common statement in C++ is an expression followed by a semicolon.
If you have an expression, say

foo(5)

where foo is the name of a function, you can write

foo(5);

to make it a statement. While the function call itself had a type and a
value, the addition of semicolon makes the compiler discard the value.

Other statements include:
loops (for, while, do),
switch,
if, and more.
.... now that is not clear to me. from Google i get this:
http://www.perlmonks.org/?node_id=101595 (that is for Perl not for C++)

This is C++, not perl, and the explanation was not very good.
 
A

arnuld

ok, i got it the only difference between an expression and statement
is the semi-colon. right ?

e.g.

my_function() --> expression
my_function(); --> statement

(1 + 2) * (3 - 7) ---> an expression that itself consists of
expressions
(1 + 2) * (3 - 7); ---> a statement
 
O

osmium

arnuld said:
ok, i got it the only difference between an expression and statement
is the semi-colon. right ?

No. If you really want to have an answer I see no alternative to looking at
the BNF.
There are expressions, expression-statements, and statements. An expression
followed by a semi-colon is an expression-statement.

A for statement includes, as an intrinsic part of its syntax, three
expressions. But for the optional expression part, they would be
expression-statements..

A semi-colon standing by itself is a statement, commonly called a null
statement - but not in the BNF I have seen..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top