First, the condition judgment
if (<expression>) {
<statement_block_1>
}
elsif (<expression>) {
<statement_block_2>
}
...
else{
<statement_block_3>
}
Second, Cycle:
1, while loop
while (<expression>) {
<statement_block>
}
2, until cycle
Until (<expression>) {
<statement_block>
}
3, Class C for the loop, such as
for ($count =1; $count <= 5; $count + +) {
# statements inside the loop go
}
Here is an example of using the comma operator in the For loop:
for ($line = <stdin>, $count = 1; $count <= 3; $line = <stdin>, $count + +) {
Print ($line);
}
It is equivalent to the following statement:
$line = <STDIN>;
$count = 1;
while ($count <= 3) {
Print ($line);
$line = <STDIN>;
$count + +;
}
4, for the list (array) of each element of the loop: foreach, the syntax is:
foreach Localvar (listexpr) {
Statement_block;
}
Cases:
foreach $word (@words) {
if ($word eq "the") {
Print ("found the word ' \ n");
}
}
Note:
(1) Here the loop variable Localvar is a local variable, and if it already has a value, the value is restored after the loop.
(2) Change the local variable in the loop, the corresponding array variable will also change, such as:
@list = (1, 2, 3, 4, 5);
foreach $temp (@list) {
if ($temp = = 2) {
$temp = 20;
}
}
At this point the @list has become (1, 20, 3, 4, 5).
5. Do Loop
do {
Statement_block
} while_or_until (condexpr);
The Do loop performs at least one loop.
6. Cycle control
The exit loop is last, the same as the break in C; the next loop is the same as the Continue function in C; a Perl-specific command is redo, which means repeating the loop, which is the same as the loop variable, returning to the starting point of the loop, but note that The redo command does not work in the Do loop.
7, the traditional goto label;
Iii. single-line conditions
The syntax is statement keyword CONDEXPR. Where keyword can be if, unless, while, or until, such as:
Print ("This is zero.\n") if ($var = = 0);
Print ("This is zero.\n") unless ($var!= 0);
Print (' not Zero yet.\n ') while ($var-> 0);
Print ("Not Zero yet.\n") until ($var-= = 0);
Although the conditional judgment is written in the back, it is executed first.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.