Swift - Programming Language

Introduction of playground:

Playground is tool (as I would like to say) provided by Apple's engineers for writing swift code and test the output on the go without and compilation and run that means it saves time. It helps you in following ways -

  1. Each step shows results instantly, like number of steps in a loop, stored value in a var / arrays etc..
  2. You don't have to explicitly write test suit to test your new functionality. That doesn't mean you don't write at all but you don't have to wait to test your functionality till you completely done with all test cases and complete functionality.
  3. You can even preview the resource you are working with say images.

How to launch playground:

  1. Launch Xcode
    (Current Version is Xcode_7.3.dmg as on Apr 30, 2016)
  2. Choose 'Get started with a playground' from launch window or File -> New Project.
    Or you can open using File Menu in Xcode.

  3. You are done and you will have a playground window opened with default code on your screen like image given below.

What next: Play with playground :)

Sample Code:

//simple string stored and printed on console
var str = "Hello, playground"
print ( str )

//mutable integar - this kind of data storage.
//allows value to be changed when needed
var a = 10 , b = 5
print ( a)
print( a+ b, a- b, a* b, a/ b, a% b)
a += b
b = 10
print( a+ b, b- a, a* b, a/ b, a% b)

//immutable integar - this kind of data storage
//allows value to be changed when needed
let c = 10, d = 15
print( c , d )
print( c+d, c-d, c*d, c/d, c%d)
c += d //Here code will not execute and raise error
d = 10 //We, will return on this in next tutorial
print( c+ d, c- d, c* d, c/ d, c% d)

//Below code snippet will show how to call function
//This function will print 1, 2, 3, 4
func print_intgers( n: Int){
for var i = 1; i <= n; i++ {
print (i, separator: "" , terminator: ", " )
//call the function print_integers

//Below code snippet will print a half xmas tree on console
func print_xmas_tree( n: Int){
for var i = 1; i <= n; i++ {
for var j = 0; j < i; j++ {
print( "*", separator: "", terminator: "")

//output on console


  1. Write code to print odd number upto 100
  2. Write code to print prime numbers below 1000
  3. Try to complete this half xmas tree
  4. Write code to print all permutation of letters a, b, c, d, e
  5. Example abcde, abced, abdce, abdec, ....
  6. Write code to print check if a given string is palindrome or not.
    Example: madam - palindrome, dear - not palindrome


  1. https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GuidedTour.html
  2. https://developer.apple.com/library/ios/recipes/Playground_Help/Chapters/AddPages.html