Control Flow In Swift

Last updated on Aug 18, 2016

Milan Sanathara

Swift

1

Control Flow in Swift

If you don’t know about the swift basic check the swift tour. Let’s get the idea of Control flow in swift. Use if and switch to create conditionals, and use for-in, for, while, and repeat-while to create loops. Parentheses around the condition or loop variable are optional. Braces around the body are needed.

Control Flow in Swift : Swift Conditionals Statements

In an if statement, the conditional should be a Boolean expression—this means code like if score {…} is an error, not an implicit comparison to zero.

You can use if and let along to work with values which may be missing. These values are described as optionals. an optional value either contains a value or contains nil to point that a value is missing. Write a question mark (?) after the type of a value to mark the value as optional.

If the optional value is nil, the conditional is false and therefore the code in braces is skipped. Otherwise, the optional value is unwrapped and assigned to the constant after let, which makes the unwrapped value available within the block of code.

Another way to handle optional values is to provide a default value using the ?? operator. If the optional value is missing, the default value is used instead.

Switches support any kind of data and a wide variety of comparison operations—they aren’t restricted to integers and tests for equality.

Notice however let is utilized in a pattern to assign the value that matched that a part of a pattern to a constant.

After executing the code within the switch case that matched, the program exits from the switch statement. Execution doesn’t continue to the following case, thus there’s no need to explicitly break out of the switch at the end of every case’s code.

Control Flow in Swift : Loops In Swift

You use for-in to iterate over items in a dictionary by providing a pair of names to use for each key-value pair. Dictionaries area unit an unordered collection, thus their keys and values area unit iterated over in an absolute order.

Use while to repeat a block of code until a condition changes. The condition of a loop can be at the end instead, ensuring that the loop is run at least once.

You can keep an index in a loop—either by using ..<  to make a range of indexes or by writing an explicit initialization, condition, and increment. These two loops do the same thing:

Use ..< to make a range that omits its upper value, and uses  to make a range that includes both values.

I hope you will find this post very useful regarding control flow in swift. Let me know if you have any question regarding swift in a comment. I will reply to you ASAP.

Have you got a cool idea about iPhone App Development? Contact us Now to get a free consultation on your idea. Alphanso Tech is rated as one of the best iPhone App Development Company in India.

Post by Milan Sanathara

I am iOS Application Developer with an aspiration of learning new technology and creating a bright future in Information Technology.

One Response to Control Flow In Swift

  1. Pingback: Awesome Swift Learning Resources | Gaurav's Blog

Leave a Reply

Your email address will not be published. Required fields are marked *