Swift is a powerful and efficient programming language that offers many great features, especially when compared to Objective-C. One such feature is the Swift Control Flow, and in this article, we’ll be focusing on the “Fallthrough” keyword in the switch statement.
Introduction
Swift is a modern programming language that offers many benefits over its predecessor, Objective-C. One of these benefits is the control flow of the switch statement. In Swift, a switch statement completes its execution as soon as the first matching case is completed, unlike Objective-C, where the execution would continue until a break statement is reached.
Switch Statement in Objective-C
In Objective-C, the switch statement is used to evaluate an expression and execute statements based on its value. The syntax for a switch statement in Objective-C is as follows:
switch(expression){
case constant-expression :
statement(s);
break; /* optional */
case constant-expression :
statement(s);
break; /* optional */
/* you can have any number of case statements */
default : /* Optional */
statement(s);
}
Here, the program terminates as soon as the first matching case is completed. If the programmer wants to continue to the next case, they must use a “break” statement.
Switch Statement in Swift
In Swift, the switch statement is similarly used to evaluate an expression and execute statements based on its value. However, the syntax for a switch statement in Swift is slightly different:
switch expression {
case expression1 :
statement(s)
fallthrough /* optional */
case expression2, expression3 :
statement(s)
fallthrough /* optional */
default : /* Optional */
statement(s);
}
In Swift, the program terminates as soon as the first matching case is completed, unless the “fallthrough” keyword is used. The “fallthrough” keyword allows the program to continue to the next case, similar to the “break” statement in Objective-C.
For example, consider the following code:
var number = 3
switch number {
case 1:
print("One")
fallthrough
case 2:
print("Two")
fallthrough
case 3:
print("Three")
fallthrough
case 4:
print("Four")
fallthrough
case 5:
print("Five")
fallthrough
default:
print("Done")
}
In this example, the case 3:
matches the value of number
and executes the corresponding statement. Because it has a “fallthrough” keyword, it continues to execute the next case until there are no more cases to execute, or a “break” statement is used. The output of the code would be:
Three
Four
Five
Done
It’s important to note that the “fallthrough” keyword does not check the conditions of the next case. It simply causes the program to move directly to the statements in the next case, similar to the standard switch statement behavior in C.
Conclusion
In this article, we’ve discussed the “Fallthrough” keyword in Swift’s control flow and compared it to the switch statement in Objective-C. We hope this article has provided a clear understanding of how the “Fallthrough” keyword works and how it can be used in your projects. For more information, please refer to the Swift documentation.
Final Thoughts
I believe you got enough details on the fallthrough statement what is the use of the fallthrough statement
Thank you very much for your precious time.
Hope you guys enjoy it.
Let me know your thoughts in the comment section.
Note: please have a look at my latest post the defer statement.
The Twitter contact:
Any questions? You can get in touch with me here
[…] an iOS Developer on wanna be an iOS Developer then maybe these articles will help defer statement, fallthrough statement, Access specifier in […]
[…] an iOS Developer on wanna be an iOS Developer then maybe these articles will help defer statement, fallthrough statement, tableviews in […]