473,509 Members | 2,946 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

difference between a statement and expression

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++)

Jun 26 '07 #1
4 9448
On Jun 26, 11:36 am, arnuld <geek.arn...@gmail.comwrote:
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

Jun 26 '07 #2
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
Jun 26 '07 #3
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

Jun 26 '07 #4
"arnuld" wrote:
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..
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

Jun 26 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
2252
by: brainsucker | last post by:
Hi My name is Juan Carlos Rodrigo, and I love Python. It is the most impressive and usefull language that I have ever seen. I am studing, at http://www.uoc.edu, an Information Technology...
19
14852
by: Jason | last post by:
Hello, could someone explain the difference to me inbetween: *ptr++ and ++*ptr Thankx a lot.. Jason.
15
2781
by: Nerox | last post by:
Hi, If i write: #include <stdio.h> int foo(int); int main(void){ int a = 3; foo(a); }
11
2173
by: Scott C. Reynolds | last post by:
In VB6 you could do a SELECT CASE that would evaluate each case for truth and execute those statements, such as: SELECT CASE True case x > y: dosomestuff() case x = 5: dosomestuff() case y >...
2
3169
by: Geoffrey KRETZ | last post by:
Hello, I'm wondering if the following behaviour is the correct one for PostGreSQL (7.4 on UNIX). I've a table temp_tab with 5 fields (f1,f2,f3,...),and I'm a launching the following request :...
15
4823
by: Matt | last post by:
Hi There, Can anyone explain me the real advantages of (other than syntax) lambda expressions over anonymous delegates? advantage for one over the other. delegate int F(int a); F fLambda = a...
18
7929
by: dspfun | last post by:
Hi! The words "expression" and "statement" are often used in C99 and C- textbooks, however, I am not sure of the clear defintion of these words with respect to C. Can somebody provide a sharp...
11
1508
by: Daniel Klein | last post by:
Given the functions: function first() { echo "first\n"; return true; } function second() { echo "second\n"; return true; }
10
3412
by: Ahmad Humayun | last post by:
Whats the difference between: char str1 = "wxyz"; char* str2 = "abcd"; I can do this: str2 = str1 but I can't do this: str1 = str2
0
7234
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7136
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7344
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7412
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7069
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5060
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4730
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
441
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.