Lesson 14.2 – If statements

Lesson Objectives

At the end of this lesson, you will know…

  • How to use “if” statements

 

One of the most common things you’ll do in a program is use an “if” statement.

“If” some condition is true, then you want a certain section of code to run. If it isn’t true, you don’t want that section of code to run.

For example, during a battle, if the player’s hit points drops to zero, you’ll want to end the battle. If the player has more than zero hit points, you’ll want them to be able to continue fighting.

You can also create “if” statements that are more complex than just checking one value. Or, you may want to be able to handle more than two conditions for a value.

Here are code samples, and description, for four typical ways you’ll use an “if” statement.

The simple “if” statement

Here, we check if the passed-in “currentHitPoints” is greater than zero. If it is, the computer will run the code between the curly braces after the “if” – in this case, lines 4 through 6. When it reaches line 5, it will stop processing the rest of the code in this function and return a value of “true”.

If “currentHitPoints” is equal, or less than, zero, the computer will continue going through this function. In this case, it will get to line 8 and return “false” to whatever called this function.

“If” with an “else”

This function does the same thing as the previous one. However, we explicitly have an “else” statement.

So, if the expression between the parentheses of the “if” statement is true, the code between the curly braces on lines 4 and 6 will be run. “Else”, the code between the next set of curly braces will be run.

This is a simple function, with just one line in each branch of the “if” statement (the “true” branch and the “false” branch). However, you could have more lines of code between the curly braces – as you’ll see in the lessons when we add the code to play the game.

Complex “if” conditions

You can check more than one condition in an “if” statement. You can also make it so all the conditions must be true (using “and”s), or, at least one of the conditions must be true (using “or”s).

This is a place where some people get a little confused – especially when the code has many conditions that it’s checking.

An important thing to remember is to look for parentheses around the conditions, and work your way from the inner-most sets of parentheses, to the outermost – just like in Algebra class (if you remember that).

Let’s say that this function is called with these parameters:

currentHitPoints = 5

hasResurrectionRing = false

If we replace the variables in the parentheses with the values, we end up with this:

Which becomes:

Now we can deal with the new symbols “||” and “&&”.

These are used when you want to work with Boolean values. “||” means, “or”. “&&” means “and”.

With “&&”, the value on both sides of it need to be true, for the whole comparison to be “true”.

Now we can reduce the “((false) && false)” to “(false)”, since the values on both sides are not equal to “true”. That gives us this:

With “||”, if the value on either side of it is “true”, then the whole statement is “true”. If you look at the original “if” statement in English, you’d say it like this, “If the player’s current hit points is greater than zero, or the player’s current hit points is zero and they have a resurrection ring, then do this…”.

Since we have a “true” on one side of the “||” (or), the whole thing evaluates to “true”.

So, for these parameter values, the code between the first set of curly braces would be run.

If you passed in these parameters, the program would follow these steps to eventually figure out to run the “else” code:

currentHitPoints = 0

hasResurrectionRing = false

Makes the “if” look like this:

To:

To:

To:

So, the code between the curly braces after the “else” would be run.

“If” with “else if”

Sometimes you want to do multiple checks against something.

Here’s one way you might look at the player’s current experience points to determine their level:

In this case, if the experiencePoints parameter is less than 100, the function will return a value of one.

However, if it’s 100 or more, we still want to do some more comparisons in the “else”. So, we use an “else if” to connect our next “if” statement.

In this function, if the player had 672 experiencePoints, it would be false for the first “if” (where it checks to see if the value is less than 100), and go to the “else if” statement. Since 672 is not less than 250, it would go to the next “else if”. There, it’s also not less than 500, so it goes to the next “else if”. Finally, 672 is less than 1000, so the program runs the code to return a value of 4.

In case none of the “if” conditions were true, the program would have continued going through the function until it hit the line to return 5.

 

Summary

Now you see how you can run, or not run, sections of code based on conditions in the program.

When we add in the source code for the game logic (moving the player around to different locations, battling monsters, completing quests, etc.) you’ll see several “if” statements in action.

Note: With the “if” condition that used “||” and “&&”, I included lots of parentheses, so you’d know exactly how the comparison logic should be calculated. Be nice to yourself, or the next programmer who will look at your code, and do the same.

I could have written it as:

But I think that makes it a little harder to understand.

 

Source code for this lesson

There is no code for this lesson

 

Next lesson: Lesson 14.3 – Foreach loops

Previous lesson: Lesson 14.1 – Variables

All lessons: Learn C# by Building a Simple RPG Index

Leave a Reply

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