Skip to main content
GameDev.net gamedev.net
🔒 Locked

Statement, Expression, Condition

Started by Tomokka Dec 4, 2006 at 1:44 PM 5 replies 1k views
Original Post
Tomokka
Tomokka
I am currently learning c++. I am doing prity good i guess, but i sometimes run into problems with words and their meaning because i am not from an english speaking country. I was wondergin could anybody explain the difference between a statement, condition and expression? In a simple way please.
Tomokka
Tomokka
To be more clear, i am currently learning loops. The while and do while loops use condition to be tested, so does the if statements. But the for loop uses an expression. I just dont get the difference here.
TheTroll
TheTroll
Thanks for explaining what you were asking. Let me give it a try.

First the "if statement" The if statement is if(x == 21), it is the whole thing. A statement in programming is just code that does something. Comments and white spaces are not classified as statements.

In the if stament is a condition. A condition is something that is being tested, will be either true or false. So on a Do/while or while statement you are testing a condition to see if you should continue the loop.

An expression is any "mathmatical" statement. x = 2, x == 21, y = x + 7, ect. are all experssions. In a for loop for(int a = 0; a < 10 ; a++) a = 0 is just an assignment experssion, a < 10 is both a condition and an expression, a++ is a incriment expression. Now also just to bring it back to the beging for (int a = 0 ; a < 10 ; a++) is also a statement becuase it is code that does something.

Ok, I think I got that right and hope that explains everything.
theTroll
juanpaco
juanpaco
To add to what TheTroll said, "a < 10" is both a condition and an expression, because conditions are a type of expression, just like a dog is a type of animal. (Sorry... not trying to follow you around the forums TheTroll!).

For loops DO use conditions. In the for loop you have three statements... kind of like:

for (; ; )

The for loop continues to execute as long as the condition evaluates to true. Remember that while loops can be written as for loops. For instance the following two examples accomplish the same task:

#1-
for (int i=0; i<5; i++)
{
}

#2
int i=0;
while (i<5)
{
i++;
}

You can see that both have conditions.

I hope that helps!





ToohrVyk
ToohrVyk
An expression is a piece code which will get a value at runtime. 1 is an expression, a+1 is an expression, foo(new bar(),j++) is an expression.

A condition is an expression which has a value compatible with a boolean. This includes booleans, integers (true if nonzero) and pointers (true if not null) and any classes with the adequate type conversion (istream, ostream), but excludes most other types (for example, std::vector or std::string).

A statement is an individual operation in a program, which has no value (only side-effects). In C++, these can be:
- Evaluate an expression and discard its value. Such as ++i; or std::cout << "Hello, world!"; or a = b;
- Call a function that returns void, such as myVector.clear();
- Control the program flow (throw, return, break, goto etc).
- Perform a conditional check one or more times, evaluating one or more blocks of statements as a result. For example: if() {}, while() {} or even catch() {}.

Tomokka
Tomokka
Thanks for answers, i am prity sure i got it now. The thing that got me confused is that in the book i am reading it says that if has a condition, do/while and while have conditions but for has an expression that will be tested true or false. So am i correct when i think the condition and expression are prity mutch same? Atleast in loops
Zahlman
Zahlman
Conditions are a kind of expression, and expressions a kind of statement. Happy?

Obligatory nitpicking for juanpaco: Not if the loop contains a "continue" statement (the for-loop's "post-loop expression" will be evaluated before looping again, but the increment at the end of the while-loop won't be reached).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.