📜  Swift——循环中的控制语句

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

Swift——循环中的控制语句

控制语句用于控制循环的流程。例如,我们编写了一个 for 循环,在 for 循环内我们编写了一些 3 或 4 个 if 条件。在满足第一个 if 条件后考虑循环需要中断,这意味着不应执行其他条件。在这种情况下,我们需要做一些事情来打破循环。所以在这种情况下使用控制语句。 Swift 支持以下类型的控制语句:

  1. 休息
  2. 继续
  3. 失败
  4. 退货声明
  5. 抛出语句

中断声明

break 语句用于中断它所在的循环并控制转移到下一个语句(如果可用)。或者我们可以说,在循环中遇到 break 语句时,循环从当前迭代中中断,下一次迭代将照常执行。我们主要在 switch 语句中使用 break 语句来控制 case 的执行。如果我们在特定情况下的语句后不使用中断,其他语句也会被执行。

句法:

流程图:

例子:

Swift
// Swift program to illustrate break statement
 
// Declaring a mutable variable
var i = 1
 
// Initializing repeat while loop
repeat
{
 
    // Checking condition
    if (i == 10)
    {
        // If the i==10 then break the loop
        // Using break statement
        break
    }
     
    // Printing the i value
    print("Iteration number \(i)")
     
    // Incrementing i value
    i += 1
}while i < 20 // Condition


Swift
// Swift program to illustrate continue statement
 
// Declaring variable
var i = 10
 
// Initializing repeat while loop
repeat
{
    // Incrementing iteration
   i = i + 1
   if (i == 15)
   {
    
        // Checking condition using if
        continue
   }
   print("Value of index is \(i)")
} while i < 20


Swift
// Swift program to illustrate fallthrough statement
 
// Declaring choice
let a = 3
 
switch a
{
    // Initializing switch statement
    // with fallthrough statement
    case 1:
        print("This is case 1")
        fallthrough
    case 2:
        print("This is case 2")
        fallthrough
    case 3:
        print("This is case 3")
        fallthrough
    case 4:
        print("This is case 4")
        fallthrough
    default:
        print("This is switch without fallthrough")
}


Swift
// Swift program to illustrate the return statement
 
// Creating a function which can
// add two integer numbers
func sum(a:Int, b:Int)->Int
{
 
    // Returning the result
    return a + b
}
 
// Calling the function
print("Sum of the given numbers is \(sum(a:10, b:20))")


Swift
// Swift program to illustrate the throw statement
enum errorexp: Error{
    case notaValidName
    case notaValidAge
}
 
func emp(age: Int, Name: String) throws{
     
    guard age > 0 else{
        throw errorexp.notaValidAge
    }
     
    guard Name.count > 0 else{
        throw errorexp.notaValidName
    }
}
 
print("This is example of throw statement...")
do{
    try emp(age: -1, Name:"")
} catch let error {
    print("Error: \(error)")
}



输出:

Iteration number 1
Iteration number 2
Iteration number 3
Iteration number 4
Iteration number 5
Iteration number 6
Iteration number 7
Iteration number 8
Iteration number 9

继续声明

Continue 语句用于跳过当前迭代并将控制转移到下一个迭代。或者我们可以在循环中说,当遇到 continue 语句时,将跳过特定的迭代并正常执行剩余的迭代。或者它告诉循环停止它正在做的事情并从下一次迭代的开始重新开始。以下是演示 continue 语句用法的流程图和示例。

句法:

流程图:

例子:

迅速

// Swift program to illustrate continue statement
 
// Declaring variable
var i = 10
 
// Initializing repeat while loop
repeat
{
    // Incrementing iteration
   i = i + 1
   if (i == 15)
   {
    
        // Checking condition using if
        continue
   }
   print("Value of index is \(i)")
} while i < 20


输出:

Iteration number 11
Iteration number 12
Iteration number 13
Iteration number 14
Iteration number 16
Iteration number 17
Iteration number 18
Iteration number 19
Iteration number 20

注意:这里我们可以看到只跳过了第 15 次迭代,其余的迭代都正常执行。

失败声明

Fallthrough 语句用于执行 match 语句之后的语句。或者我们可以说,当找到匹配项时,switch 语句会立即停止执行。所以 fallthrough 语句用于显示匹配语句之后出现的语句,即使 case 的值与 switch 语句不匹配。它通常用于实现 C 风格的失败行为。或者在 switch 语句中,当在任何情况下遇到 fallthrough 语句时,流程会自动执行该特定 case 语句下方的所有 case 语句。下面是演示 fallthrough 语句用法的语法和示例。

句法:

例子:

迅速

// Swift program to illustrate fallthrough statement
 
// Declaring choice
let a = 3
 
switch a
{
    // Initializing switch statement
    // with fallthrough statement
    case 1:
        print("This is case 1")
        fallthrough
    case 2:
        print("This is case 2")
        fallthrough
    case 3:
        print("This is case 3")
        fallthrough
    case 4:
        print("This is case 4")
        fallthrough
    default:
        print("This is switch without fallthrough")
}


输出:

This is case 3
This is case 4
This is switch without fallthrough

退货声明

通常 return 语句用于从函数或方法返回某些内容。它可以在单个语句中返回单个或多个值。如果 return 语句后跟一个表达式,并且表达式的值与返回类型不匹配,则在将值返回给调用函数之前,表达式值的类型将转换为返回类型。而如果return语句后面没有跟表达式,那么它只用于从不返回值的函数中返回。

句法:

例子:

迅速

// Swift program to illustrate the return statement
 
// Creating a function which can
// add two integer numbers
func sum(a:Int, b:Int)->Int
{
 
    // Returning the result
    return a + b
}
 
// Calling the function
print("Sum of the given numbers is \(sum(a:10, b:20))")


输出:

sum of the given numbers is 30

抛出语句

throw 语句通常用于抛出函数或方法。它也用于闭包表达式,其类型通过 throw 关键字进行标记。或者我们可以说它用于停止当前作用域的执行并开始错误传播,直到错误被 do-catch 语句处理。或者 throw 语句允许方法或函数在发生任何异常时停止执行并抛出错误。让我们看看演示 throw 语句用法的语法和示例。

句法:

例子:

迅速

// Swift program to illustrate the throw statement
enum errorexp: Error{
    case notaValidName
    case notaValidAge
}
 
func emp(age: Int, Name: String) throws{
     
    guard age > 0 else{
        throw errorexp.notaValidAge
    }
     
    guard Name.count > 0 else{
        throw errorexp.notaValidName
    }
}
 
print("This is example of throw statement...")
do{
    try emp(age: -1, Name:"")
} catch let error {
    print("Error: \(error)")
}


输出 :

This is example of throw statement...
Error: notaValidAge