zoukankan      html  css  js  c++  java
  • [dart学习]第六篇:流程控制语句

    经过前面的基础知识了解学习,我们今天可以进入语句模块啦。

    dart主要有以下流程控制语句:

    • if-else
    • for循环
    • while和do-while循环
    • break和continue
    • switch-case
    • assert
    • 当然,你还可以使用 try-catch或throw

    (一)if-else

    dart的if(或者else if)的条件表达式必须为bool表达式,不能使用其他类型。dart的if-else用法与C语言类似,不再细述。

    int a = 6;
    if(a<0)
    {
        print("aaa");
    }
    else if((a>=0) && (a<=3))
    {
        print("bbb");
    }
    else
    {
        print("ccc");
    }
    

    (二) for循环

    与C语言系的for循环用法相同,不再细述。补充一点:对于List和Set等可迭代类型,也可以使用for-in格式去迭代(有点像python),看个例子:

    var collection = [0, 1, 2];
    for (var x in collection) {
      print(x); // 0 1 2
    }

    就是这样。

    (三) while和do-while

    这两个也不再细述了,和C语言一样。(while循环是先判条件再执行动作;do-while是先执行动作再判循环条件)。

    (四)break和continue

    与C语言一样,break是跳出当前循环,continue是跳过当次循环的剩余语句,继续开始新一次的循环。

    (五)switch-case

    与C语言类似,不再细述。一点特例,dart在switch-case里支持continue:

    var command = 'CLOSED';
    switch (command) {
      case 'CLOSED':
        executeClosed();
        continue nowClosed;
      // Continues executing at the nowClosed label.
    
      nowClosed:
      case 'NOW_CLOSED':
        // Runs for both CLOSED and NOW_CLOSED.
        executeNowClosed();
        break;
    }

    (六)assert

    如果布尔条件为false,则会中断执行。assert语句是有两个参数的

     assert(condition, optionalMessage);     //第二个参数是可选的

    第一个参数可以是返回值为bool的表达式,如果表达式的返回值为true,则assert通过且程序继续正常执行; 如果表达式为false,则assertion失败且抛出一个异常。

    再贴一段官方的注释,讲了assert能有效工作的场景,大家自行阅读吧

    When exactly do assertions work? That depends on the tools and framework you’re using:

    • Flutter enables assertions in debug mode.
    • Development-only tools such as dartdevc typically enable assertions by default.
    • Some tools, such as dart and dart2js, support assertions through a command-line flag: --enable-asserts.

    In production code, assertions are ignored, and the arguments to assert aren’t evaluated.

     

  • 相关阅读:
    Wireshark按照域名过滤
    【黑客免杀攻防】读书笔记12
    【逆向知识】反调试-除0异常-编程与逆向
    JS脚本病毒调试脚本-Trojan[Downloader]:JS/Nemucod
    【Python】HackBack(获取暴力破解服务器密码的IP来源)
    【Python】CVE-2017-10271批量自查POC(Weblogic RCE)
    【Python】exe2shellcode,shellcode2exe
    【Python】如何基于Python写一个TCP反向连接后门
    【工具】用命令行与Python使用YARA规则
    选择排序
  • 原文地址:https://www.cnblogs.com/gklovexixi/p/11016987.html
Copyright © 2011-2022 走看看