Skip to content

Instantly share code, notes, and snippets.

@PeteGabriel
Created July 7, 2022 07:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PeteGabriel/8922b8806d1ce6aaf1ddf4d3d1ae0d07 to your computer and use it in GitHub Desktop.
Save PeteGabriel/8922b8806d1ce6aaf1ddf4d3d1ae0d07 to your computer and use it in GitHub Desktop.
import Cocoa
//contants do not vary
let a = "Hello, playground"
//a = "Hello, world" // error: reassignment to constant 'a'
//variables can vary
var anotherA = "Hello, Pedro"
anotherA += "!"
print(anotherA) // Hello, Pedro!
var numberOfStoplights = 3
numberOfStoplights += 1 // numberOfStoplights = 4
//String interpolation
var population: Int
population = 9_000_000
let townName = "San Francisco"
let elevation = 12
var townDescription = "\(townName) has a population of \(population) with \(numberOfStoplights) stoplights."
townDescription += " It is located at an elevation of \(elevation) feet."
print(townDescription) // San Francisco has a population of 9000000 with 4 stoplights. It is located at an elevation of 12 feet.
//Conditional statements
//big or small
let population2: Int = 42200
var message : String
let hasPostOffice: Bool = true
if population2 < 10000 {
message = "Small"
} else {
message = "Big"
}
/*
This could be written using the ternary operator.
*/
message = population2 < 10000 ? "Small" : "Big"
if population2 < 10000 {
message = "\(townName) is a Small town!"
} else if population2 >= 10000 && population2 < 50000 {
message = "\(townName) is a Medium town!"
} else {
message = "\(townName) is a Big town!"
}
print(message) // Small
if !hasPostOffice {
print("Where do we buy stamps?¿") //never prints
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment