条件语句 (if, switch)

概述

条件语句是编程中用于根据不同条件执行不同代码块的工具。在 Swift 中,我们主要使用 if 和 switch 语句来实现条件判断。if 语句用于简单的条件判断,而 switch 语句则适用于更复杂的条件匹配。

if 语句

基本语法

if 语句的基本语法如下:

if condition {
    // 当 condition 为 true 时执行的代码
}

示例

let temperature = 25

if temperature > 30 {
    print("It's hot outside!")
} else if temperature > 20 {
    print("It's warm outside.")
} else {
    print("It's cold outside.")
}

解释

  • if 语句首先检查 condition 是否为 true。如果为 true,则执行 if 块中的代码。
  • 如果 condition 为 false,则检查 else if 语句中的条件。如果有多个 else if 语句,它们会依次被检查。
  • 如果所有条件都不满足,则执行 else 块中的代码。

switch 语句

基本语法

switch 语句的基本语法如下:

switch value {
case pattern1:
    // 当 value 匹配 pattern1 时执行的代码
case pattern2:
    // 当 value 匹配 pattern2 时执行的代码
default:
    // 当 value 不匹配任何模式时执行的代码
}

示例

let day = "Monday"

switch day {
case "Monday":
    print("Start of the week.")
case "Friday":
    print("End of the week.")
default:
    print("Midweek day.")
}

解释

  • switch 语句根据 value 的值来匹配不同的 case。
  • 每个 case 后面可以跟一个或多个模式。
  • 如果 value 匹配某个 case,则执行该 case 块中的代码。
  • default 块是可选的,用于处理所有未匹配的情况。

实践练习

练习 1: 温度提示

编写一个程序,根据用户输入的温度提示不同的天气情况。

let temperature = 15

if temperature > 30 {
    print("It's very hot!")
} else if temperature > 20 {
    print("It's warm.")
} else if temperature > 10 {
    print("It's cool.")
} else {
    print("It's cold.")
}

练习 2: 星期几

编写一个程序,根据用户输入的星期几输出不同的信息。

let day = "Wednesday"

switch day {
case "Monday", "Tuesday", "Wednesday", "Thursday", "Friday":
    print("Weekday.")
case "Saturday", "Sunday":
    print("Weekend.")
default:
    print("Invalid day.")
}

总结

条件语句是编程中非常重要的工具,能够帮助我们根据不同的条件执行不同的代码。if 语句适用于简单的条件判断,而 switch 语句则适用于更复杂的条件匹配。通过练习,你可以更好地掌握这些语句的使用。

下一步

接下来,我们将学习循环语句 (for-in, while, repeat-while),它们可以帮助我们重复执行代码块。