Skip to content

Instantly share code, notes, and snippets.

@mildocjr
mildocjr / ifelse1.swift
Created August 10, 2018 02:50
if-else-1
var hasCoffee = false
var madeCup: Bool
if hasCoffee {
madeCup = true
} else {
madeCup = false
}
@mildocjr
mildocjr / elseif1.swift
Created August 10, 2018 02:51
else-if-1
var age: Int = 16
var ageDescription: String
if age < 13 {
ageDescription = "You are young."
} else if age <= 18 {
ageDescription = "You are a teenager."
} else {
ageDescription = "You are an adult."
}
// ageDescription is equal to "You are a teenager."
@mildocjr
mildocjr / nestedifelse1.swift
Created August 10, 2018 02:51
nested-if-else-1
var age: Int = 25
var ageDescription: String
if age < 13 {
ageDescription = "You are young."
else {
if age <= 18 {
ageDescription = "You are a teenager."
else {
ageDescription = "You are an adult."
}
@mildocjr
mildocjr / guessage1.swift
Created August 10, 2018 02:52
guess-age-1
var likesCoffeeWithSugar = true
var likesCoffeeWithMilk = false
var likesCoffeeBlack = !likesCoffeeWithMilk
var hasCoffee = true
var hasMilk = true
var hasSugar = false
var madeCup: Bool
if (likesCoffeeWithMilk && hasMilk) &&
(likesCoffeeWithSugar && hasSugar) && hasCoffee {
@mildocjr
mildocjr / ternaryoperators3.swift
Created August 10, 2018 02:55
ternary-operators-3
var isSunShining = true
var description: String = "" // This is an empty string
if isSunShining {
description = "Yay!"
else {
description = "Aww..."
}
// Same if statement using a ternary operator
description = isSunShining ? "Yay!" : "Aww..."
@mildocjr
mildocjr / ifimprovements1.swift
Last active August 10, 2018 02:56
if-improvements-1
var likesCoffeeWithSugar = true
var likesCoffeeWithMilk = false
var likesCoffeeBlack = !likesCoffeeWithMilk
var hasCoffee = true
var hasMilk = true
var hasSugar = false
var madeCup = (likesCoffeeWithMilk && hasMilk) &&
(likesCoffeeWithSugar && hasSugar) && hasCoffee ? true :
(likesCoffeeWithMilk && hasMilk) ||
@mildocjr
mildocjr / whileloops1.swift
Last active August 10, 2018 02:57
while-loops-1
// The while loop
var countA = 0
while countA < 10 {
countA += 1
}
// The repeat-while loop
var countB = 0
repeat {
countB += 1
@mildocjr
mildocjr / whileloops2.swift
Created August 10, 2018 03:00
while-loops-2
var madeCupCount = 0
var shouldMakeCoffee = false
repeat {
madeCupCount += 1
} while shouldMakeCoffee
// madeCupCount = 1
while shouldMakeCoffee {
@mildocjr
mildocjr / whilegame1.swift
Created August 10, 2018 03:00
while-game-1
let winningScore = 100
var playerOneScore = 0
var playerTwoScore = 0
repeat {
playerOneScore += 1
playerTwoScore += 2
} while (playerOneScore < winningScore) ||
(playerTwoScore < winningScore)
@mildocjr
mildocjr / whileissue1.swift
Created August 10, 2018 03:01
while-issue-1
var count: Double = -100.0
var total: Double = 25
var increment: Double = -3.75
while count < total
count += increment
}