# 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:

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

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 usedA = 10B = 8C = 9if( 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 commonif( 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 = 99> if(memory % 2 == 1) memory = memory * 2; else memory = memory * 3;> memory18`

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 = (memory % 2 == 1) ? memory * 2 : memory * 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)

Loops!

Go home.