You are on page 1of 5

Conditional Logic

Monday, August 12, 2019 5:41 PM

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

There are two main parts to a basic If statement.


-The condition
-The code to execute.

Let's cut straight to an example.


We'll put it inside of a function, for testing puposes.

//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) {

//Write an 'If' statement to check if x is greater than 10


if(x > 10){
console.log(x + " is greater than 10");
}
}

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.

If statements are pretty straightforward, as far as logic goes.


If the condition inside of the parenthesis is true,
the code inside the curly braces will execute.
If not, the code inside the braces is skipped.

CONDITIONS AND COMPARISONS

An if statement checks inside of the parenthesis, next to the keyword if and evaluates the condition
within.

RMMV Tutorials Page 1


within.
A condition is a statement that will evaluate to either true or false.
if(5 > 1)
if(1 + 1 == 2)
if(myBoolean == true)
if($gamePlayer.characterName() == "Actor1")

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

Operator Name Description Example


> Greater Than Evaluates to true if the number to the left is greater than 10 > 4 (true)
the number on the right (of the > operator)
< Less Than Evaluates to true if the number to the left is less than the 10 < 4 (false)
number on the right
>= Greater than Evaluates to true if the number to the left is greater than 10 >= 10
OR Equal To OR equal to the number on the right (true)
<= Less than OR Evaluates to true if the number to the left is less than OR 10 <= 10
Equal To equal to the number on the right (true)
== Equal To Evaluates to true if the value(string, number, boolean, etc) 10 == '10'
on the left is equal to the value on the right (true)
=== Equal To Strict Evaluates to true if the value on the left is equal to AND of 10 === '10'
the same data type as the value on the right (false)
!= Not Equal To Evaluates to true if the value on the left is NOT equal to the 10 !== '10'
value on the right (true)
&& And Special operator that links two conditions together. Left 4 > 2 && 4
AND Right conditions must be true for the entire condition < 3 (false)
to evaluate to true
|| Or Special operator that links two conditions together. Left OR 4 > 2 || 4 < 3
Right condition must be true for the entire condition to (true)
evaluate to true

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.

RMMV Tutorials Page 2


Else Statement

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) {

//Write an 'If' statement to check if x is greater than 10


if(x > 10){
console.log(x + " is greater than 10");
}

//Write a second 'if' statement to check if x is less than 10


if(x < 10{
console.log(x + " is less than 10";
}
}

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.

So let's instead add an else statement

//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) {

//Write an 'If' statement to check if x is greater than 10


if(x > 10){
console.log(x + " is greater than 10");
} else {
console.log(x + " is less than 10");
}
}

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.

RMMV Tutorials Page 3


If the condition evaluates to false, however, like if we passed the function the number 4, it will skip the
initial curly braces, and run the code inside of the else braces instead.

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) {

//Write an 'If' statement to check if x is greater than 10


if(x > 10){
console.log(x + " is greater than 10");
} else if(x < 10) {
console.log(x + " is less than 10");
} else {
console.log(x + " equal to 10");
}
}

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.

RMMV Tutorials Page 4


another. You won't use them often. If statements and it's variants, you'll use constantly.

So that's all for this lesson.


The next lesson will be a bit longer, a bit confusing, but if you stick with me, and follow my example
excercises, it's gonna be the thing that brings everything you've learned so far together, and leave you
with enough knowledge to really start making some things happen on your own.

It won't be the last lesson, but probably the second to last.


The next lesson will be on Arrays and Loops.

RMMV Tutorials Page 5

You might also like