博客
关于我
Perl基础学习03之流程控制结构
阅读量:393 次
发布时间:2019-03-05

本文共 3668 字,大约阅读时间需要 12 分钟。

布尔值及逻辑运算符

在编程中,布尔值是最基础的数据类型之一,它用来表示真值(True)或假值(False)。在 Perl 语言中,布尔值的判断规则非常简单明确,以下是具体的判断规则:

  • 数字标量:0 为假值,其他数字(包括正数、负数、零)为真值。
  • 字符串标量:空字符串 '' 为假值,非空字符串为真值。
  • 其他标量(如数组、哈希等)会自动转化为上述两种类型进行判断。
  • 布尔值的取反操作可以通过 "!" 运算符实现。例如:

    $is_true = "hello"; # $is_true 的值为 "hello"$is_false = "";     # $is_false 的值为 ""$is_true = !$is_false; # $is_true 的值变为 "hello"

    逻辑运算符在 Perl 中的含义和用法如下:

    • and(&&):与运算,两个条件都为真时返回真值。例如:
      $condition1 = $a > $b;$condition2 = $c > $d;if($condition1 && $condition2) {    print "两个条件都满足!";}
    • or(||):或运算,两个条件中只要有一个为真时返回真值。例如:
      $condition1 = $a > $b;$condition2 = $c > $d;if($condition1 || $condition2) {    print "至少一个条件满足!";}
    • not(!):取反运算,将条件的真假值取反。例如:
      $condition = $a > 5;if(!($condition)) {    print "原条件为真,现在变为假!";}

    条件判断是程序运行的核心逻辑, Perl 提供了多种方式来实现条件控制。以下是常用的条件判断方法:

    条件判断1:if....elsif....else

    if(condition1) {    # 当条件1为真时执行代码    do_something();} elsif(condition2) {    # 当条件1为假,条件2为真时执行代码    do_something_else();} else {    # 当条件1和条件2都为假时执行代码    do_finalthing();}

    示例代码

    #!/usr/bin/perluse strict;use warnings;my $str1 = $ARGV[0];my $str2 = $ARGV[1];if($str1 == $str2) {    print "两个输入完全相同!\n";} elsif($str1 > $str2) {    print "第一个输入大于第二个!\n";} else {    print "第一个输入小于第二个!\n";}perl ifelse.pl 1 1 # 输出: 两个输入完全相同!perl ifelse.pl 2 1 # 输出: 第一个输入大于第二个!perl ifelse.pl 2 3 # 输出: 第一个输入小于第二个!

    条件判断2:unless...else....

    unless(condition) {    # 当条件为假时执行代码    do_something();} else {    # 当条件为真时执行代码    do_something_else();}

    示例代码

    #!/usr/bin/perluse strict;use warnings;my $str1 = $ARGV[0];my $str2 = $ARGV[1];unless($str1 != $str2) {    print "两个输入完全相同!\n";} else {    print "两个输入不相同!\n";}perl unless1.pl 1 1 # 输出: 两个输入完全相同!perl unless1.pl 1 2 # 输出: 两个输入不相同!

    条件判断3:三目运算符

    expression ? true_value : false_value

    如果 expression 为真,则整个表达式返回 true_value;如果 expression 为假,则返回 false_value。

    示例代码

    #!/usr/bin/perluse strict;use warnings;my $result = ($ARGV[0] == $ARGV[1] ? "输入相同" : "输入不同");print "$result\n";perl ternary_operator.pl 1 2 # 输出: 输入不同perl ternary_operator.pl 1 1 # 输出: 输入相同

    循环是程序中常用的控制结构,用于重复执行特定的代码块。 Perl 提供了多种循环方式,以下是常用的循环控制结构:

    循环1:while

    while(condition) {    # 当条件为真时,执行循环体    do_something();}

    示例代码

    #!/usr/bin/perluse strict;use warnings;my $in = 5;while($in < 10) {    print "$in\n";    $in += 2;}perl while1.pl # 输出: 5, 7, 9

    循环2:for

    for(start; expression; expression) {    # 当 expression 为真时,执行循环体    do_something();}

    示例代码

    #!/usr/bin/perluse strict;use warnings;for(my $i = 5; $i < 10; $i += 2) {    print "$i\n";}perl for1.pl # 输出: 5, 7, 9

    循环3:foreach

    foreach (range) {    # 遍历 range 中的每一个元素    do_something();}

    示例代码

    #!/usr/bin/perluse strict;use warnings;foreach (5..10) {    print "$_\n";}foreach my $i (5..10) {    print "$i\n";}perl foreach1.pl # 输出: 5, 6, 7, 8, 9, 10

    循环4:each(用于遍历哈希)

    while(each %hash) {    # 遍历哈希中的每一个键值对    print "$k\t$v\n";}

    示例代码

    #!/usr/bin/perluse strict;use warnings;my %hash = (    "apple" => "fruit",    "tomat" => "vegetables",    "tomat1" => "vegetables");while(my $k, $v = each %hash) {    print "$k\t$v\n";}perl while_each1.pl # 输出: tomat vegetables, apple fruit, tomat1 vegetables

    循环控制模块:last、next、redo

  • last:退出当前层次的循环,不会退出外层循环。
  • next:跳过本次循环,进入下一轮循环。
  • redo:忽略之后的语句,重新执行本次循环。
  • 示例代码

    #!/usr/bin/perluse strict;use warnings;my $in = 2;foreach (1..20) {    last if $_ % $in == 0;    print "$_\n";}perl last1.pl # 输出: 2, 4, 6, 8, 10, 12, 14, 16, 18, 20
    #!/usr/bin/perluse strict;use warnings;my $in = 2;foreach (1..20) {    next if $_ % $in == 0;    print "$_\n";}perl next1.pl # 输出: 1, 3, 5, 7, 9, 11, 13, 15, 17, 19
    #!/usr/bin/perluse strict;use warnings;my @array;for my $i (1..3) {    push @array, $i;    redo if(@array == 2);}print "@array\n";perl redo1.pl # 输出: 1 2 2 3

    表达式后面加流程控制语句:

    print "True.\n" if $a > $b;

    参考资料:

    转载地址:http://cbmwz.baihongyu.com/

    你可能感兴趣的文章
    Objective-C实现perfect number完全数算法(附完整源码)
    查看>>
    Objective-C实现perfect square完全平方数算法(附完整源码)
    查看>>
    Objective-C实现permutate Without Repetitions无重复排列算法(附完整源码)
    查看>>
    Objective-C实现PNG图片格式转换BMP图片格式(附完整源码)
    查看>>
    Objective-C实现pollard rho大数分解算法(附完整源码)
    查看>>
    Objective-C实现Polynomials多项式算法 (附完整源码)
    查看>>
    Objective-C实现power iteration幂迭代算法(附完整源码)
    查看>>
    Objective-C实现powLinear函数和powFaster函数算法 (附完整源码)
    查看>>
    Objective-C实现PrimeFactors质因子分解算法 (附完整源码)
    查看>>
    Objective-C实现pythagoras哥拉斯算法(附完整源码)
    查看>>
    Objective-C实现qubit measure量子位测量算法(附完整源码)
    查看>>
    Objective-C实现quick select快速选择算法(附完整源码)
    查看>>
    Objective-C实现radians弧度制算法(附完整源码)
    查看>>
    Objective-C实现radianToDegree弧度到度算法(附完整源码)
    查看>>
    Objective-C实现radix sort基数排序算法(附完整源码)
    查看>>
    Objective-C实现rail fence围栏密码算法(附完整源码)
    查看>>
    Objective-C实现rayleigh quotient瑞利商算法(附完整源码)
    查看>>
    Objective-C实现RC4加解密算法(附完整源码)
    查看>>
    Objective-C实现recursive bubble sor递归冒泡排序算法(附完整源码)
    查看>>
    Objective-C实现recursive insertion sort递归插入排序算法(附完整源码)
    查看>>