• Swift 条件语句


    条件语句通过设定的一个或多个条件来执行程序,在条件为真时执行指定的语句,在条件为 false 时执行另外指定的语句。

    可以通过下图来简单了解条件语句的执行过程:

    Swift 提供了以下几种类型的条件语句:

    语句描述

    if 语句

    if 语句 由一个布尔表达式和一个或多个执行语句组成。

    if...else 语句

    if 语句 后可以有可选的 else 语句else 语句在布尔表达式为 false 时执行。

    if...else if...else 语句

    if 后可以有可选的 else if...else 语句, else if...else 语句常用于多个条件判断。

    内嵌 if 语句

    你可以在 if 或 else if 中内嵌 if 或 else if 语句。

    switch 语句

    switch 语句允许测试一个变量等于多个值时的情况。

    ?: 运算符

    我们已经在前面的章节中讲解了 条件运算符 ? :,可以用来替代 if...else 语句。它的一般形式如下:

    Exp1 ? Exp2 : Exp3;

    其中,Exp1、Exp2 和 Exp3 是表达式。请注意,冒号的使用和位置。

    ? 表达式的值是由 Exp1 决定的。如果 Exp1 为真,则计算 Exp2 的值,结果即为整个 ? 表达式的值。如果 Exp1 为假,则计算 Exp3 的值,结果即为整个 ? 表达式的值。


    Swift 几种类型的条件语句语法


    if boolean_expression {
       /* 如果布尔表达式为真将执行的语句 */
    }
    
    if boolean_expression {
       /* 如果布尔表达式为真将执行的语句 */
    } else {
       /* 如果布尔表达式为假将执行的语句 */
    }
    if boolean_expression_1 {
       /* 如果 boolean_expression_1 表达式为 true 则执行该语句 */
    } else if boolean_expression_2 {
       /* 如果 boolean_expression_2 表达式为 true 则执行该语句 */
    } else if boolean_expression_3 {
       /* 如果 boolean_expression_3 表达式为 true 则执行该语句 */
    } else {
       /* 如果以上所有条件表达式都不为 true 则执行该语句 */
    }
    if boolean_expression_1 {
       /* 当 boolean_expression_1 表达式 true 时执行 */
       if boolean_expression_2 {
          /* 当 boolean_expression_1 表达式 true 时执行 */
       }
    }
    switch expression {
       case expression1  :
          statement(s)
          fallthrough /* 可选 */
       case expression2, expression3  :
          statement(s)
          fallthrough /* 可选 */
      
       default : /* 可选 */
          statement(s);
    }

  • 相关阅读:
    maven的pom.xml文件详细说明
    python 给视频添加马赛克
    cv2.VideoCapture 图像旋转问题
    三分钟理解知识蒸馏
    深度学习、机器学习常见概念及理解(持续更新)
    python用直方图规定化实现图像风格转换
    1分钟理解人体姿态估计与行为识别
    数据清洗要点
    3分钟理解NMS非极大值抑制
    python用pandas遍历csv文件
  • 原文地址:https://www.cnblogs.com/loaderman/p/10154812.html
Copyright © 2020-2023  润新知