|
From now on we are going to provide with more "dynamism" our scripts since from diverse structures we will indicate that action must realize in every case, also also it will give us the possibility of realizing the same action multitude of times with only a line of code.
Structure IF
IF is a control structure used to take decisions as there is fulfilled a condition (or several) or not. His basic structure is the following one:
if (condition / is) {
action to realize;
}
else {
action to realize in case it is not fulfilled;
}
Let's see a basic example to understand it better:
if ($edad> =18) {
To buy beer;
}
else {
I begin "you cannot buy beer because you are not 18 years old";
}
and we even can realize conditional more compline as the following case:
if (($edad> =18) && ($dinero> 0)) {
You can buy beer because you have 18 and your money it is major than 0;
}
else {
I begin "Or you do not have peel or do not have the 18";
}
He structures SWITCH
It takes different decisions according to the different states of the variable. His syntax is the following one:
switch (expression) {
marry valor1:
judgment to be executed when the expression has like value valor1
break
marry valor2:
judgment to be executed when the expression has like value valor2
break
marry valor3:
judgment to be executed when the expression has like value valor3
break
default:
judgment to be executed for defect when there is not fulfilled any of the previous conditions
Ringlet FOR
The ringlet for is used to repeat the same operation a certain number of times. His syntax is the following one:
for (initialization; condition; update) {
judgment to be executed while the condition is fulfilled
}
The ringlet for is composed of 3 parts:
- Initialization: It is executed just on having initiated for the first time the ringlet. In this part it usually place the variable that was counting the number of times that repeats the ringlet.
- Condition: It is the condition that was evaluated whenever the ringlet begins. This condition is the one that determines the duration of the ringlet.
- Update: It serves to indicate the changes that we want to execute in the variables whenever the ringlet is executed.
An example of his use would be the following one:
for ($i=1; i <=10; i ++) {
I begin "The current number is".$i;
}
Thus he would write all the numbers contained between 0 and 10.
Ringlets WHILE and DO WHILE
Ringlet WHILE
This ringlet is used when we want to repeat the execution of a few judgments an indefinite number of times. His syntax is the following one:
while (condition) {
judgment to be executed
}
To understand better the use of while we will make use of the following example:
while ($color! = "red") {
color = give me a color;
}
This is an example of what it is possible to do with while. In this case any time the color should not be red he will say to us that we should introduce a color.
Ringlet DO... WHILE
This ringlet is used when we do not know the number of times that is going to execute a ringlet but what if we realize well is that at least once if that was executing the action. His syntax is the following one:
do {
judgment of the ringlet
} while (condition)
BREAK and I CONTINUED
BREAK
It is used to stop the ringlet and to stop interpreting the code that continues after the break
I CONTINUED
It serves to return at the beginning of the ringlet from any part of the ringlet.
|