博客
关于我
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/

    你可能感兴趣的文章
    postman进行http接口测试
    查看>>
    Postman高阶技能:Collection集合批量运行!
    查看>>
    postMessage跨标签页共享数据
    查看>>
    QImage对一般图像的处理
    查看>>
    post为什么会发送两次请求?
    查看>>
    Post表单提交TextArea的值出现转译乱码问题 - Spring MVC处理表单提交
    查看>>
    Power BI 中的 Python 可视化需要什么设置?任何特定的 matplotlib 包版本或系统设置?
    查看>>
    Power BI:如何在 Power Query 编辑器中将 Python 与多个表一起使用?
    查看>>
    power english (3) main text -emotion mastery - focus
    查看>>
    POWER ENGLISH (6) - MODEL
    查看>>
    power english (1) —— passion
    查看>>
    Power English (1) 原文
    查看>>
    power English (3)原文
    查看>>
    POWER ENGLISH(7)- repetition
    查看>>
    SpringBoot中集成SpringBatch详细解析与实战示例(CSV文件读取十万条数据进行业务处理后写入Mysql数据库)
    查看>>
    powerbi 一张表在另外一张表中出现的数量_PowerBi之初步学习笔记
    查看>>
    QGIS怎样设置简体中文以及新建可编辑的多边形的图层
    查看>>
    PowerBuilder 使用自定义事件触发键盘Enter事件
    查看>>
    PowerCreatorCMS UploadResourcePic 任意文件上传漏洞复现
    查看>>
    PowerDesigner 使用的一些技巧(转)
    查看>>