| Home > Computer & I'net > Conditionals... |
|
|
|||||||||||||||
|
|
|||||||||||||||
|
||||||||||||||||
Conditionals | |||||||||||||||
IF will execute a statement block upon a true expression value.
If the expression had a false value, then that statement block would not be executed and the script would go onto the next line of code. What about the times when you want a different statement block to be executed during a false value and THEN go onto the next line of code? That is where the ELSE addon comes in.
Quick example :
Result : a is not equal to 10. ELSIF statements can be used when you have more than one expression to evaluate and to select the correct statement block to execute. When any statement block within the control structure is executed, the control is taken out of the IF area and continues onto the next line of code. The balance of the IF area is ignored. You can have as many elsif statements in an control structure as required. *NOTE* : be sure to spell ELSIF correctly! There is no E in the middle as in ELSEIF.
If the IF expression is true, it will execute the first statement block. If the IF expression is false, it will test the ELSIF expression. If the ELSIF expression is true, it will execute it's statement block. If the ELSIF expression is false, the control continues to the next ELSIF, or ELSE, or end the control structure (whatever is next after the ELSIF). The IF command is mostly used to find a TRUE expression. The UNLESS command is used to find a FALSE expression. You can actually find a false expression using the IF command as well, but that's another story.
The ELSE command can also be used with UNLESS command and will react like it did on the IF command.
WHILE creates a loop effect, so a scalar can be used to control the number of loops like a counter.
While the value of $counter is less than 25, a sentence is printed. The value of $counter is increased by one and the expression is re-evaluated. The loop (re-evaluation) will continue until a FALSE result breaks it. UNTIL is the opposite of WHILE, but the end result is very similar. It will repeat a statement block "until" the expression is true.
Compare the WHILE and UNTIL examples. If you test them out, you will notice that the WHILE did 10 loops and UNTIL did 11 loops. The choice of which one to use will depend on your specific application and the result you are looking for. The DO WHILE conditional statement allows you to execute a statement block at least one time and continue its loop as long as the expression remains true. The expression is evaluated at the end of the loop instead of at the start.
The DO WHILE comes in handy if you want a specific block executed at least once no matter what the value holds. Change the last line to while ($counter<10); and see what happens. Notice there is a semicolon added at the end of the WHILE line. This shows it belongs to a DO WHILE loop instead of a regular WHILE loop. The FOR conditional provides a shorthand method of specifying how many times a statement block is to be executed. It follows a different layout than the above conditional statements :
The looping control is all kept in the expression area instead of within the statement block area. Nice, tidy and in control. Don't let those foo words spoil the fun. Initial is just where you want the loop to start. Expression is just like you've seen in all the other examples. Iterator is the counter part.
During each loop, the conditional statement is re-evaluated, the iterator re-increases, and so forth until the conditional statement is false. FOREACH is often used to find values within an array structure. The control will go to each value within a specified array, assign that value to a temporary scalar, and use the value for whatever purpose.
So the original value of $x was not altered. The $x in the FOREACH loop is a separate $x of the main script even though it holds the same name. This is known as a local variable. The outer $x is called a global variable. For an easier approach, you can take out the scalar variable and an implied variable will be used.
The first print assumes there is a variable holding a value and will print it. Perl automatically knows you are referring to an "implied" variable when you don't give the print command any variables. The second print statement is just entering a line break for easier output viewing.
----------------- Article by David Stanley. Visit his site http://www.htmlite.com. Reprinted with permission. | |||||||||||||||
| Articles |
•Auto & Trucks•Business•Computer & I'net·General·Apache·CSS·Database·Hardware·HTML·Javascript&DHTML·Linux·MySQL·Operating SystemPerl / CGI·PHP·Programming·Publishing·Search Engines·Software Problems·SSI·Tips & Tricks·Utilities·Web Design•Family•Food & Drink•Gardening•Health•Other•Pets•Psychology•Spiritual•Travel•Women |
| Calculators |
|