We just saw the first example of what's known as a controlled structure in HP the far each loop in this
section will learn about other control structures.
HP provides.
AP HP script is made up of a series of instructions are statements and instruction can be assigning
of value to a variable like this a function call like this on a loop like this.
Instructions usually end with a semicolon.
We also saw that we can group statements into a block like we did in the Far East loop by enclosing
them in curly braces like this when iph prescript is executed these instructions are executed in ardour
one at a time.
However you might not always want this by using the if control structure we can perform different actions
based on different conditions.
We use it like this.
First we put it off.
Then the condition in brackets.
Then the cold we want to run.
If the condition is true contained inside curly braces the condition is a boolean value.
True are false.
If it's true the cold inside of the block is executed if it's false it isn't.
Let's look at an example.
We'll add an if statement and a block of cold inside the codebook will just echo out a message.
If we take a look at this in the browser we see the message printed out.
Now if we change the condition to false and then go back to the browser we don't see the message.
This is because the condition is false so the code block isn't entered into.
And this line of code isn't executed.
In practice you'd never have a literal boolean value here.
You'd have a variable are the result of a comparison which we'll see next.
So let's change this to a more useful example.
First let's add an array with no elements in it which you can do like that.
The empty function will return a bewley in value depending on whether a variable is empty or not.
Let's just dump this out so you can see like that.
And in the browser we get the boolean value true
because the array is empty.
So this function has returned true.
Let's change our IF condition to use this along with the message like that.
Now if we run this code we get the message saying the array is empty.
So the empty function here returned true.
So the cold inside this block was executed.
If we add some elements to the array and then run the code again the empty function has returned false
and therefore the message isn't shown.
We can also execute some code if the condition is false by using else.
Else extends an IF statement to run some code.
If the condition is FALSE.
Let's add else to our IF statement along with a message to print out.
If the condition is FALSE let's also empty the array.
And now if we look at this in a browser.
First we see the message that the array is empty.
And then if we add some elements back in the array and then look at this in the browser we get the message
coming from the ELSE code block.
One more thing I need to mention about if statements if you only have one statement in a cold block
you can omit the curly braces.
However this is generally bad practice as if later you want to have more than one statement and you
forget to add the curly braces.
You can end up with unexpected results.
It's best practice to always include the curly braces like this.