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:
What`s the difference between those two statements?
- Well, the first one increments i and adds the result (arithmetically) to the old i.
(pre-increment) - The second statement adds i itself and increments the result.
(post-increment) (In this case there is no difference in the result, but allways take a look at what you need!)
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; wether 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 limitted 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.
|