• perl return和break


    zjzc01:/root/test# cat a3.pl 
    sub mask {
            my $n=shift;
            my $j =100;
             for ($i = 1;$i <= 5;$i++){
             print "$i is $i
    ";
            while ($n=$n+1) {
                    $j++;
              print "$n is $n
    ";
              if ($n==10){print "$n---end is $n
    ";$n=0;last};
            }}
    };
    
    &mask(1);
    zjzc01:/root/test# perl a3.pl 
    $i is 1
    $n is 2
    $n is 3
    $n is 4
    $n is 5
    $n is 6
    $n is 7
    $n is 8
    $n is 9
    $n is 10
    $n---end is 10
    $i is 2
    $n is 1
    $n is 2
    $n is 3
    $n is 4
    $n is 5
    $n is 6
    $n is 7
    $n is 8
    $n is 9
    $n is 10
    $n---end is 10
    $i is 3
    $n is 1
    $n is 2
    $n is 3
    $n is 4
    $n is 5
    $n is 6
    $n is 7
    $n is 8
    $n is 9
    $n is 10
    $n---end is 10
    $i is 4
    $n is 1
    $n is 2
    $n is 3
    $n is 4
    $n is 5
    $n is 6
    $n is 7
    $n is 8
    $n is 9
    $n is 10
    $n---end is 10
    $i is 5
    $n is 1
    $n is 2
    $n is 3
    $n is 4
    $n is 5
    $n is 6
    $n is 7
    $n is 8
    $n is 9
    $n is 10
    $n---end is 10
    
    
    last 是退出当前循环
    
    zjzc01:/root/test# cat a3.pl 
    sub mask {
            my $n=shift;
            my $j =100;
             for ($i = 1;$i <= 5;$i++){
             print "$i is $i
    ";
            while ($n=$n+1) {
                    $j++;
              print "$n is $n
    ";
              if ($n==10){print "$n---end is $n
    ";$n=0;return};
            }}
    };
    
    &mask(1);
    zjzc01:/root/test# perl a3.pl 
    $i is 1
    $n is 2
    $n is 3
    $n is 4
    $n is 5
    $n is 6
    $n is 7
    $n is 8
    $n is 9
    $n is 10
    $n---end is 10
    
    return是退出当前函数
    
    退出循环是break
    return是退出函数并返回, 如果函数有返回值,可以将返回值返回

  • 相关阅读:
    Python学习心得第二周-作业
    Python学习心得第二周-02 字符串、列表、元组、字典
    Python学习心得第二周-01 数字类型
    eclipse 性能调优之内存分配
    Spring 面试
    技巧 linux 如何显示一个文件的某几行(中间几行)
    命令 scp
    机器学习遇到的好的资料
    maven 使用记录
    “冷启动”问题浅析
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13350982.html
Copyright © 2020-2023  润新知