what is a statement and expression?

V

vlsidesign

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.
 
D

Dik T. Winter

> 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.
 
J

Jack Klein

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
 
V

vlsidesign

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


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.

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.
 
C

Chris Dollin

vlsidesign said:
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.
 
V

vlsidesign

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.
--
Chris "{4} *cough* Spice" Dollin

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Good stuff, thanks :)
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top