Perl-11-loop Modifier

Source: Internet
Author: User

A nested loop structure is often used in programming. Sometimes, a method is required to interrupt it. Perl provides the following four loop modifiers: continue, next, last, redo;

1. Continue statement block:

The incremental list of the for loop is executed after the loop body. The function of the continue statement block is equivalent to the incremental list of the loop; you can add the continue statement block to the while loop, until loop, and foreach loop:

◆ WHILE LOOP

While (conditional expression)

{

# While loop body

}

Continue

{

# Continue program block; you can execute the incremental list of the while loop to operate the loop control variable of the while loop;

}

◆ Until Loop

Until (conditional expression)

{

# Until loop body

}

Continue

{

# Continue program code block;

}

◆ Foreach Loop

Foreach scalar (list)

{

# Foreach loop body

}

Because the continue statement block type is another way to implement incremental list, there cannot be a continue statement block in the for loop. The following statements are incorrectly written and have syntax errors:

For (expression 1; expression 2; expression 3)

{

# For Loop body

}

Continue

{

# Continue statement Block

}

When using the continue statement blocks, next, last, and redo modifiers at the same time, follow the following rules:

★The next loop modifier stops the execution of the statement from the next statement of the next statement to the end identifier of the loop body, and forwards the execution of the continue statement block, then return to the starting point of the loop body and start executing the next loop;

★The last loop modifier completely exits from its loop statement block, ending the loop. The statements after the last statement are no longer executed, and the continue statement block is no longer executed;

★The redo loop modifier is directly transferred to the first line of the loop body to start executing the loop again. The statement after the redo statement is no longer executed, and the continue statement block is no longer executed;

2. last statement:

The last statement can contain a label modifier or a label modifier, for example, last or last label;

The last statement with the label modifier indicates to exit the statement block associated with the label modifier; the last statement without the label modifier indicates the current statement block where the last statement is located; if it is in a for loop or contains a continue statement block, the incremental list or continue statement block in the for loop is no longer executed;

3. Redo statement:

The redo statement can contain a label modifier or a label modifier, such as redo Or redo label;

The redo statement with the label modifier indicates that the loop control flow is directly transferred to the first line of the statement block associated with the label modifier to start execution, instead of executing the redo statement and the continue statement block. The redo statement without the label modifier indicates that the loop control flow is directly transferred to the first line of the current statement block for execution, the statement and the continue statement block after the redo statement is no longer executed. If it is in a for loop or contains a continue statement block, the incremental list and the continue statement block in the for loop are no longer executed;

4. Next statement:

The last and redo loop modifiers can be used for empty statement blocks, while the next statement can only be used for the loop body. The next statement indicates the statement after the next statement is stopped and then the continue statement block is executed, finally, go to the first line of the loop body to start execution. Next can contain a label modifier or a label modifier, for example, next; or next label;

The next statement without a label modifier transfers the cycle control flow to the first line of the current cycle for execution. The next statement with a label modifier exits the cycle associated with the label modifier;

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.