![]() |
VOOZH | about |
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:
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:
👁 ImageExample:
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 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:
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 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:
Output:
This is case 3 This is case 4 This is switch without fallthrough
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:
Output:
sum of the given numbers is 30
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:
Output :
This is example of throw statement... Error: notaValidAge