difference between a statement and expression

Discussion in 'C++' started by arnuld, Jun 26, 2007.

  1. arnuld

    arnuld Guest

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


    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.


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

    ..... 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++)
    arnuld, Jun 26, 2007
    1. Advertisements

  2. arnuld

    s_amrollahi Guest

    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 ;

    S. Amrollahi
    s_amrollahi, Jun 26, 2007
    1. Advertisements

  3. 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.
    Yes. And the operands may themselves be expressions.
    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).
    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.
    The most common statement in C++ is an expression followed by a semicolon.
    If you have an expression, say


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


    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),
    if, and more.
    This is C++, not perl, and the explanation was not very good.
    Robert Bauck Hamar, Jun 26, 2007
  4. arnuld

    arnuld Guest

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


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

    (1 + 2) * (3 - 7) ---> an expression that itself consists of
    (1 + 2) * (3 - 7); ---> a statement
    arnuld, Jun 26, 2007
  5. arnuld

    osmium Guest

    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

    A semi-colon standing by itself is a statement, commonly called a null
    statement - but not in the BNF I have seen..
    osmium, Jun 26, 2007
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.