C# Conditional Checking

C# Conditional Checking

Last Revision: 17th February 2019

Conditional checking is in every piece of programing: from simplistic to complex.
In this tutorial, we'll cover if and switch statements.

Note:
In C# (and other languages), the equals signs has 2 types - assignment and condition.

Assignment
Assignment is used to assign a value to a variable (var number = 20)

Conditional
Conditional is used when comparing 2 or more values to each other ( tempA == tempB )

//Assignment (One = symbol)
int Year = 2019;
float PI = 3.142;
float money = 3.50;
float addUp = PI + money;

//Conditional (Two = symbol)
if(Year == 2019) //Is the variable "Year" equal 2019?
{
    Console.WriteLine("Hey, its 2019!");
}
else
{
    Console.WriteLine("Wooh, it isn't 2019!");
}

if(PI == money) //Is the variable "PI" is the same value as variable "money"?
{
    Console.WriteLine("PI is the same as money!");
}
else
{
    Console.WriteLine("PI isn't the same as money!");
}

If statement
The if statement is used in most languages to provide logic to the application, without it we cannot perform certain crashes (no validation!)

In a if statement, we can have multiple if statements inside it - this is called Nested If Statements.

if(A == true)
{
    //Do something
}
else
{
   if(A == false && B == true)
   {
      //Do something
   }
   else
   {
      if(A == false | B == false && C == true)
      {
         Console.WriteLine("Can you fly bobby?");
      }
   }
}

Right away this can get confusing, lets try and simplify this logic:

//Idea: To validate responce of y/n
if(responce == "y")
{
    //User said yes
    DoSuccessfulMethod();
}
else
{
    if(responce == "n")
    {
        //User said no
        DoNoResponceMethod();
    }
    else
    {
        //responce doesn't fit into any above!
        DoInvalidRequest();
    }
}

We can then use the new conditional type, Switch:

//Idea: To validate responce of y/n
switch (responce) //Target: responce
{
    case "y":
        DoSuccessfulMethod();

    case "n":
        DoNoResponceMethod();

    default: //responce doesn't fit into any above! (default call if doesn't match any above)
     DoInvalidRequest();
}

From this example, we reduced the amount of lines by 36.84%! (19 lines to 12 lines)

Switches downfall:
Here are a few exceptions of using switches (still possible, but more effort! Assuming its typed normally without using a few tricks up our selves!):
  • You cannot use comparisons
    • You cannot use >, <, >= or <= (Only in If statements you can achieve this)
  • You can only validation ONE value for comparing (With if statements, you are free to do whatever (too a limit!))
When Switches are better than If Statements:
  • When you need to validate a response (Yes/No etc)
  • Working with enums
When If Statements are better than Switches:
  • When you need to validate more than one variable for validation
  • When you need to perform numeric comparisons
  • To perform nested loops (Validation of validation)



Popular posts from this blog

C# Object Oriented Programming

C# Memory - Tutorial