- f(1 + 2 * "hello".length)
Assignment
- var y = 0
- val x = y = 5
x?- int y = 0;
- int x = y = 5;
In Scala, everything is an expression!
- printf("hello");
- ^^^^^^^^^^^^^^^ expression
- ^^^^^^^^^^^^^^^^ statement
- return 1+x;
- ^^^ expression
- ^^^^^^^^^^^ statement
- int count = 0;
- while (1) {
- int ch = getchar();
- switch (ch) {
- case -1: return count;
- case 'a': count = count + 1;
- default: continue;
- }
- }
Cannot use statements verbatim as part of expressions
Use functions to turn statements into expressions
- int x = 1;
- printf ("%d\n", ++x); // pre increment, prints 2
- // value of x is now 2
- int x = 1;
- printf ("%d\n", x++); //
- //
- x = 1 + (y = 5); //
- int x = 1;
- printf ("%d\n", (x = x + 1) + x); //
- int x = 1;
- printf ("%d\n", ++x); // pre increment, prints 2
- // value of x is now 2
- int x = 1;
- printf ("%d\n", x++); // post increment, prints 1
- // value of x is now 2
- x = 1 + (y = 5); //
- int x = 1;
- printf ("%d\n", (x = x + 1) + x); //
- int x = 1;
- printf ("%d\n", ++x); // pre increment, prints 2
- // value of x is now 2
- int x = 1;
- printf ("%d\n", x++); // post increment, prints 1
- // value of x is now 2
- x = 1 + (y = 5); // assigns 5 to y and 6 to x
- int x = 1;
- printf ("%d\n", (x = x + 1) + x); //
- int x = 1;
- printf ("%d\n", ++x); // pre increment, prints 2
- // value of x is now 2
- int x = 1;
- printf ("%d\n", x++); // post increment, prints 1
- // value of x is now 2
- x = 1 + (y = 5); // assigns 5 to y and 6 to x
- int x = 1;
- printf ("%d\n", (x = x + 1) + x); // no "sequence point", undefined!
Sequence point: A point in the execution of a C program at which all previous side effects are guaranteed to be complete.
- class C {
- private int x = 0;
- private int y = 0;
- public int f(int z) {
- x = x-z;
- return x;
- }
- public int g() {
- y = y+5;
- return y;
- }
- }
- C c = new C();
- c.f(c.g()); // same as x -= (y += 5)
, creates a sequence of expressions: last expression provides valueStatement sequence
- int main () {
- int x = 5;
- x *= 2;
- printf ("%d\n", x);
- }
Expression sequence
- int main () {
- int x = 5;
- printf ("%d\n", (x *= 2, x));
- }
e1, e2, ... ene1 ? e2 : e3# <span class="fa-stack"><i class="fa-solid fa-circle fa-stack-2x"></i><i class="fa-solid fa-code fa-stack-1x fa-inverse"></i></span> Is this Accepted Syntax? <div class="grid grid-cols-2 gap-4"> <div> **Is this Scala?** ```scala def f (x: Int) : Int = var y: Int = 0 if x!=0 then y=1 else y=2 y end f ``` - Enter in REPL to find out </div> <div> **Is this C?** ```c int f (int x) { int y; if (x!=0) y=1; else y=2; return y; } ``` - Compile to find out </div> </div> ---
# <span class="fa-stack"><i class="fa-solid fa-circle fa-stack-2x"></i><i class="fa-solid fa-code fa-stack-1x fa-inverse"></i></span> Side-Effecting Expressions in C <div class="grid grid-cols-2 gap-4"> <div> - :fa fa-question-circle: Name some side-effecting expressions in C * Post-increment `x++` * Add and assign `x += 2` * Assignment `x = (y = 5)` * Combined `x -= (y += 5)` </div> <div data-marpit-fragment> - Side effects common in OOP ```java class C { private int x = 0; private int y = 0; public int f(int z) { x = x-z; return x; } public int g() { y = y+5; return y; } } C c = new C(); c.f(c.g()); // same as x -= (y += 5) ``` - Results often depend on object state :fa fa-arrow-right: potentially on entire execution history </div> </div> ---
# <span class="fa-stack"><i class="fa-solid fa-circle fa-stack-2x"></i><i class="fa-solid fa-list-ol fa-stack-1x fa-inverse"></i></span> Sequencing in Expressions <div class="grid grid-cols-2 gap-4"> <div> - Scala `{ e1; e2; ...; en }` <div class="text-sm"> ```scala { e1 e2 ... en } ``` </div> </div> <div> - C `( e1, e2, ..., en )` <div class="text-sm"> ```c ( e1, e2, ... en ) ``` </div> </div> </div> <div class="grid grid-cols-2 span-4"> <div> - `e1` ... `en-1` executed for side effect - Result is the value of `en` </div> <div> * :fa fa-question-circle: Why have statement and expression sequences? * A C [example](https://stackoverflow.com/questions/52550/what-does-the-comma-operator-do-in-c): ```c string s; while(read_string(s), s.len() > 5) { // do something } ``` </div> </div> ---