Perl control structure condition control if while

Source: Internet
Author: User

I. Condition Determination
If (<expression> ){
<Statement_block_1>
}
Elsif (<expression> ){
<Statement_block_2>
}
...
Else {
<Statement_block_3>
}
Ii. Loop:
1. While Loop
While (<expression> ){
<Statement_block>
}
2. Until Loop
Until (<expression> ){
<Statement_block>
}
3. For Loop of class C, such
For ($ COUNT = 1; $ count <= 5; $ count ++ ){
# Statements inside the loop go here
}
The following is an example of using the comma operator in a 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. Loop for each element in the list (array): foreach. Syntax:
Foreach localvar (listexpr ){
Statement_block;
}
Example:
Foreach $ word (@ words ){
If ($ word EQ ""){
Print ("found the word 'The' \ n ");
}
}
Note:
(1) The loop variable localvar is a local variable. If it already has a value before, it will be restored after the loop.
(2) Change the local variable in the loop, and the corresponding array variable will also change, for example:
@ List = (1, 2, 3, 4, 5 );
Foreach $ temp (@ list ){
If ($ temp = 2 ){
$ Temp = 20;
}
}
At this time, @ list has been changed to (1, 20, 3, 4, 5 ).
5. Do Loop
Do {
Statement_block
} While_or_until (condexpr );
The do loop must be executed at least once.
6. Loop Control
The exit loop is last, which is the same as the break in C. Execute the next loop as next, which is the same as the continue in C. A special command of Perl is redo, which means to repeat this loop, that is, the loop variable remains unchanged and returns to the cycle start point. Note that the redo command does not work in the do loop.
7. Traditional goto label statements.

3. Single Row Condition
The syntax is statement keyword condexpr. The keyword can be if, unless, while, or until, for example:
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 condition judgment is written later, it is executed first.

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.