Programming in C#, Part 3: If Statements, Switches, and Enums

So this lesson is gonna contain a lot of very important information, and you will use these concepts throughout programming frequently. You might already be familiar with these concepts. Stuff to google or research outside of this lesson will be, as always, in bold.


So before we get into our switch statements, we need to quickly talk about scope. Scope defines what is happening where, and what variables and functions can see each other. You’ve already been exposed to scope, it’s defined anytime you use the { } brackets. So lets say we have this:

int Bob = 0;
    int Jim = Bob + 1;
Bob = Jim;


Now, this will give us an error. Stuff inside a lower level scope can see stuff in a higher scope, but the higher scope cannot see the lower scope. So here Jim can see Bob, because its outside, its the higher scope and so it knows it exists. However Bob has no idea Jim exists, its a sad love story. So we will get an error here because Bob has no idea what Jim is.

If Statements

One of the fundamentals of programming, being able to evaluate a statement to determine if something should happen, or not. These allow us to check if a boolean statement is true or false, and if it is true we will go and do whatever the if statement tells us to.

How do we use this amazing feature? We simply do this:

    //Do something
    //This will never run

It’s that simple! You don’t just put true and false in there raw though, normally you give it a boolean variable or statement. So you can also check things inside these, like a name for example.

if(name == "Cory")

This is something you will use often. There are also a couple statements to use alongside this, else and else if, these are pretty simple so I’m going to let you google them.


Switches are like faster, cooler if statements. If you needed to check multiple values for something you might have to do multiple if else statements; which will take a tiny bit longer to check and be ugly. So we have been given the wonderful tool of switches.

They allow us to “switch” on a specific value that we give to it, so we check the value, and can have certain cases do certain things. For example, if we were given some input and wanted to do something special with it.

    case "yes":
        Console.WriteLine("You said yes!");
    case "no":
        Console.WriteLine("You said no!");
        Console.WriteLine("You didn't say anything we care about.");

So notice we give the switch some input, in this case just reading whatever the user typed. Then we have our cases, checking if they typed yes or no, and if they didn’t we have a default case that it will run.


Enums are awesome. Default enums are pretty much an int, except pretty so we can read it easier. You can also define them as being other types, but that’s not super important to go over right now. They represent int values, can be casted to an int if need be, and look wonderful. You can define an enum like so

enum Classes { Knight, Wizard, Rogue }

That defines a new enum called Classes. You can also now make a variable out of that new declaration like any other variable. Classes myPlayerClass = Classes.Knight
To access a value, you use the name of the enum (in this case Classes) and then you can get access to its values. Want to cast to an int? Simple do (int)myPlayerClass. Now what values are these enums you may ask? Simple, they start at 0 and go upwards by 1. So Knight is 0, Wizard is 1, Rogue is 2. You can override this if you want, like this.

enum Classes { Knight = 3, Wizard, Rogue = 10 }

So now Knight is 3, Wizard is 4, and Rogue is 10.
We can use this really nicely with our switch statements like so.

    case Classes.Knight:
        Console.WriteLine("The player is a knight!");
    case Classes.Wizard:
        Console.WriteLine("The player is a wizard!");
    case Classes.Rogue:
        Console.WriteLine("The player is a rogue!");

Next Time

You may notice the lack of the extra practice here, I’m actually going to leave that for the next tutorial, as combining this information along with our next tutorial will work well. Check out the next tutorial for arrays and loops!


Are you having trouble with understanding this tutorial? Please feel free to contact me via email at or message me on discord at 7ark#8194.

I would love to get feedback from people so I can add and improve these tutorials overtime. Letting me know what you’re confused about will let me update the tutorials to be more concise and helpful.

If you’re interested in supporting me personally, you can follow me on Twitter or Instagram. If you want to support me financially, I have a Patreon and a Ko-fi.

0 comments on “Programming in C#, Part 3: If Statements, Switches, and EnumsAdd yours →

Leave a Reply

Your email address will not be published. Required fields are marked *