An expression statement consists just of any expression. Basically, expressions are:
- Function calls ("foo()")
- Assignments ("=", "+=", ...), see operators
- Comparations ("==", "<", "is", ...), see operators
- "Mathematical", boolean and unary expressions ("a + b", "a - b", "a && b" ...), see operators
- Conditional expressions ("foo ? "a" : "b")
- Literal values (like 8 or the string "abc"), see literal values
- Variables, see variable types
- Some special keywords ("this" or "super()")
Examples:
x = y;
foo();
a = (b < c) ? b : c; |
Special cases:
There are some expression statements that need some more explanation about what they do and - that`s the point - in which order they do this.
Examples:
i = 1;
++i;
i++;
i += ++i;
i += i++; |
What`s the difference between those two statements?
Well, it is just the same thing in C/C++. The side effect of both increament-s is increament i by 1, i.e., i = i + 1. However, the value of the prefix increament expression is the result of i+1, say in our example, 2. Whereas the value of the postfix expression is the old value of i, say, 1 in this case. Attention: those two expressions followed by the comment /* error: value of i undetermined */ are bad. "Unless otherwise specified, the implementation is free to evaluate the components of an expression in any order. It is an error to depend on order of evaluation when it is not specified. " It is because of something like "sequence point" in C/C++. Those two expressions try to change the value of the same variable i twice between two sequence points. If you do not know what is a "sequence point". You`d better read the C/C++ standard report or any books on C/C++ talking about the term, or easier, google it.
while ( (++i) < j )
statement;
while ( (i++) < j )
statement; |
As before explained, there is a big difference between pre-incrementals and post-incrementals inside the condition of a while-loop.
- First example increments i before the comparison,
- second one increments i after the comparison.
With the first example you can avoid using while-statements like:
Special Keywords
See also:
Classes
- "this" is a pointer to current object (pointer gets dereferenced in statements afaik - so x = this; would copy the object) and the 'name' for constructors.
class foo
{
this() { }
foo bar() { return this; }
} |
- "super()" calls the constructor of the base-class:
class Foo
{
this() { }
}
class Bar : Foo
{
this() { super(); } } |
Conditional expressions
Conditional expressions are expressions that are evaluated on boolean purpose; whether something is true or false for example. They're used mostly in control structures, such as while loops or if constructs. For conditional expressions there exist special operators, like AND (&&) and OR (||).
if(conditional expression)
{
} |
You are not limited to only using conditional operators in conditional expressions; in fact, sometimes it's useful to use a normal operator in a conditional expression. What is true though, is that for a true conditional expression, the eventually evaluated value will be 1 (true) or 0 (false). Any other value than 0 will be interpreted as being true.
== Equals, != Not equals
&& And, || Or
(1 == 1) && (2 == 2); (1 == 1) && (1 == 2); (1 == 1) || (1 == 2); (1 == 2) || (3 == 4); |
To do: Explain conditional expressions and special keywords here, maybe a bit of expression evaluation and special cases (i += ++i;) too.
|