For and While loops in Swift 2

For and While loops allow a piece of code to be executed iteratively and repeatedly. I’ve played around with loops before in Python so already have an idea how they function. Transferring the skills from Python to Swift 2 seems relatively easy. Playground allows the code to be run as you are editing and highlights errors quickly, which allows for easy amendments when you mess up.

While loops

Some basic While loops

I played around with While loops implementing and printing information from an array and amending the data – lots of trial and error was involved getting the code to work as hoped, but slowly I’ve been getting there. I also explored some simple For loops, creating code that generates sequences of numbers and manipulates arrays with simple math. The two images I’ve uploaded show screen shots of the code running in Playground and the resulting outputs.

FOR loops in Swift 2

Some simple For loops in Swift 2 (Xcode Playground)

My next challenge is to create a simple app with UI which tells a user whether the number they input is a prime number or not. This time I’m aiming to create the code first in Playground and then implement it into the app. Considering I only starting learning app development a few days ago I feel like I am making good progress – but I’ve got a LOT to learn!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s