Learning C# Notes - Part III: Selection & Operation Statements

So up until now I’ve left my notes on classes, which work as functional units in c# and on variables which store all sorts of data.

Next thing would be to use that data residing within classes and do something with it. Selection statements allow us to so different thing depending on the data itself while operation statements gives you the ability of parsing through collections of data to read or modify them.

Conditional Statements (If-else)

  • This is the most basic selection statement.

  • We analyze a condition and determine what to do if it is true.

  • Since a condition can only be either true or false, we must always analyze a boolean variable or any method that returns a bool.

  • We can use any logic expression that would result in a boolean like 3 > number.

  • We can also determine what we want to happen if the condition is not true with the else keyword.

  • Additionally, we can check further conditions with the else if keyword.

  • See examples:

int health;

if (health < 10) 
{
    //Player is hurt.  
}
else if (health < 50) 
{
    //Player is low health.  
}
else 
{
    //Player is healthy. 
}

Switch Statement

  • The conditional structure explained above is good when you just have a few possible options but sometimes you have a bunch of cases and you want to do something different for each one.

  • We can also include a default case which will be used if the expression passed is not found.

  • The syntaxis is a bit different that conditional statements, see below:

enum playerState; //This struct contains a list of states the player can be in.

switch (playerState) 
{
  case playerState.poisoned
    //We play a poisend snapshot.
    break;
  case playerState.burning
    //We play a burning snapshot.
    break;
  case playerState.confused
    //We play a confused snapshot.
    break;
  case playerState.dead
    //We play a death snapshot.
    break;
  case default
    //We play the normal snapshot.
    break;
}

Logical Operators

  • If we want to do more complex statements we can use logical operators to add certain conditions and logic.

  • We use “&&” as an AND statement when we want to check that two or more conditions are true.

  • We use “||” as an OR statement if we want to do something when either condition is true. Only one needs to be true.

  • We use “!” as a NOT statement to signify that we want to check the opposite of a condition. In other words, the “!” operator inverts a bool, turning a true into false.

  • When checking that something is bigger or smaller that our condition, we use “<“ and “>”.

  • We can also use “<=” to check is something is smaller or equal and “>=” to check if it is bigger or equal.

  • We use “==” to check if a variable equals another.

  • See these in action below:

// NOT
bool result = true;
if (!result)
{
    Console.WriteLine("The condition is true (result is false).");
}
else
{
    Console.WriteLine("The condition is false (result is true).");
}

// Short-circuit AND
int m = 9;
int n = 7;
int p = 5;
if (m >= n && m >= p)
{
    Console.WriteLine("Nothing is larger than m.");
}

// AND and NOT
if (m >= n && !(p > m))
{
    Console.WriteLine("Nothing is larger than m.");
}

// Short-circuit OR
if (m > n || m > p)
{
    Console.WriteLine("m isn't the smallest.");
}

// NOT and OR
m = 4;
if (!(m >= n || m >= p))
{
    Console.WriteLine("Now m is the smallest.");
}

Iteration Statements: foreach

  • We use this iterator to go through all the values on a list, dictionary or other collection.

  • We use the “in” keyword to specify the type of element to be examined and where it comes from.

  • Is important to note that “foreach” creates a copy of the collection when iterating through it so we can’t alter it directly.

  • If we want to alter a collection as we loop through it we need to use a “for” loop (see below).

  • "foreach” is more readable and clean but performs worst than a for loop.

var fibNumbers = new List<int> { 0, 1, 1, 2, 3, 5, 8, 13 };
int count = 0;
foreach (int element in fibNumbers)
{
    Console.WriteLine($"Element #: ");
    count++;
}
Console.WriteLine($"Number of elements: ");

Iteration Statements: for

  • This will execute the code inside as long as the condition evaluated is true.

  • We initialize an iterator value and alter it on each cycle.

  • It is very useful for checking all the elements in a collection or do something a certain number of times.

  • It allows you to directly alter the values of the collection that you are examining.

  • Is important to note that we first check the condition and then execute the code between { }.

int[] Numbers = {1,2,3,4,5,6}
contant someConstantValue;

for (int i = 0; Numbers.Length; i++) 
{
    Numbers[i] = Numbers[i]*someConstantValue;    
}

Iteration Statements: do while

  • This basically works like a for loop but the main difference is that the code is executed first and then the condition is checked.

  • So in other words a “do while” statement will always execute the code between { } at least once.

  • You can use the keyword “break” to exit the loop at anytime.

int n = 0;
do
{
    Console.WriteLine(n);
    n++;
} while (n < 5);

Iteration Statements: while

  • “while” can also be used by itself to to something while a certain condition is true.

int n = 0;
while (n < 5)
{
    Console.WriteLine(n);
    n++;
}