首页 > 编程语言 >无涯教程-Perl - 嵌套循环函数

无涯教程-Perl - 嵌套循环函数

时间:2023-08-05 15:33:09浏览次数:45  
标签:while value 无涯 嵌套 嵌套循环 statement Perl condition

一个循环可以嵌套在另一个循环中, Perl允许嵌套所有要嵌套的循环。

nested loops - 语法

Perl中嵌套for循环语句的语法如下-

for ( init; condition; increment ) {
   for ( init; condition; increment ) {
      statement(s);
   }
   statement(s);
}

Perl中嵌套while循环语句的语法如下-

while(condition) {
   while(condition) {
      statement(s);
   }
   statement(s);
}

Perl中嵌套do...while循环语句的语法如下-

do{
   statement(s);
   do{
      statement(s);
   }while( condition );

}while( condition );

Perl中嵌套直到循环语句的语法如下:

until(condition) {
   until(condition) {
      statement(s);
   }
   statement(s);
}

Perl中嵌套的foreach循环语句的语法如下-

foreach $a (@listA) {
   foreach $b (@listB) {
      statement(s);
   }
   statement(s);
}

nested loops - 示例

以下程序使用嵌套的 while 循环显示用法-

#/usr/local/bin/perl
   
$a=0;
$b=0;

# outer while loop
while($a < 3) {
   $b=0;
   # inner while loop
   while( $b < 3 ) {
      print "value of a=$a, b=$b\n";
      $b=$b + 1;
   }
   $a=$a + 1;
   print "Value of a=$a\n\n";
}

这将产生以下输出-

value of a=0, b=0
value of a=0, b=1
value of a=0, b=2
Value of a=1

value of a=1, b=0
value of a=1, b=1
value of a=1, b=2
Value of a=2

value of a=2, b=0
value of a=2, b=1
value of a=2, b=2
Value of a=3

参考链接

https://www.learnfk.com/perl/perl-nested-loops.html

标签:while,value,无涯,嵌套,嵌套循环,statement,Perl,condition
From: https://blog.51cto.com/u_14033984/6975866

相关文章

  • 无涯教程-Perl - foreach 语句函数
    foreach循环遍历列表值,并将控制变量(var)依次设置为列表的每个元素-foreach-语法Perl编程语言中的foreach循环的语法是-foreachvar(list){...}foreach-流程图foreach-示例#!/usr/local/bin/perl@list=(2,20,30,40,50);#foreachloopexecutionf......
  • 无涯教程-Perl - for 语句函数
    for循环是一种重复控制结构,可让您有效地编写需要执行特定次数的循环。for-语法for(init;condition;increment){statement(s);}for-流程图for-例#!/usr/local/bin/perl#forloopexecutionfor($a=10;$a<20;$a=$a+1){print"valueofa:......
  • 无涯教程-Perl - until 语句函数
    只要给定条件为假,Perl编程语言中的until循环语句就会重复执行目标语句。until-语法until(condition){statement(s);}until-流程图直到until循环的关键点是该循环可能永远不会运行。当条件被测试并且输出为true时,将跳过循环主体,并执行直到循环之后的第一条语句......
  • 无涯教程-Perl - unless...elsif..else 语句函数
    除非unless语句后可以跟可选的elsif...else语句,这对于使用单个unless...elsif语句测试各种条件非常有用。unless...elsif..else-语法Perl编程语言中的unless...elsif...else语句的语法是-unless(boolean_expression1){#Executeswhenthebooleanexpression......
  • 无涯教程-Perl - unless 语句函数
    Perl的除非unless语句由一个布尔表达式和一个或多个语句组成。unless-语法Perl编程语言中的exclude语句的语法是-unless(boolean_expression){#statement(s)willexecuteifthegivenconditionisfalse}如果布尔表达式的输出为false,则将执行exclude语句中的代......
  • 无涯教程-Perl - if...elsif...else语句函数
    if语句后可以跟可选的elsif...else语句,这对于使用单个if...elsif语句测试各种条件非常有用。if...elsif...else-语法Perl编程语言中的if...elsif...else语句的语法是-if(boolean_expression1){#Executeswhenthebooleanexpression1istrue}elsif(......
  • 无涯教程-Perl - 包和模块
    package语句将当前命名上下文切换到指定的名称空间。以下是在文件中包含main和Foo软件​​包的示例。在这里,特殊变量__PACKAGE__已用于打印软件包名称。#!/usr/bin/perl#Thisismainpackage$i=1;print"Packagename:",__PACKAGE__,"$i\n";packageFoo;#......
  • 无涯教程-Perl - 数据库访问
    本章教您如何在Perl脚本中访问数据库。从Perl5开始,使用DBI模块编写数据库应用程序变得非常容易。DBI代表Perl的数据库独立接口,这意味着DBI在Perl代码和基础数据库之间提供了一个抽象层,使您可以真正轻松地切换数据库实现。DBI体系结构DBI独立于后端中可用的任何数据库。无论......
  • "No appenders found for logger" and "Please configure log4j properly"
    WhydoIseeawarningabout"Noappendersfoundforlogger"and"Pleaseconfigurelog4jproperly"?Thisoccurswhenthedefaultconfigurationfileslog4j.propertiesandlog4j.xmlcannotbefoundandtheapplicationperformsnoexplici......
  • 无涯教程-Perl - 格式化
    Perl使用称为“formats”的模板来输出内容。要使用Perl的格式函数,必须先定义一种格式,然后才能使用该格式写入格式化的数据。定义格式以下是定义Perl格式的语法-formatFormatName=fieldlinevalue_one,value_two,value_threefieldlinevalue_one,value_two.这里的For......