Swift : Optionals

What is Optional in Swift ? In Swift, variable values can be optional. That means that a variable will either be nil or it will have a valid value. We can declare a variable as Optional if there is a possibility that the variable may not have a valid value.   Declaring Optional variables and […]

Swift while and repeat-while loop

Swift while loop The while loop evaluates a conditional statement and then repeatedly executes a block of code if the conditional statement is true.   Syntax : while condition {      block of code to be executed }   Example of while loop in Swift In this program, variable i starts at 0 and is […]

Swift for loop

Swift for loop allows some code to be executed again and again.   Syntax of for loop for initialization;conditional expression;update counter {   // Statements to be executed }   The loop executes as follows : Step 1: When the loop is first entered, the initialization statement is evaluated. Step 2: The conditional is evaluated; if […]

Swift for-in loop

for-in loop   The for-in loop iterates over a collection of items or a range of numbers and executes a block of code for each item in the collection or range.   Syntax of for-in in Swift   The syntax of for-in statement looks like: for variable in Collection/Range {      block of code } […]

Swift Operators

Operators in Swift can be:  Unary, which requires only one operand or input.(e.g. -a) Binary, which requires two operands or inputs. (e.g. a+b) Ternary, which requires three operands. (e.g. ?:).   Swift Assignment Operator The assignment operator initializes or updates a variable. var name = "John Doe" name = "Dave Mathias"   Swift Comparison […]