Ticker

6/recent/ticker-posts

Basics of Swift programming - Part-2

In this tutorial we are going to see about condition statements, defining enum values, structures and loops.

1) Defining Enums
An enumeration is a user-defined data type which consists of set of related values. Keyword enum is used to defined enumerated data type.

enum DaysOfAWeek {
    case Sunday
    case Monday
    case Tuesday
    case Wednsday
    case Thursday
    case Friday
    case Saturday
}


For more

2) Condition or Decision Making statements
a) if, if else, if else if
The statements inside of the if will be executed when the condition is true

Ex1:
let isDone: Bool = false
if isDone{
    print("isDone is true")
}

Ex2:
let yes : Bool = true
if yes{
    print("entered into if")
}else{
    print("entered into else")
}

Ex3:
let name = "Jayachandra"
if name == "Jay"{
    print("name is Jay")
}else if name == "Raju"{
    print("name is Raju")
}else if name == "Jayachandra"{
    print("name is Jayachandra")
}

b) switch
You already seen declaring of enum above, now let's see how it is used. Consider the following code which returns boolean flag according to the argument you passed to the function

    func isWeekDay(day:DaysOfAWeek) -> Bool {
        switch day {
        case .Monday:
            return true
        case .Tuesday:
            return true
        case .Wednsday:
            return true
        case .Thursday:
            return true
        case .Friday:
            return true
        case .Saturday:
            return false
        case .Sunday:
            return false
        }

    }




3) Structures :
A struct variable is defined using the struct keyword. Following is an example of a variable holding employee data:


//declaring
struct Employee {
    let name : String
    let id : Int
    let loc : String
}

//test method to implement structure
func test() -> Void {
    let em = Employee.init(name: "Jayachandra", id: 89, loc: "Bangalore")
    print(em.name)
}

//calling test function
test()

4) loops
a) for
let count = 10
var sum = 0
for var i = 1; i <= count; i += 1 {
    sum += i
}
print(sum)

The loop gets iterates for 10 time, you can see the execution process graphically in xcode playgroud by clicking (+) at the right side of editor.
b) for - each
syntax:
for <variable> in <list of objects>{
}

Ex:
let fruits = ["Apple","Mango","Pine Apple","Banana","Blueberry","Grapes","Jackfruit","Custard Apple","Guava"]
for fruit in fruits{
    print(fruit)
}


c) while
 syntax : 
while <condition>{
<Code>
}

The code will be executed when the condition is satisfied.
Ex:
var count = 10 
while count>=10
   print(count) count -= 1

}


c) repeat while
Another variant of the while loop is called the repeat-while loop. It differs from the while loop in that the condition is evaluated at the end of the loop rather than at the beginning.

syntax
repeat {
    <LOOP CODE>
} while <CONDITION>

Ex:
var sum = 1
repeat {
    sum = sum + (sum + 1)
} while sum < 1000

Post a Comment

0 Comments