Hello C# 003

"The general laws of Nature are not, for the most part, immediate objects of perception" – George Boole (maybe)

If only

With memory and the ability to sequence instructions that alter it, we can describe some very primitive programs, like "sum A and B".
Add some creativity and a language where operations like X > Y evaluates to either 1 if true or 0 if false, it is possible to recreate conditional behavior just like the example bellow to "biggest of A and B:

~ % csharprepl
Welcome to the C# REPL (Read Eval Print Loop)!
...
> var memory = new int[128]; // create memory
> memory[0] = 10; // set number A
> memory[1] = 11; // set number B
> memory[2] = memory[0] > memory[1] ? 1 : 0; // the > returning 1 or 0
> memory[3] = (memory[2] * -1) + 1; // just negates the previous value
> memory[4] = memory[0] * memory[2] + memory[1] * memory[3]; // "select" by using basic multiplication properties
> memory[4] // the biggest of A and B
11

Conditional behavior happens when we want certain instruction to only be executed under given conditions. To achieve this we use boolean algebra to describe the condition and conditional statement to specify the instructions to be executed.

Because Boolean values can only be true or false (1 or 0 in certain languages), there is also an optional statement to specify conditions to be executed in the case the conditions are not met.

Syntax

if( <CONDITION> )
<EXECUTE IF TRUE>
else
<EXECUTE IF FALSE>

The execute block can be any statement or a sequence of statements by surrounding them by curly braces ( { } ).
Due to readability reasons and easy of mistake, even single statements may be surrounded by curly braces:

// seldom used
A = 10
B = 8
C = 9
if( A > B)
if( A > C)
D = A;
else // is this the else of `if( A > C)` or `if( A > B )` ?! D = C;
else
if(B > C)
D = B;
else
D = C;
// more common
if( A > B)
{
if( A > C)
{
D = A;
}
else
{
D = C;
}
}
else
{
if(B > C)
{
D = B;
}
else
{
D = C;
}
}

Using it

Here is an example of a program that that gives the double if given an even number and the triple otherwise:

> memory[0] = 9
9
> if(memory[0] % 2 == 1) memory[1] = memory[0] * 2; else memory[1] = memory[0] * 3;
> memory[1]
18

For some reason unknown to myself, some languages have a "shorthand" version of the if else using the `?` operator.
Such that the code above can me rewritten as:

// <Boolean expression> ? <if true expression> : <if false expression>
> memory[1] = (memory[0] % 2 == 1) ? memory[0] * 2 : memory[0] * 3;

Just be careful with precedence, each language have it own rules, so it is a good idea to check the documentation for operator precedence.

[Try by yourself] Add parenthesis to the following expression such that it evaluates to 2 in the REPL: 10 > 9 ? 1 : 2 > 1 ? -1 : 2

[Try by yourself] Write a program that compute real roots of quadratic equations or set both values to `float.NaN` in case the root is imaginary.
(create a memory of float instead of int, note that it is not trivial to understand, but it allows you to reason about non whole numbers)

Up Next

Loops!


Go home.

☆Powered by Azure Blob