what is a statement and expression?

Discussion in 'C Programming' started by vlsidesign, Dec 7, 2007.

  1. vlsidesign

    vlsidesign Guest

    Some of the words used in programming books are foggy to me (maybe I
    should revisit English one day). I looked up some terms in the
    dictionary, which seem to help. In particular, statement and
    expression. Here is what I gathered from a dictionary:
    A statement is an expression in words.
    In math, an expression is a collection of symbols expressing a
    quantity.

    In programming, is a statement and expression similar things? How
    would you define an expression in programming, code that evaluates to
    something. Control structures and loops seem to be different and in
    fact contain statements and/or expressions. Thanks for any insight.
     
    vlsidesign, Dec 7, 2007
    #1
    1. Advertising

  2. In article <> vlsidesign <> writes:
    > Some of the words used in programming books are foggy to me (maybe I
    > should revisit English one day).


    Using a dictionary can be confusing, especially if is a dictionary that
    is not technical.

    > In programming, is a statement and expression similar things?


    Depends on the programming language involved. To know what the meaning
    of those words actually is, you need to read the standard for the
    particular language, where such terms are actually defined. In C,
    every expression can be used as a statement, but not the other way
    around. In other programming languages, they are quite different.
    --
    dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
    home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
     
    Dik T. Winter, Dec 7, 2007
    #2
    1. Advertising

  3. vlsidesign

    Jack Klein Guest

    On Thu, 6 Dec 2007 18:37:51 -0800 (PST), vlsidesign
    <> wrote in comp.lang.c:

    > Some of the words used in programming books are foggy to me (maybe I
    > should revisit English one day). I looked up some terms in the
    > dictionary, which seem to help. In particular, statement and
    > expression. Here is what I gathered from a dictionary:
    > A statement is an expression in words.
    > In math, an expression is a collection of symbols expressing a
    > quantity.
    >
    > In programming, is a statement and expression similar things? How
    > would you define an expression in programming, code that evaluates to
    > something. Control structures and loops seem to be different and in
    > fact contain statements and/or expressions. Thanks for any insight.


    In C, an expression is a sequence of operators and or operands that
    yields a value, or a call to a function with a void return type, which
    does not yield a value. An expression can stand alone or be a
    subexpression.

    2 + 2

    ....is an expression, that happens to yield a value of 4 with type int.

    Given a previous definition "int x, y = 0;", then...

    x = 2 + 2 + y

    ....is an expression of which (2 + 2) is a subexpression, and (2 + 2 +
    y) is a subexpression. This could be a subexpression of a larger and
    more complex expression.

    An expression becomes a statement when terminated by the statement
    termination token, ';'. So:

    x = 2 + 2 + y;

    ....is a statement.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Dec 7, 2007
    #3
  4. vlsidesign

    vlsidesign Guest

    On Dec 6, 6:53 pm, "Dik T. Winter" <> wrote:
    > In article <> vlsidesign <> writes:
    > > Some of the words used in programming books are foggy to me (maybe I
    > > should revisit English one day).

    >
    > Using a dictionary can be confusing, especially if is a dictionary that
    > is not technical.
    >
    > > In programming, is a statement and expression similar things?

    >
    > Depends on the programming language involved. To know what the meaning
    > of those words actually is, you need to read the standard for the
    > particular language, where such terms are actually defined. In C,
    > every expression can be used as a statement, but not the other way
    > around. In other programming languages, they are quite different.
    > --
    > dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
    > home: bovenover 215, 1025 jn amsterdam, nederland;http://www.cwi.nl/~dik/


    I did some hunting and found a link to a C standard pdf file:
    http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf

    "A statement specifies an action to be performed. Except as indicated,
    statements are
    executed in sequence." From above document, page 131

    "An expression is a sequence of operators and operands that specifies
    computation of a
    value, or that designates an object or a function, or that generates
    side effects, or that
    performs a combination thereof." From above document file, page 67


    It seems from some of the posts and the document, that you can have
    different types of statements, of which an "expression-statement" is
    one of them. Jack Klein also shares some good info below too, thanks.
     
    vlsidesign, Dec 7, 2007
    #4
  5. vlsidesign

    Chris Dollin Guest

    vlsidesign wrote:

    > Some of the words used in programming books are foggy to me (maybe I
    > should revisit English one day). I looked up some terms in the
    > dictionary, which seem to help. In particular, statement and
    > expression. Here is what I gathered from a dictionary:
    > A statement is an expression in words.
    > In math, an expression is a collection of symbols expressing a
    > quantity.
    >
    > In programming, is a statement and expression similar things? How
    > would you define an expression in programming, code that evaluates to
    > something. Control structures and loops seem to be different and in
    > fact contain statements and/or expressions. Thanks for any insight.


    As a very broad summary: expressions are things that describe a value;
    statements are things that /do/ something [1].

    Complications arise because in C some expressions involve statements,
    because expressions like F(X) call the function F, which has got
    statements inside it [2], and because assignment `L = R`, one of the
    two definitive "do somethings", is syntactically an expression, and
    because any C expression turns into a statement when you stick a
    semicolon at the end, which can give you statements that don't do
    anything, like `17;` or `1 + 2`. (The other "do something" is to
    produce output/read input, which in C is done by magic system-provided
    functions, giving us expressions that "do" things ...)

    There's a syntactic distinction between statements and expressions [3],
    but that only turns out to be interesting when its inconvenient.

    [1] In most programming languages. There are other uses for "statement"
    that have it /assert/ things, but I'll quietly pass over them.

    [2] C's ancestor BCPL allowed embedding statements directly in expressions
    using the VALOF and RESULTIS constructs, but this dead handy syntax
    evaporated sometime during C's evolution.

    [3] In C. Some languages don't make the distinction in the syntax; everything
    (including loops and declarations) is an expression {4}. There is no
    mangling of distinctions so bizarre that every language designer
    avoids it.

    --
    Chris "{4} *cough* Spice" Dollin

    Hewlett-Packard Limited registered office: Cain Road, Bracknell,
    registered no: 690597 England Berks RG12 1HN
     
    Chris Dollin, Dec 7, 2007
    #5
  6. vlsidesign

    vlsidesign Guest

    On Dec 7, 3:10 am, Chris Dollin <> wrote:
    > vlsidesign wrote:
    > > Some of the words used in programming books are foggy to me (maybe I
    > > should revisit English one day). I looked up some terms in the
    > > dictionary, which seem to help. In particular, statement and
    > > expression.

    <snip>
    >
    > As a very broad summary: expressions are things that describe a value;
    > statements are things that /do/ something [1].
    >
    > Complications arise because in C some expressions involve statements,
    > because expressions like F(X) call the function F, which has got
    > statements inside it [2], and because assignment `L = R`, one of the
    > two definitive "do somethings", is syntactically an expression, and
    > because any C expression turns into a statement when you stick a
    > semicolon at the end, which can give you statements that don't do
    > anything, like `17;` or `1 + 2`. (The other "do something" is to
    > produce output/read input, which in C is done by magic system-provided
    > functions, giving us expressions that "do" things ...)
    >
    > There's a syntactic distinction between statements and expressions [3],
    > but that only turns out to be interesting when its inconvenient.

    <snip>
    > --
    > Chris "{4} *cough* Spice" Dollin
    >
    > Hewlett-Packard Limited registered office: Cain Road, Bracknell,
    > registered no: 690597 England Berks RG12 1HN


    Good stuff, thanks :)
     
    vlsidesign, Dec 7, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    833
  2. Jay McGavren
    Replies:
    11
    Views:
    1,181
    Alan Krueger
    Jan 16, 2006
  3. tedsuzman
    Replies:
    2
    Views:
    7,163
    Michel Claveau, résurectionné d'outre-bombe inform
    Jul 21, 2004
  4. Ted
    Replies:
    1
    Views:
    493
    Duncan Booth
    Jul 22, 2004
  5. Replies:
    21
    Views:
    1,086
    Giannis Papadopoulos
    Aug 2, 2005
Loading...

Share This Page