Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between continue, break, fall-through, throw and return in SWIFT? [closed]

Tags:

swift

Could someone help explain to me the differences between these keywords in Swift?

  • continue
  • break
  • fallthrough
  • throw
  • return
like image 363
Honestraj19 Avatar asked Sep 03 '25 06:09

Honestraj19


1 Answers

Continue:

A continue statement ends program execution of the current iteration of a loop statement but does not stop execution of the loop statement. (source)

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:

A break statement ends program execution of a loop, an if statement, or a switch statement. (source)

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

Fallthrough:

A fallthrough statement causes program execution to continue from one case in a switch statement to the next case. (source)

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 a throw statement to throw an error. (source)

You could throw an error like the one below that says that five more coins are needed due to insufficient funds.

throw VendingMachineError.InsufficientFunds(coinsNeeded: 5)

Return:

A return statement occurs in the body of a function or method definition and causes program execution to return to the calling function or method. (source)

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
}

Returns can also be used to return a value from the function.

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

func newFunc() -> Int {
    return 5 
}
like image 192
Benjamin Lowry Avatar answered Sep 04 '25 23:09

Benjamin Lowry