arnuld wrote:
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.
--
rbh