perl控制流介绍(if条件,while,for循环,foreach)

1. 语句块:
{ }之间的部分即为BLOCK语句块。express

2. 条件语句:
if ( expression )  BLOCK;rem

if ( expression )  
   BLOCK1
else BLOCK2;字符串

if ( expression1 )  
   BLOCK1;
elsif ( expression2 ) 
   BLOCK2;
else
    BLOCK3;it

#倒置的if语句
expression if ( test_expression );io

涉及到的关系运算符:
数字值比较:==, >, <, >=, <=, != ;注意用数字值比较运算符比较字符串时,字符串看成0处理;
字符串比较: eq, gt, lt, ge, le, ne;(undef被看成假来处理)
逻辑运算符:&&, ||, 和! ; and, or, not等。ast

3. 循环:
while (expression)
BLOCK;
dotest

BLOCK
while(expression);foreach

for( initialization; test; increment) 
BLOCK;循环

foreach $each (@list)
BLOCK;程序

4. 其余last:最后一次了,跳出当前BLOCK,紧接BLOCK以后的代码往下。next:这一次到此为止,对本BLOCK开始下一轮。标号:相似于goto。last 和 next 一般和 if 倒置语句或者标号结合使用,实现跳转。exit语句: exit 0; 结束当前的Perl程序,返回OS;注意:Perl中没有switch语句,使用if-else来模仿。

相关文章
相关标签/搜索