📜  Swift – Fallthrough 声明

📅  最后修改于: 2022-05-13 01:54:28.938000             🧑  作者: Mango

Swift – Fallthrough 声明

就像 Swift 中的其他语言一样,switch 语句也非常有用和强大。它接受一个值,然后将其与几个可能的匹配模式进行比较,或者我们可以说 case 并在它成功找到第一个匹配的 case 语句时停止执行,然后执行匹配语句中存在的代码。它通过避免重复使用 if-else 语句来优化代码。在 Swift 中,switch 语句不会落在每个 case 语句的底部。相反,switch 语句在遇到第一个匹配的 case 而没有任何显式的 break 语句时完成其执行。它使 switch 语句更安全、更易于使用,并避免错误地执行多个 case 语句。

如果你希望你的 switch 语句通过或者你想要 C 风格的 fallthrough 功能,那么你可以使用 fallthrough 语句。该语句用于强制执行匹配语句之后出现的下一个 case,即使该 case 不匹配指定条件。请记住,它只执行一个出现在匹配语句之后的 case 语句,而不是所有 case 语句,除非在所有 case 语句之后使用 fallthrough 语句。

句法:

示例 1:

在这个例子中,我们将首先看到普通的 switch 语句是如何工作的。

Swift
// Swift program to illustrate the
// the switch statement
import Foundation
import Glibc
 
// Creating and initializing variable
var choice = 2
 
// Switch statement
switch(choice) {
case 1:
print("Hi! its Monday")
case 2:
print("Hi! its Tuesday")
case 3:
print("Hi! its Wednesday")
case 4:
print("Hi! its Thursday")
case 5:
print("Hi! its Friday")
case 6:
print("Hi! its Saturday")
case 7:
print("Hi! its Sunday")
default:
print("Choice not found")
}


Swift
// Swift program to illustrate the
// working of fallthrough statement
import Foundation
import Glibc
 
// Creating and initializing variable
var choice = 2
 
// Switch statement
switch(choice) {
case 1:
print("Hi! its Monday")
case 2:
print("Hi! its Tuesday")
fallthrough
case 3:
print("Hi! its Wednesday")
case 4:
print("Hi! its Thursday")
case 5:
print("Hi! its Friday")
case 6:
print("Hi! its Saturday")
case 7:
print("Hi! its Sunday")
default:
print("Choice not found")
}


Swift
// Swift program to illustrate the
// working of fallthrough statement
import Foundation
import Glibc
 
// Creating and initializing variable
var choice = "Three"
 
// Switch statement
switch(choice) {
case "One":
print("Your today day is going to be good")
fallthrough
case "Two":
print("Your today day is going to be bad")
fallthrough
case "Three":
print("Your today day is going to be very bad")
fallthrough
case "Four":
print("Your today day is going to be ok")
fallthrough
case "Five":
print("Your today day is going to be full of up and downs")
fallthrough
default:
print("Choice not found")
}


输出:

Hi! its Tuesday

在这里,我们在 switch 语句中输入条件“choice = 2”。现在 switch 语句将条件与每个 switch 语句逐一比较,并在第二种情况下找到匹配项,因此它显示“嗨!它的星期二”并停止执行。

现在我们使用相同的代码来查看 fallthrough 语句是如何工作的。

迅速

// Swift program to illustrate the
// working of fallthrough statement
import Foundation
import Glibc
 
// Creating and initializing variable
var choice = 2
 
// Switch statement
switch(choice) {
case 1:
print("Hi! its Monday")
case 2:
print("Hi! its Tuesday")
fallthrough
case 3:
print("Hi! its Wednesday")
case 4:
print("Hi! its Thursday")
case 5:
print("Hi! its Friday")
case 6:
print("Hi! its Saturday")
case 7:
print("Hi! its Sunday")
default:
print("Choice not found")
}

输出:

Hi! its Tuesday
Hi! its Wednesday

在这里,switch 语句(即选择 = 2)在案例 2 上找到了它的匹配项,即“嗨!它是星期二”,但它也显示“嗨!在“嗨!它的星期二”,因为在 case 2 的语句之后使用了 fallthrough 语句。由于 fallthrough 语句,case 3 被强制执行。

示例 2:

迅速

// Swift program to illustrate the
// working of fallthrough statement
import Foundation
import Glibc
 
// Creating and initializing variable
var choice = "Three"
 
// Switch statement
switch(choice) {
case "One":
print("Your today day is going to be good")
fallthrough
case "Two":
print("Your today day is going to be bad")
fallthrough
case "Three":
print("Your today day is going to be very bad")
fallthrough
case "Four":
print("Your today day is going to be ok")
fallthrough
case "Five":
print("Your today day is going to be full of up and downs")
fallthrough
default:
print("Choice not found")
}

输出:

Your today day is going to be very bad
Your today day is going to be ok
Your today day is going to be full of up and downs
Choice not found

说明:在这个例子中,我们在 switch 语句中传递了“choice = “Three”条件,并在每个 case 语句之后放置了 fallthrough 语句。因此,当 switch case 找到匹配的 case 即 case ” Three” 时, case “Three” 语句和 case “Three” 之后出现的 case 将由于 fallthrough 语句而执行。