Basics in Programming #3 (C#): 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.

Scope

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:

if(true)
{
    //Do something
}
if(false)
{
    //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")
{
    PostNewTutorial();
}

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

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.

switch(Console.ReadLine())
{
    case "yes":
        Console.WriteLine("You said yes!");
        break;
    case "no":
        Console.WriteLine("You said no!");
        break;
    default:
        Console.WriteLine("You didn't say anything we care about.");
        break;
}

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

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.

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

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!

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

Leave a Reply

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