Dan Kenny Game Design
  • Home
  • Portfolio
    • Game Design
    • 2D & Animation
  • Blog
  • About Me
  • Contact
  • Home
  • Portfolio
    • Game Design
    • 2D & Animation
  • Blog
  • About Me
  • Contact

C# Beginners Tutorial: If Statements

31/3/2018

Comments

 
Hey there, Gamers and Game Makers!

Welcome back to the third part of this introduction to C# tutorial series. This week, we're going to be looking at a if statements and how they are used. So let's dive right in!

So, what exactly is an if statement? We can use if statements to check for certain conditions and then run a chunk of code if those conditions are met.
Picture
Let's make a simple example. I've declared a variable of type int called level and given it a value of 11 for now. I'm going to create an if statement that will check the value assigned to level and if the level is a match, it will run the print function that will display the string "Hello" to the console. I create the if statement as follows:

if (level == 12) {
print ("Hello");
}


This if statement checks if the variable level is equal to 12. If it is then it will run the print function. if we  try running it as it is, we'll see nothing displayed to the console as the condition is not met. Let's change the level value to 12 and see what happens.
Picture
Picture
Since the value assigned to the variable level is now 12, the condition of the if statement has been met and it can run the code inside of it which in this case is to print the word Hello to the console. However, it's not very helpful if the player is not the right level as then the condition is not met and nothing is printed. We should do something to tell the player they are not the right level yet. We can add something to help with this.
Picture
Ok,I've introduced two new things to the if statement now. The first thing to notice is I've added an else just below the if statement. This will run anytime the condition of our if statement is not met. In this case we'll use it to tell the player that they are not the right level.

The next thing to note is, I've changed the condition of our if statement. Now instead of the variable level having to be equal to exactly 12, if it is greater than or equal to 12 then the condition is met and the code will run. If I run it as is, the word Hello will be printed to the console again.

Picture
Picture
However, as you can see if I drop the value of the variable level to 10 which is less than 12 and run the script, I'll be given the test "You are not the right level" in the console. This is because 10 is less than 12 so obviously the condition of our if statement is not met and as a result the else section of the statement kicks in. Ok, let's try something else.
Picture
I've added a new string variable called name and given it the value of "Dan". So, if you want to check multiple conditions at once the best way to do this is to use the "and" or "or" operators in your if statement. Let's start with the "or" operator which is represented by ||. Using the || operator means that you can check multiple conditions and the code will run provided at least one of the conditions is met. In this example, both conditions are met and the word "Hello" will be printed to the console.
Picture
Picture
In this example, my level isn't high enough but the string condition is met so the word "Hello" is still printed to the console.
Picture
In this case the level condition is met but the string condition is not met but because at least one of the conditions has been met the word "Hello" will still get printed to the console.
Picture
Picture
In this case, none of the conditions are met so the if statement does not execute its code but the else kicks in to let you know "You shall not pass!" Ok, let's take a look at the "and" operator which is represented simply as two && symbols.
Picture
With &&, the code will only run if both conditions are met. In this case both the level and string condition are met so the word "Hello" will be printed to the console.
Picture
Picture
In this example, the string condition is met but the level condition is not met. Since we are using the && operator, both conditions need to be met in order for the code to run. So the else will kick off in this case.
Picture
Here the level condition is met but the string condition is not met so once again the code will not run and the else will kick in. Have a go at trying your own combinations of if statements and see what you can come up with.

That does it for this week's tutorial. If you have any questions or suggestions, feel free to leave a comment below or get in touch.

Until next time!

Comments

    Archives

    April 2019
    March 2019
    February 2019
    January 2019
    December 2018
    November 2018
    October 2018
    September 2018
    August 2018
    July 2018
    June 2018
    May 2018
    April 2018
    March 2018
    February 2018
    January 2018
    December 2017
    November 2017
    October 2017
    September 2017
    August 2017
    July 2017
    June 2017
    May 2017
    April 2017
    March 2017
    February 2017
    January 2017
    August 2014
    November 2013
    September 2013

    Categories

    All

    RSS Feed

© COPYRIGHT 2020. ALL RIGHTS RESERVED.