Hello

I have started to study C++ after having worked with a procedural language for many years. I have read a couple of C++ books (hmm, well at least looked into) and some tutorials about C++. I started to prepare a C++ quick reference guide for my own usage and it was then that I discovered that only Bjarne Stroustrup followed the ANSI standard in "The C++ Programming Language" 3rd edition when the syntax for while and the do...while statements was described.

The formal syntax for these statements are according to this book and the ANSI specification:

the while statement

while ( condition ) statement

the do while statement

do statement while (expression) ;

All other authors I have seen use a different syntax for condition and expression.
That is, condition may be used in both while and do...while
statements or expression may be used in both while and do...while statements.

My question is what the difference is between condition and expression in these statements? I haven't been able to figure it out from the C++ specification :)

A condition is something which is brought down to be true or false .
Most of the time, an expression can be evaluated to be true or false , false stands for the value 0 and true stands for all the other possible values :)

However in this Both the terms almost mean the same thing.

Just see that The do while loop first executes the whole block and then gets to the condition!

In a do-while statement the condition is always evaluated at the end of the loop which means that the loop is always at least executed one time :)

... what the difference is between condition and expression in these statements?

None. The condition simply evaluates to a TRUE/FALSE. Expression is a statement that will evaluate to a TRUE/FALSE. In the context of these loop definitions, they are interchangeable, and probably 'conditional expression' is more accurate.


[edit]Wow, a plethora of responses! We have no lives... :) [/edit]

The condition and expression statements have the following description:

condition: 
 expression
 type-specifier-seq declarator = assignment-expression
expression: 
 assignment-expression
 expression , assignment-expression

Has condition a wider meaning as it also includes the expression description.

But what does the following line add to the condition test?

type-specifier-seq declarator = assignment-expression

IMO, you're getting hung up on definitions. Use a conditional expression in a loop definition and you've got a proper statement. As you get farther along, you can worry about the nuances of this conditional.

In modern C++ condition is not the same thing as an expression. A condition can be a declaration, that's an example from the Standard:

int i = 1;
while (A a = i) {
//...
i = 0;
}

In modern C++ condition is not the same thing as an expression. A condition can be a declaration, that's an example from the Standard:

int i = 1;
while (A a = i) {
//...
i = 0;
}

Can you give an example from the standard for an expression as well?
So we can compare :)

Can you give an example from the standard for an expression as well?
So we can compare :)

I don't understand what do you want to compare with?
I can't quote all C++ Standard here ;)
To compare possible declarations with initializers or an expression (that's a condition) with an expression only - or what else?

Thanks for your answers. My understanding now is that I have to base my quick reference manual on the ANSI standard or Bjarne Stroustrup's "The C++ Programming Language" bok. I can't use other text books as input without checking it with the standard because it seems to be the same confusion with other statements, for instance:

selection-statement:
                  if ( condition ) statement
                  if ( condition ) statement else statement
                  switch ( condition ) statement

Some text book's use condition and other books use expression in above statements when the standard specifies condition.

Hence,

In the context of these loop definitions, they [[I]condition/expression[/I]] are interchangeable

Here's a perfectly good if statement:

if ((2*x)+7)
{
   ...
}

Is this considered a condition or expression? As I say, stop getting hung up on vague definitions of words.

Hence,


Here's a perfectly good if statement:

if ((2*x)+7)
{
   ...
}

Is this considered a condition or expression? As I say, stop getting hung up on vague definitions of words.

I think this is considered as a conditional expression :P (check this)

OP asked about syntax. An expression is a partial case of a condition in the context of these loop definitions. Another form of condition in this context: declaration (with some restrictions). So an expression and a condition are not interchangeable. An expression is a condition but not vice versa.

if (Node* p = q.getNode()) {

>Is this considered a condition or expression?
The same (funny) question: is F-15 an aircraft or it's a fighter?

Based on what I have learned,
an expression is an assignment to a variable like:

int sum = 1 + 1;
float a = 2 * (2 + 1.5);

So, anything that has the = sign is considered an expression. strcpy(str, "copy") is an expression as well. See why? int ret = returnFoo(int whatever); is an expression as well to the variable ret.

Condition on the other hand is a comparison that usually uses the relational operator. Condition is like:

if (a == 1) //Condition
{...}

while (1) //this becomes a condition
{}

for (int a = 1; a < 5 /*This becomes a condition*/; ++a) 
{}

do {
} while (...) //This while becomes a condition

if (ReturnFoo()) //This is a condition as well. See it?
{}

if ((int a = 1) > (2)) //This is a combination of both...
{}

That's what I've learned and I'm happy to share it with Sune.

>Condition on the other hand is a comparison
Wrong! A condition is not necessarily a comparison: if(true) cout << "Where is the comparison?" << endl; >an expression is an assignment to a variable like ...
Wrong again! An assignment is a statement, I would describe an expression as something which has a value and can be passed to a function :)

But you can say that (a=10) is an expression in the following context:

int a;
int b = [B](a=10)[/B];

Condition on the other hand is a comparison

Wrong! A condition is not necessarily a comparison:
if(true) cout << "Where is the comparison?" << endl;

In logic, if (true) cout << "..." <<endl;, this is called
simple conditional syllogism, which states that:
if a, then b. But a. Thus b. In that statement, it can be stated as "if it is true, then cout..." The "But a. Thus b." can be seen through implicitly i.e. "It is true. So cout..." That is what I mean in the second example on conditions. There is still a comparison because you are comparing true to false to activate the cout.

an expression is an assignment to a variable like ...

Wrong again! An assignment is a statement, I would describe an expression as something which has a value and can be passed to a function

I could not actually argue with that quote because you didn't
give an example regarding "I would describe an expression as something which has a value and can be passed to a function".

int a; is an expression because there is an assignment to the variable a. See through it? It is the default value or an unknown value which is assigned to a, which can be int a = 3287; or whatever. Still assigning.

"something which has a value" That value is stored in a variable or like that explained above. You cannot have an expression in your code as this: 1 + 1, without assigning it to a variable though in algebra this is an expression.

"can be passed to a function". There is still an assignment involved: Calling function: callFoo(5). Called Function: callFoo(int num). 5 is assigned to num. Still an assignent.

My problem is, I had explained to general.
I'm sorry for being too pathetic. I'm just proving my point.

Peace be with you.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

true is a conditional expression, so there was nothing wrong with my example, BTW: don't they always say that a condition can be true or false ?

>int a; is an expression because there is an assignment to the variable a . See through it? It is the default value or an unknown value which is assigned to a , which can be int a = 3287; or whatever. Still assigning.

Wrong! An assignment is a statement, the value you're assigning is an expression, in other words: on the right side of the assignment operator '=' there always has to be an expression !

>You cannot have an expression in your code as this: 1 + 1 , without assigning it to a variable though in algebra this is an expression.

Completely wrong! 1+1 is a valid C++ expression, don't you believe me?
Try compiling the following code:

#include <iostream>

using namespace std;

int main()
{
    [B]1+1;[/B] // -> The "impossible" expression you were talking about
    return 0;
}

>I'm sorry for being too pathetic. I'm just proving my point.

No, you're just proving that you're wrong.

To the OP: if([B]condition[/B]) : condition is the expression being evaluated, if the evaluated expression has a value other than zero, the condition is true , otherwise the condition is false ...

This also applies to your while and do-while question :)

