Swift: The continue Keyword

continue Keyword in Swift:

Let’s discuss the continue keyword in Swift in detail.

We may not need to use every single value when iterating through a sequence. In these cases, simply telling our loop to skip a value would be sufficient. This is where the word continue comes into play in Swift programming language.

If we only wanted to print all the letters in a String except the vowels (a, e, i, o, u), we could use the following syntax:

let test = "Be Fearless";
 
for letters in test {
  switch letters {
    case "a", "e", "i", "o", "u":
      continue;
    default:
      print(letters);
  }
}

The switch statement in the above example checks letters to see if it contains any of the following letters like: "a", "e""i""o", and "u". If the letters matches in our case statement, we skip over it with the continue keyword. The character is only printed if it is not a vowel.

When we run our code, we get the following output:

B
 
F
r
l
s
s

Helpful Tools:

Here are some useful tools to help you along your journey!

Setting up an IDE (Integrated Development Environment) can be difficult for beginners. The Online Compiler will enable you to run your code inside your browser without the need to install an IDE. If you need a more detailed explanation of a specific topic, the best place to find answers is in the Official Documentation.

Scroll to Top
%d bloggers like this: