Conditional Logic
Conditional Logic
The term conditional logic, in programming refers to the idea that you set the conditions under which
code will run.
For example, perhaps I only want to allow you to open a door in game, if you have a key. The idea would
be
IF
player has door key
Then
Door is opened
Key is removed from inventory
Simple enough, right? Conditional logic gives you the control you need to truly establish 'behavior' in
your programs.
The primary way to construct a conditional statement is an if statement.
You're probably kind of already acquainted with the principle behind If statements. In RPG Maker MV,
when you select the event command Conditional Branch, you're essentially creating an if statement.
IF STATEMENTS
//Create a function that will accept a number (x) as an argument. If the numberr is greater than 10, it
will log it to //the console.
function greaterThanTen(x) {
Save that into your .js document and reload your project. Run the function and pass it a number. If the
number you pass is greater than 10, it will log that to the console.
An if statement checks inside of the parenthesis, next to the keyword if and evaluates the condition
within.
An if statement, like a function, has syntax that contains three basic components.
-The if keyword
-The parenthesis
-The curly braces
Inside of the parenthesis is the condition, inside of the curly braces is the code to run if the condition
passes (is true). Just remember that, and you know all you need to know about a condition.
Now a comparison is a refers to what makes up the condition. Two numbers, checked for equality in
some way, or if one is greater or lesser than the other. If one string is equal to another, etc. These are all
comparisons. Even checking a boolean for true or false is comparing it's value to the value true, or the
value false.
Here are some of the operators you'll see with comparisons, and that you'll want to know how to use
If statements are kind of the core of conditional logic, when it comes to OOP. You'll get tons of practice
with them, and you'll see them throughout the rest of these lessons.
If statements are very useful, but consider the last function we wrote, greaterThanTen. It only does
something if the number is greater than 10. Otherwise, you can't tell if the function even ran. Maybe we
could add an if statement to handle what happens if the number is less than 10?
//Create a function that will accept a number (x) as an argument. If the numberr is greater than 10, it
will log it to //the console.
function greaterThanTen(x) {
You could do that, and there's nothing wrong with it that will prevent it from running correctly.
However, there is an easier and more appropriate way to do this.
Imagine we run the function above, as is, and give it the number 20. First, it looks at x to see if it is
greater than 10. 20 is greater than 10, right? So it will run the code iside of the curly braces.
Perfect.
Next, however, it would run the next if statement, to check x again, and see if it passes a condition we
now know will be false.
In this particular case, f the first condition passes, there's no point in running the rest of the function
and checking the opposite condition.
//Create a function that will accept a number (x) as an argument. If the numberr is greater than 10, it
will log it to //the console.
function greaterThanTen(x) {
Now, it will check to see if x is greater than 10, just like before. If it is, it will run the code inside the
initial set of curly braces.
It won't run the code inside of the else curly braces.
Imagine someone was at the store, and called you to see if you wanted something. Maybe you'd say
IF they have King Size Reese's Cups, grab me a pack.
OTHERWISE get me a Snickers
Do one thing if the condition passes, do something else if it doesn't. Pretty simple, right?
ELSE IF
There is another variant of the if/else conditional statement. Take a look at the example below
function greaterThanTen(x) {
An else if statement is added the same way an else statement is added. However, and else if statement
comes with it's own set of parenthesis in which it checks a condition.
There are different ways to incorporate if, else and else if logic, and the statements can even be nested
inside one another. Showing nested if-statements can look ver confusing in the beginning, even though
they aren't at all. It's something that will be easy to understand when it comes up naturally, and when
you're comfortable with the if-else-else if structures.
Wrap up
This lesson was kind of short. I could have gone more in depth with nesting, and teaching you about the
Switch statement. You could look these up on your own. Nesting, for sure you would run into on your
own. It's just putting a conditional statement inside the curly braces of another conditional statement.
This can be done multiple times, creating a scenario where the structure is
if a is true, check b
if b is true, check c
if c is true etc
In doing this, you can make it so multiple conditions must pass before code can be executed. You can do
a lot more with it than just that, but I'm going to avoid covering it here.
The reasons is this: It's one of those things that's simple to understand, but messy to show/explain when
you're first learning it.
As far as Switch statements, you could look them up if you wanted. I'm skipping them because it's sort of
the same situation I just described, but more because they're more flexible in one way and very rigid in
another. You won't use them often. If statements and it's variants, you'll use constantly.