Swift – Control Statements in Loops
Last Updated :
28 Feb, 2022
Control statements are used to control the flow of the loop. For example, we have written a for loop, inside the for loop we have written some 3 or 4 if conditions. Consider after satisfying 1st if condition the loop need to break, which means other of conditions should not be executed. In that case, we need to do something to break the loop. So in this situation control statements are used. Swift supports the following types of control statements:
- Break
- continue
- fallthrough
- Return statement
- Throw statement
Break statement
The break statement is used to break the loop in which it is present and controls transfer to the next statement(if available). Or we can say, in a loop when the break statement is encountered, then the loop breaks from the current iteration, and the next iterations will be executed as usual. We use break statements mostly in the switch statements to control the execution of the cases. If we don’t use a break after the statements in a particular case, other statements will also be executed.
Syntax:
break
Flow diagram:
Example:
Swift
var i = 1
repeat
{
if (i == 10)
{
break
}
print ( "Iteration number \(i)" )
i += 1
} while i < 20
|
Output:
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 statement
Continue statement is used to skip the current iteration and transfer the controls to the next iteration. Or we can say in a loop when a continue statement is encountered that particular iteration will be skipped and the remaining iterations are executed normally. Or it tells the loop to stop what it’s doing and start again from the beginning of the next iteration. Below are the flow chart and example which demonstrates the usage of the continue statement.
Syntax:
continue
Flow diagram:
Example:
Swift
var i = 10
repeat
{
i = i + 1
if (i == 15)
{
continue
}
print ( "Value of index is \(i)" )
} while i < 20
|
Output:
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
Note: Here we can see that only iteration 15 is skipped and the remaining iterations are executed normally.
Fallthrough statement
Fallthrough statements are used to execute the statements present after the match statement. Or we can say, a switch statement immediately stops its execution when the match is found. So fallthrough statement is used to display the statement present after the matched statement even if the value of the case does not match with the switch statement. It is generally used to achieve C-style fallthrough behavior. Or in a switch statement, when a fallthrough statement is encountered in any case then the flow automatically executed all the case statements below that particular case statement. Below is the syntax and example which demonstrates the usage of the fallthrough statement.
Syntax:
fallthrough
Example:
Swift
let a = 3
switch a
{
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" )
}
|
Output:
This is case 3
This is case 4
This is switch without fallthrough
Return statement
Generally return statement is used to return something from a function or method. It can return single or multiple values in a single statement. If a return statement is followed by an expression and the value of the expression does not match with the return type then before returning the value to the calling function, the type of the expression value is converted to the return type. And if the return statement does not followed by an expression, then it is only used to return from the function that does not return a value.
Syntax:
return value 1,value 2…value n
Example:
Swift
func sum(a: Int , b: Int )-> Int
{
return a + b
}
print ( "Sum of the given numbers is \(sum(a:10, b:20))" )
|
Output:
sum of the given numbers is 30
Throw statement
A throw statement is generally used in throwing functions or methods. It is also used in a closure expression whose type is marked with the help of the throw keyword. Or we can say that it is used to stop the execution of the current scope and start error propagation until the error is handled by the do-catch statement. Or the throw statement allows methods or functions to stop the execution and throw an error if there is any exception occurs. Let us see the syntax and example which demonstrates the usage of the throw statement.
Syntax:
func fun_name() throws -> <Return Type> {}
Example:
Swift
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)" )
}
|
Output :
This is example of throw statement...
Error: notaValidAge
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...