Unity Game Dev : create a Player Bounds with “if statement”! (Part II)

Matteo Lo Piccolo
3 min readMay 6, 2021

--

In the previous lesson we saw when the condition inside “If statement” is true, the code is executed, but if the condition is false nothing happens.

But in the PseudoCode, we have two option right?
If is greater or is if is less.
And to do that, we need another keyword, “ELSE”.

The else statement run the code when the condition inside IF is false

In this example the condition is true, but if we change my Age to 15 in the inspector

and press Play, look what’s happen

The “else statement” code is executed.

This is how work the “IF / ELSE” statement.
The code check the if condition, if is true run that code, if no run the ELSE code.

BUT, there is an extra.
In the IF statement you can have more than TWO condition to check.
We make an example for clarify the concept.

Add one condition just for fun :
if we have 25, Debug.Log(“I’m an expert already!”)

The syntax is nearly the same

We use the “ELSE IF” statement when we have more than one condition to check.

Before ending this topic, we talk about CONDITION and the syntax.

We can check the condition with this sintax
> (greater)

> (greater)

< (minor)

< (minor)

= = (equal)

= = (equal)

> = (greater or equal)

> = (greater or equal)

< = (minor or equal)

< = (minor or equal)

We have another TWO symbol,
&& (AND) and || (OR)
&& literally means AND, || literally means OR

We use && when we have to compare more than one condition AT THE SAME TIME

Example : I have 3 numbers, and I check if first is greater than second but minor than third

Here the code runs because 5 > 2 AND 5 < 10

With &&, the code runs ONLY IF BOTH OF CONDITION ARE TRUE

INSTEAD, when we use ||

Here the code runs again because 2 IS NOT > 5 BUT 10 > 2

With ||, the code runs ONLY IF ONE OF CONDITION ARE TRUE

This is the logic, && both true, || only one must be true

And you can have more than two option inside a single IF condition

This is the simple example to how and when use “IF /ELSE” statement.

--

--

Matteo Lo Piccolo
Matteo Lo Piccolo

Written by Matteo Lo Piccolo

Always in love with programming, even if late (I'm already 39 years old) I decided to follow my dream! We will see how far my passion will take me!

No responses yet