What is the difference between continuation, interruption, failure, throw and return to SWIFT?

What is the difference between continuation, interruption, failure, throw and return to SWIFT?

What is the difference between continuation, interruption, failure, throw and return to SWIFT?

+4
source share
1 answer

Continue

The continue statement completes the current iteration of the loop statement, but does not stop the execution of the loop statement.

var sum = 0;
for var i = 0 ; i < 5 ; i++ {
    if i == 4 {
        continue //this ends this iteration of the loop
    }
    sum += i //thus, 0, 1, 2, and 3 will be added to this, but not 4
}  

Break:

The break statement terminates the execution of a loop program, an if statement, or a switch statement.

var sum = 0;
for var i = 0 ; i < 5 ; i++ {
    if i == 2 {
        break //this ends the entire if-statement
    }
    sum += i //thus only 0 and 1 will be added
}

fails:

The final statement causes the program to continue from one case to the switch statement in the next case.

var sum = 0
var i = 3
switch i {
case 1:
    sum += i
case 2:
    sum += i
case 3:
    sum += i
    fallthrough //allows for the next case to be evaluated
case i % 3:
    sum += i
}

throw:

You use the throw statement to throw an error.

, , , - .

throw VendingMachineError.InsufficientFunds(coinsNeeded: 5)

:

return .

func myMethod(){
    newMethod()
}

func newMethod(){
    var sum = 0;
    sum += 2;
    if sum > 1 {
        return //jumps back to myMethod()
    }
    sum += 3; //this statement will never be executed
}

.

func myFunc() {
    let value = newFunc() //assigns 5 to "value"
}

func newFunc() -> Int {
    return 5 
}
+16

Source: https://habr.com/ru/post/1628780/


All Articles