Property in Swift - Computed property | Property Observers (WillSet & DidSet)

   Computed property -  Computed properties are part of a property type in Swift.  Stored properties are the most common which save and return a stored value  Computed properties calculate (rather than store) a value. A computed property provides a getter and an optional setter to indirectly access other properties and values. Computed property in swift is an interesting topic as you can write code inside a property in swift. Computed properties are provided by classes, structures, and enumerations but stored properties are provided only by classes and structures. This computed property swift tutorial has some tips on how to use the computed property in the right way and avoid the common mistakes that swift programming beginners do while computed property. Example :- Computed property A Calculate Simple Intrest struct CalculateLoan {      var amount : Int      var rate : Int      var years : Int      var simpleInterest: Int {          get {              return ( amount * rate

Control Flow Statements - statements used in swift


List out what is the control transfer statements used in swift

Control flow statements are used to control the flow of execution in a program. There are several types of control flow statements in Swift, including loop statements, branch statements, and control transfer statements. 
1.       Loop statements allow a block of code to be executed repeatedly
2.        Branch statements allow a certain block of code to be executed only when certain conditions are met
3.       Control transfer statements provide a way to alter the order in which code is executed.
4.       Swift provides a do statement to introduce scope, and catch and handle errors,
5.       Defer statement for running cleanup actions just before the current scope exits.



Loop Statements -

Loop statements allow a block of code to be executed repeatedly, depending on the conditions specified in the loop.
Swift there are three loop statements
·         For-in statement
·         While statement
·         Repeat-while statement
A for-in statement allows a block of code to be executed once for each item in a collection that conforms to the Sequence protocol.
for item in collection {
    statements
}
A while statement allows a block of code to be executed repeatedly, a condition remains true.
while condition {
    statements
}
A repeat-while statement allows a block of code to be executed one or more times, then condition true.
repeat {
    statements
} while

Branch Statements

Branch statements allow a certain block of code to be executed only when certain conditions are met. Swift have three branch statements
·         if statement
·         guard statement
·         Switch statement
Guard Statement
A guard statement is used to transfer program control out of a scope if one or more conditions aren’t met.
guard condition else {
    statements
}
Switch Statement
A switch statement allows certain blocks of code to be executed depending on the value of a control expression.
switch control expression {
case pattern 1:
    statements
case pattern 2 where condition:
    statements
case pattern 3 where condition,
     pattern 4 where condition:
    statements
default:
    statements
}

Control Transfer Statements

Control transfer statements can change (alter) the order in which code in your code is executed.
 Swift have five control transfer statements
·         break statement,
·         continue statement - A continue statement ends program execution of the current iteration of a loop statement 
·         fallthrough statement
·         return statement
·         throw statement

Comments

  1. Thanks for the useful information, give more updates like First time I visit your site really nice, here after a daily visit.
    ecommerce website development company in chennai

    ReplyDelete

Post a Comment