Introduction to perl control flow (if condition, while, for loop, foreach)

Source: Internet
Author: User

1. Statement block:
The parts between {} Are BLOCK statement blocks.

2. Condition Statement:
If (expression) BLOCK;

If (expression)
BLOCK1
Else BLOCK2;

If (expression1)
BLOCK1;
Elsif (expression2)
BLOCK2;
Else
BLOCK3;

# Inverted if statement
Expression if (test_expression );

Relational operators involved:
Numeric Value Comparison: =,>, <, >=, <= ,! =; When comparing strings with numeric values, the string is treated as 0;
String comparison: eq, gt, lt, ge, le, ne; (undef is treated as false)
Logical operators: &, |, and! ; And, or, not, and so on.

3. Loop:
While (expression)
BLOCK;
Do

BLOCK
While (expression );

For (initialization; test; increment)
BLOCK;

Foreach $ each (@ list)
BLOCK;

4. Miscellaneous

Last: The last time, the current BLOCK exists, and the Code following the BLOCK goes down.
Next: This time till now, the next round of this BLOCK is started.
Label: similar to goto.
Last and next are usually used in combination with if inverted statements or labels to achieve redirection.
Exit statement: exit 0; end the current Perl program and return OS;
Note: Perl does not have a switch statement. if-else is used for imitation.

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.