What's a nightmare! Have you ever seen a simplest book on programming languages syntax?..

>an expression is an assignment to a variable like...

What is an expression in C++:

An expression is a sequence of operators and operands that specifies a computation. An expression can result in a value and can cause side effects.

It's NOT an expression: int a = 1; . It's a declaration.

Now have a look at conditional expression in C++:

...
logical-or-expression:
  logical-and-expression
  logical-or-expression || logical-and-expression

conditional-expression:
  logical-or-expression
  logical-or-expression ? expression : assignment-expression

So condition and conditional expression are different things.

>An assignment is a statement...

An assignment is an expression in C++.

assignment-expression:
  conditional-expression
  logical-or-expression assignment-operator assignment-expression
  throw-expression

There is assignment (binary) operator in this language. There is an expression statement in C++, that's it:

expression ; // expression + semicolon

An example of assignment without any statement:

Class c(a = b); // It's a declaration
// a = b is not a statement here
// A statement has no value in C++ (C++ is not Algol 68)

An example of assignment in return statement:

return mSize = n; // not an assignment statement!

>I would describe an expression as something which has a value and can be passed to a function
Look at

f(a = b); // because assignment is an expression

and

void f();
// f() is an expression (operator(), operand f)

but you CAN't pass its value (of type void) to a function!

At the final:

condition:
  expression
  type-specifier-seq declarator = assignment-expression

That's why we can see C++ definitions like

while (condition)
  statement

but not only

while (expression)
  statement

as in C language...
...

Do you have any idea why condition can't be used in both of the following while construct's?

while ( condition ) statement
do statement while (expression) ;

I have not seen one book that use the ANSI syntax, except for Bjarne Stroustrup's C++ book's. Even the

don't follow the ANSI syntax.

This has been answered so many times in this thread -- why are you still asking it?

Write to Stroustrup and ask him since none of the answers here seem to be acceptable and his book is causing your meltdown. His home page is http://www.research.att.com/~bs/ with an email address at the bottom.

Comments
Good advice!

>why condition can't be used in both of the following while construct's?
It's a good question.
The 2nd form of condition declares some variables with the loop body scope. However it's impossible to declare variables after its using in do-while loop. That's why no need in condition syntax construct in do-while statement: an expression only possible there...

I sent a mail to Bjarne Stroustrup and asked him: What's the difference between a condition and an expression (in C++) ?

I'm quoting his whole reply:

What's the difference between a condition and an expression in C++ ?
A condition is something you test in an if, while, for, or switch statement.

A condition can be an initialized declaration or an expression:

if (a==7) // the condition is an expression

if(int a = f(x,5,y)) // the condition is a declaration

And now why you write:

do {
/* Code here */
} while ([B]expression[/B]);

A declaration in a condition introduces a name to be used in the following statement; the condition in a do-while has the statement before the condition, so declarations make no sense there (and are disallowed).

Comments
Good job!!!

condition an expression has altogeth a different meaning...
condition tells for the statements that the loop will hold the value as true/false
whereas expression is merely a statement where the value will tell as true/false...

Comments
Wrong, and you can't understand it :(
This article has been dead for over six months. Start a new discussion instead.