Unity Game Dev : create a Player Bounds with “if statement”! (Part II)
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”.
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
= = (equal)
> = (greater 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
With &&, the code runs ONLY IF BOTH OF CONDITION ARE TRUE
INSTEAD, when we use ||
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.