Monday 24 October 2016

Conditional Statements In Swift3.0



Swift provides two ways to add conditional branches to your code: the if statement and the switch statement. Typically, you use the if statement to evaluate simple conditions with only a few possible outcomes. The switch statement is better suited to more complex conditions with multiple possible permutations and is useful in situations where pattern matching can help select an appropriate code branch to execute.

1) If:  It executes a set of statements only if that condition is true.














The if statement can provide an alternative set of statements, known as an else clause, for situations when the if condition is false. These statements are indicated by the else keyword.











You can chain multiple if statements together to consider additional clauses.














Switch: A switch statement considers a value and compares it against several possible matching patterns. It then executes an appropriate block of code, based on the first pattern that matches successfully. A switch statement provides an alternative to the if statement for responding to multiple potential states.
  • switch value {
  • 
    
  • case value 1:
  • 
    
  •     Statements
  • case value 2,  
  •        value 3:
  •       Statement for case 2 and 3
  • default:
  •     otherwise, Execute Default Case
  • }















Interval Matching : Above example solved by interval matching.Each case compares that value to a number or interval. Because the value of approximateCount falls between 18 and 40.So its output is "Easy to find soul mate for you".


Compound Switch Case: To make a switch with a single case that matches both "z" and "Z", combine the two values into a compound case, separating the values with commas.

















Tuples With Switch Case :
You can use tuples to test multiple values in the same switch statement. Each element of the tuple can be tested against a different value or interval of values. Alternatively, use the underscore character (_), also known as the wildcard pattern, to match any possible value.
























Where In Switch Case:
The where statement may be used within a switch case match to add additional criteria required for a positive match. The following switch statement, for example, checks not only for the range in which a value falls, but also whether the number is odd or even:


















Control Transfer Statements:
Control transfer statements change the order in which your code is executed, by transferring control from one piece of code to another. Swift has five control transfer statements:
  • continue
  • break
  • fallthrough
  • return
  • throw

continueThe continue statement skips some statements inside the loop.



break
The break statement ends execution of an entire control flow statement immediately. The break statement can be used inside a switch statement or loop statement when you want to terminate the execution of the switch or loop statement earlier than would otherwise be the case.



fallthrough :
Switch statements in Swift don’t fall through the bottom of each case and into the next one. Instead, the entire switch statement completes its execution as soon as the first matching case is completed. By contrast, C requires you to insert an explicit break statement at the end of every switch case to prevent fallthrough. Avoiding default fallthrough means that Swift switch statements are much more concise and predictable than their counterparts in C, and thus they avoid executing multiple switch cases by mistake.


















Note*
The fallthrough keyword does not check the case conditions for the switch case that it causes execution to fall into. The fallthrough keyword simply causes code execution to move directly to the statements inside the next case (or default case) block, as in C’s standard switch statement behavior.

return:
The return instruction is used either to return a function value or to terminate the execution of a function.






Here we return language name from a function named "studyWhichLanguage".


throw:
 throw is used in error handling.concept of throw is covered in my upcoming  error handling blog.







No comments:

Post a Comment