个性化阅读
专注于IT技术分析

Perl循环:foreach、foreach、while、do…while、until嵌套循环

编程语言中的循环是一项功能, 可在某些条件评估为真时, 促进重复执行一组指令或功能。循环使程序员的任务更简单。 Perl提供了不同类型的循环来处理程序中基于条件的情况。 Perl中的循环是:

  • for循环
  • foreach循环
  • while循环
  • do-while循环
  • until循环
  • 嵌套循环

循环

” for”循环提供了编写循环结构的简洁方法。与while循环不同, for语句在一行中消耗了初始化, 条件和增量/减量, 从而提供了更短, 更易于调试的循环结构。

语法如下:

for (init statement; condition; increment/decrement ) 
{
    # Code to be Executed
}

流程图:

Perl循环:foreach、foreach、while、do…while、until嵌套循环

for循环适用于预定义的控制流。控制流可以通过以下方式确定:

  • 初始化语句:这是第一个执行的语句。在此步骤中, 我们初始化一个控制循环的变量。
  • 健康)状况:在此步骤中, 评估给定条件, 如果条件为True, 则for循环运行。这也是进入控制回路因为条件是在执行循环语句之前检查的。
  • 语句执行:一旦条件评估为真, 就执行循环主体中的语句。
  • 增减:在此更改循环控制变量(递增或递减), 以更新变量以用于下一次迭代。
  • 循环终止:当条件变为假时, 循环终止, 标志其生命周期的结束。

范例:

# Perl program to illustrate
# the for loop
  
# for loop
for ( $count = 1 ; $count <= 3 ; $count ++)
{
     print "lsbin\n"
}

输出如下:

lsbin
lsbin
lsbin

foreach循环

一个foreach循环用于遍历一个列表, 并且变量一次保存一个列表的元素的值。当我们在列表中有一组数据并且想要遍历列表的元素而不是遍历其范围时, 主要使用它。每个元素的迭代过程由循环自动完成。

语法如下:

foreach variable 
{
    # Code to be Executed
}

流程图:

Perl循环:foreach、foreach、while、do…while、until嵌套循环

例子:

# Perl program to illustrate
# the foreach loop
  
# Array
@data = ( 'GEEKS' , 'FOR' , 'GEEKS' );
  
# foreach loop
foreach $word ( @data )
{
     print $word
}

输出如下:

lsbin

while循环

while循环通常在括号中使用一个表达式。如果表达式为True, 则执行while循环体内的代码。当我们不知道要执行循环的次数但知道循环的终止条件时, 使用while循环。也被称为进入控制回路因为条件是在执行循环之前检查的。 while循环可被视为重复的if语句。

句法 :

while (condition)
{
    # Code to be executed
}

流程图:

while_loop_perl

范例:

# Perl program to illustrate
# the while loop
  
# while loop
$count = 3;
while ( $count >= 0)
{
     $count = $count - 1;
     print "lsbin\n" ;
}

输出如下:

lsbin
lsbin
lsbin
lsbin

无限While循环:while循环可以执行无限次, 这意味着此循环没有终止条件。换句话说, 我们可以说某些条件始终保持为真, 这会导致while循环执行无限次, 或者可以说它永远不会终止。

例子:

下面的程序将无限期打印指定的语句, 并给出运行时错误为

超出输出限制

在在线IDE上

# Perl program to illustrate
# the infinite while loop
  
# infinite while loop
# containing condition 1 
# which is always true
while (1)
{
     print "Infinite While Loop\n" ;
}

输出如下:

Infinite While Loop
Infinite While Loop
Infinite While Loop
Infinite While Loop
.
.
.
.

做…。 while循环

do..while循环与while循环几乎相同。唯一的区别是do..while循环至少运行一次。第一次执行后将检查条件。当我们希望循环至少运行一次时, 使用do..while循环。也被称为退出控制回路因为条件是在执行循环后检查的。

语法如下:

do {

    # statments to be Executed

} while(condition);

流程图:

do-while

范例:

# Perl program to illustrate 
# do..while Loop
  
$a = 10;
  
# do..While loop
do {
  
     print "$a " ;
     $a = $a - 1;
} while ( $a > 0);

输出如下:

10 9 8 7 6 5 4 3 2 1

until循环

until循环与while循环相反。它在括号中带有一个条件, 并且仅在条件为假之前运行。基本上, 它重复一条指令或一组指令, until条件为FALSE。这也是入口控制器循环, 即首先检查条件, 然后执行块内的指令集。

语法如下:

until (condition) 
{
   # Statements to be executed
}

流程图:

until_loop_perl

范例:

# Perl program to illustrate until Loop
  
$a = 10;
  
# until loop
until ( $a < 1)
{
     print "$a " ;
     $a = $a - 1;
}

输出如下:

10 9 8 7 6 5 4 3 2 1

嵌套循环

嵌套循环是循环内部的循环。 Perl编程还支持嵌套循环。并且所有上面讨论的循环都可以嵌套。

Perl中不同的嵌套循环的语法:

  • 嵌套循环
    for (init statement; condition; increment/decrement ) 
    {
        for (init statement; condition; increment/decrement ) 
        {
             # Code to be Executed
        }
    }
  • 嵌套的foreach循环
    foreach variable_1 (@array_1) {
    
        foreach variable_2 (@array_2) 
       {
    
           # Code to be Executed
       } 
    }
    
  • 嵌套while循环
    while (condition)
    {
        while (condition)
        {
            # Code to be Executed
        }
    }
  • 嵌套的do..while循环
    do{
        do{
    
            # Code to be Executed
    
           }while(condition);
    
    }while(condition);
    
  • 嵌套until循环
    until (condition) {
    
        until (condition) 
        {
           # Code to be Executed
        }
    }

范例:

# Perl program to illustrate
# nested while Loop
  
$a = 5;
$b = 0;
  
# outer while loop
while ( $a < 7) 
{
    $b = 0;
     
    # inner while loop
    while ( $b <7 ) 
    {
       print "value of a = $a, b = $b\n" ;
       $b = $b + 1;
    }
     
    $a = $a + 1;
    print "Value of a = $a\n\n" ;
}

输出如下:

value of a = 5, b = 0
value of a = 5, b = 1
value of a = 5, b = 2
value of a = 5, b = 3
value of a = 5, b = 4
value of a = 5, b = 5
value of a = 5, b = 6
Value of a = 6

value of a = 6, b = 0
value of a = 6, b = 1
value of a = 6, b = 2
value of a = 6, b = 3
value of a = 6, b = 4
value of a = 6, b = 5
value of a = 6, b = 6
Value of a = 7
赞(0)
未经允许不得转载:srcmini » Perl循环:foreach、foreach、while、do…while、until嵌套循环

评论 抢沙发

评论前必须登录!