This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var hasCoffee = false | |
| var madeCup: Bool | |
| if hasCoffee { | |
| madeCup = true | |
| } else { | |
| madeCup = false | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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." |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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." | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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..." |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) || |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // The while loop | |
| var countA = 0 | |
| while countA < 10 { | |
| countA += 1 | |
| } | |
| // The repeat-while loop | |
| var countB = 0 | |
| repeat { | |
| countB += 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var madeCupCount = 0 | |
| var shouldMakeCoffee = false | |
| repeat { | |
| madeCupCount += 1 | |
| } while shouldMakeCoffee | |
| // madeCupCount = 1 | |
| while shouldMakeCoffee { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let winningScore = 100 | |
| var playerOneScore = 0 | |
| var playerTwoScore = 0 | |
| repeat { | |
| playerOneScore += 1 | |
| playerTwoScore += 2 | |
| } while (playerOneScore < winningScore) || | |
| (playerTwoScore < winningScore) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var count: Double = -100.0 | |
| var total: Double = 25 | |
| var increment: Double = -3.75 | |
| while count < total | |
| count += increment | |
| } |