tl;dr: You're going to learn the rules that programming languages use to evaluate expressions.
We're going to start simple but by the end of these exercises
you should be able to correctly determine the order of
operations for this expression:
b = c = a[x] * 3 << 4 < 4 || 7 % 3 == 2 + obj.f(x)
We're going to learn the rules for how expressions are
evaluated in the C# programming language. (We're going to look
at C#, but the rules are pretty much the same for any other
programming language). An expression could be something
like (I'm going to assume that int a; int b; int c;
have already been declared):
a = 1;
or something like:
a = 2 * b + c / 4;
or something like:
Console.WriteLine( 2 * b + c / 4);
The goal is for you to be able to determine the order of operations for any C# expression.
int y;
y = 3 * 3 + 2;
|
y = 3 * 3 + 2 |
y = 9 + 2 |
y = 9 + 2 |
y = 11 |
y = 11 |
11 |
Order Of Operations | |
---|---|
* | Multiplication |
+ | Addition |
= | Assignment: Variable on the left hand side is given the value on right hand side, value of the right hand side is used in any further evaluation. |
int y;
y = 3 + 3 * 2;
|
y = 3 + 3 * 2 |
y = 3 + 6 |
y = 3 + 6 |
y = 9 |
y = 9 |
9 |
y = 1 + 2 * 3;first 2 *3, then 1+6, then assign 7
int x = 4;
int y = 7;
if( x + 2 > y - 2 )
|
if( x + 2 > y - 2 ) |
if( 4 + 2 > y - 2 ) |
if( 6 > y - 2 ) |
if( 6 > y - 2 ) |
if( 6 > y - 2 ) |
if( 6 > 5 ) |
if( 6 > 5 ) |
if( true ) |
Order Of Operations | |
---|---|
* | Multiplication |
+ | Addition |
> or <= | Comparison: A > B is true if A is strictly greater then B (and false otherwise). A <= B is true if A is less than or equal to B (and false otherwise) |
int x = 4;
int y = 7;
if( y + 2 <= x * 2 )
|
if( y + 2 <= x * 2 ) |
if( y + 2 <= 4 * 2 ) |
if( y + 2 <= 8 ) |
if( y + 2 <= 8 ) |
if( 7 + 2 <= 8 ) |
if( 9 <= 8 ) |
if( 9 <= 8 ) |
if( false ) |
if( y * 3 < x - 3 )(pick your own values for x and y) first 7 *3, then x - 3, then compare the two