Created
January 18, 2018 04:26
-
-
Save glowcap/6309e502f6155c9ebaf684746f96d4fe to your computer and use it in GitHub Desktop.
Example of using continue in a loop
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
| import UIKit | |
| let puzzleInput = "great minds think alike" | |
| var puzzleOutput = "" | |
| let charactersToRemove: [Character] = ["a", "e", "i", "o", "u", " "] | |
| /// When using `continue` in an `if` statement (or switch) of a loop, the | |
| /// loop immediately goes to the next iteration of the loop with completing | |
| /// the rest of the current loop iteration. | |
| /// This is different from a `break` that would simply end the loop. | |
| /// If you run this example (adapted from The Swift Programming Language), | |
| /// you'll see that when `continue` is hit, "contains vowels" is skipped. | |
| for character in puzzleInput { | |
| if charactersToRemove.contains(character) { | |
| print("continue") | |
| continue | |
| } else { | |
| puzzleOutput.append(character) | |
| print("appended") | |
| } | |
| print("contains vowels") | |
| } | |
| print(puzzleOutput) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment